Linux C语言 如何判断该进程是否还存在

虽然shell很方便,但是我用QT的话,接shell就不方便了,这是网上拷贝的,与君分享:

 

int detect_process(char * process_name) { FILE *ptr; char buff[512]; char ps[128]; sprintf(ps,"ps -e | grep -c ' %s</p>",process_name); strcpy(buff,"ABNORMAL"); if((ptr=popen(ps, "r")) != NULL) { while (fgets(buff, 512, ptr) != NULL) { if(atoi(buff)>=2) { pclose(ptr); return CONFLICT; } } } if(strcmp(buff,"ABNORMAL")==0) /*ps command error*/ return ERROR; pclose(ptr); return NOERROR; } 

你可能感兴趣的:(c,linux,shell,command,语言,conflict)