加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

C语言判断当前某一个进程是否存在

发布时间:2021-07-06 05:57:30 所属栏目:大数据 来源: https://blog.csdn.net/yilovex
导读:#include stdio.h #include stdlib.h #include unistd.h #include fcntl.h #include limits.h #include sys/types.h #include sys/wait.h #define BUFSZ 150 void err_quit(char *msg);int main(int argc,char *argv[]) { FILE* fp;int count;char buf[BUFSZ
#include <stdio.h>   
#include <stdlib.h>   
#include <unistd.h>   
#include <fcntl.h>   
#include <limits.h>   
#include <sys/types.h>   
#include <sys/wait.h>   

#define BUFSZ 150   

void err_quit(char *msg);


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("进程不存在!n");   
		
		else  

			printf("进程已找到,有%d个!n",count);   
	} 

	pclose(fp);   
	return EXIT_SUCCESS;
}


void err_quit(char *msg) {

	perror(msg);   
	exit(EXIT_FAILURE);   
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读