linux c 编程 通过shell中的system命令,根据文件名查找进程号,
来源:互联网 发布:淘宝洗照片不清楚 编辑:程序博客网 时间:2024/06/11 20:28
开始想通过cut -f 2 -d " " 的方法获得,但是发现结果返回的是空行,无奈,觉得自己实现!!!!
通过system() 来执行shell 命令,然后通过文件操作对文件内容进行读取!
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 100
int main()
{
int ret1 = 0;
int ret2 = 0;
char buf[BUFSIZE];
FILE *fp = NULL;
char ch ;
int flag1 = 0;
int flag2 = 0;
char pidBuf[10];
int index = 0;
int result = 0;
printf("当前进程的进程号为%d/n",getpid());
ret1 = system("ps aux > ps.txt");
printf("ret1 = %d/n",ret1);
if(0 != ret1)
{
printf("system is called failed/n");
return ;
}
ret2 = system("grep bash ps.txt> grep.txt");
printf("ret2 = %d/n",ret2);
if(127 == ret2 | ret2 < 0)
{
printf("system is called failed/n");
return ;
}
fp = fopen("grep.txt","r+");
if(NULL == fp)
{
printf("can not open file grep.txt/n");
return ;
}
while(EOF != (ch = getc(fp)))
{
if (' ' == ch && 0 == flag1)
{
flag1 = 1;
}
if (1 == flag1 && ' ' != ch)
{
pidBuf[index++] = ch;
flag2 = 1;
}
if (1 == flag2 && ' ' == ch)
{
pidBuf[index] = '/0';
break;
}
}
result = atoi(pidBuf);
printf("%s,%d/n",pidBuf,result);
return 0;
}
- linux c 编程 通过shell中的system命令,根据文件名查找进程号,
- linux 根据端口查找进程号
- linux中的find命令——查找文件名
- linux中的find命令——查找文件名
- linux中的find命令——查找文件名
- linux中的find命令——查找文件名
- linux中的find命令——查找文件名
- linux中的find命令——查找文件名
- shell编程,根据文件名找出不同文件
- Linux根据内容查找所在文件名
- linux中根据端口号来关闭进程的shell命令
- Linux 根据端口查找进程
- 根据文件名查找进程打开的文件全路径
- Linux Shell编程中的几个特殊符号命令 & 、&& 、 ||
- linux根据进程id查找进程
- Linux 根据进程名查找进程ID
- linux c编程中调用 shell 命令
- linux C 语言的 system(执行shell 命令)
- sql递归
- WindML Driver for Mesa 4.0
- 引用类型实例
- 道德经 第五章
- MVC学习资料网站
- linux c 编程 通过shell中的system命令,根据文件名查找进程号,
- windows下进程通讯的方法
- 移植boa出现的错误及解决方法
- crontab命令用法与格式
- MyEclipse 无法保存中文解决方案
- PXA270 shell重复显示
- moncler
- Linux 之文件编程(三)
- 滤波过程(继续完善)