C语言判断当前某一个进程是否存在
2016-07-13 10:58
761 查看
#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); }
相关文章推荐
- C语言获取当前登陆的用户名的UID
- C语言获取当前登陆的用户名
- C++ STL标准模板库(vector)
- C++Primer(5th)学习-数组篇
- 关于c++ 学习
- 求5阶矩阵其对角线上所有元素之和
- inf & nan
- C++中typename和class的区别
- <<More Effective C++>>读书笔记1: 基础议题
- C++建立及销毁链表
- c++多态
- C++中 overload 、override、overwrite 之间的区别
- 排序算法系列---快速选择排序(C++)
- C语言中lseek()函数和fseek()函数的使用详解
- Static关键字所有用法详解
- c/c++ 动态内存配置的使用原则
- C语言 程序 画圆、画圆和y=x^2
- Leetcode 7. Reverse Integer (Easy) (cpp)
- 《C和指针》总结
- C语言的fopen函数(文件操作/读写)