C程序监测进程是否存在

#include 
#include 
#include 
 

int main()
{
    FILE *ptr = NULL;
    char cmd[128] = "ps -ef | grep mp_client0 | grep -v grep | wc -l";
    int status = 0;
    char buf[150];
    int count;

    if((ptr = popen(cmd, "r"))==NULL)
    {   
        printf("popen err\n");  
    }   
    memset(buf, 0, sizeof(buf));

    if((fgets(buf, sizeof(buf),ptr))!= NULL)//获取进程和子进程的总数
    {   
        count = atoi(buf);
        if(count <= 0)//当进程数小于等于0时,说明进程不存在
        {   
            printf("not exist \n");
        }else{

            printf("exist \n");
        }   

    }   

}

你可能感兴趣的:(Linux命令,编程问题)