execShell.c

来源:互联网 发布:mac怎么解压缩zip 编辑:程序博客网 时间:2024/04/29 11:13

#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;
   
}

原创粉丝点击