免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1193 | 回复: 0
打印 上一主题 下一主题

子进程间通讯, 实现 ps -x |grep smb ,请帮我看看代码! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-09 15:27 |只看该作者 |倒序浏览

  1. #include <stdio.h>;

  2. int main(void)
  3. {
  4.     int pfd[2],pfd1[2];
  5.     int pid;
  6.         int pid1;
  7.     char buf[2000];
  8.     if (pipe(pfd) == -1)
  9.     {
  10.        perror("pipe failed");
  11.        exit(1);
  12.     }

  13.     if ((pid = fork()) < 0)
  14.     {
  15.        perror("fork failed");
  16.        exit(2);
  17.     }

  18.                 if(pid == 0)
  19.         {       
  20.            close(pfd[0]);
  21.        dup2(pfd[1], 1);
  22.        close(pfd[1]);
  23.        execlp("ps", "ps","-x",  (char *) 0);
  24.        perror("ps failed");
  25.        exit(4);
  26.     }
  27. dup2(pfd[0],0);
  28. close(pfd[0]);
  29. read(0,buf,2000);
  30.     if (pipe(pfd1) == -1)
  31.     {
  32.        perror("pipe failed");
  33.        exit(1);
  34.     }

  35.                     if ((pid1 = fork()) < 0)
  36.     {
  37.        perror("fork failed");
  38.        exit(2);
  39.     }
  40.        
  41.         if(pid1>;0)
  42.         {
  43. close(pfd1[0]);
  44. write(pfd1[1],buf,2000);
  45.         }
  46. if (pid1 == 0)
  47.    {
  48.        close(pfd1[1]);
  49.        dup2(pfd1[0], 0);
  50.        close(pfd1[0]);
  51.       // execlp("grep", "grep","smb",  (char *) 0);
  52.            read(0,buf,2000);
  53.            write(1,buf,2000);
  54.        exit(3);
  55.     }

  56.     exit(0);
  57. }
复制代码

在上述代码中,
在pid2进程可读到pid的并可以输出了,但为什么执行
execlp("grep", "grep","smb",  (char *) 0);
则,没有任何结果输出?

请各大虾指点!
在终端执行: ps -x |grep smb

2229 ?        S      0:00 smbd -D
8907 ?        S      0:01 smbd -D
13499 pts/1    S      0:00 grep smb
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP