swprogress.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: swprogress.c
  4. * CONTENT: 进程
  5. * NOTE:
  6. * HISTORY:
  7. * 1, [2010-12-21] created by NiuJiuRu
  8. ***********************************************************************/
  9. #include "swapi.h"
  10. #include "swstring.h"
  11. #include "swdir.h"
  12. #include "swprogress.h"
  13. /* 得到当前进程的PID */
  14. int xGetCurrentProcessId()
  15. {
  16. return getpid();
  17. }
  18. /* 得到当前进程所对应的命令文件名 */
  19. char *sw_progress_getCMD(char *cmd)
  20. {
  21. if(!cmd) return NULL;
  22. xGetSelfRunningInfo(NULL, cmd);
  23. return cmd;
  24. }
  25. /* 根据进程ID杀死进程 */
  26. void sw_progress_killByPID(int pid)
  27. {
  28. kill(pid, SIGKILL);
  29. }
  30. /* 根据命令文件名杀死进程 */
  31. void sw_progress_killByCMD(const char *cmd, int ignorePID)
  32. {
  33. FILE *fp = NULL;
  34. char buf[MAX_LINE_CHARS] = { 0 };
  35. int pid = 0;
  36. // 创建进程快照
  37. fp = popen("ps -e", "r");
  38. if(!fp) return;
  39. // 遍历所有进程
  40. while(fgets(buf, sizeof(buf), fp) != NULL)
  41. {
  42. sscanf(buf, "%d", &pid); // 得到进程PID
  43. if(xstrcasestr(buf, "right", cmd) != NULL && pid != ignorePID)
  44. sw_progress_killByPID(pid);
  45. memset(buf, 0, sizeof(buf));
  46. }
  47. // 关闭快照句柄
  48. pclose(fp);
  49. }