本文實例講述了C語言實現查看進程是否存在的方法。分享給大家供大家參考,具體如下:
#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<limits.h>#define BUFSZ 150void err_quit(char *msg){ perror(msg); exit(EXIT_FAILURE);}int main(int argc, char *argv[]){ FILE* fp; int count; char buf[BUFSZ]; char command[150]; sprintf(command, "ps -ef | grep **** | grep -v grep | wc -l" ); if((fp = popen(command,"r")) == NULL) err_quit("popen"); if( (fgets(buf,BUFSZ,fp))!= NULL ) { count = atoi(buf); if(count == 0) printf("not found/n"); else printf("process :tdv1 total is %d/n",count); } pclose(fp); exit(EXIT_SUCCESS);}
希望本文所述對大家C語言程序設計有所幫助。
新聞熱點
疑難解答
圖片精選