免费注册 查看新帖 |

Chinaunix

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

为什么被阻塞了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-14 10:01 |只看该作者 |倒序浏览
我想实现 ls | more | wc,但exec的顺序是反着来的,运行后始终没有结果,我猜是被阻塞了。请各位帮忙看一下,谢谢了。

  1. /*
  2. * ls | more | wc
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>

  7. int main()
  8. {
  9.         pid_t pids[10];
  10.         int pipes[10][2];
  11.         char *ls[] = {"ls", NULL};
  12.         char *more[] = {"more", NULL};
  13.         char *wc[] = {"wc", NULL};
  14.         int i;

  15.         /*
  16.          * wc
  17.          */
  18.         for(i = 0; i < 10; i++){
  19.                 if(pipe(pipes[i]) == -1){
  20.                         printf("pipe error\n");
  21.                 }
  22.         }
  23.         if((pids[2] = fork()) == -1){
  24.                 perror("fork error");
  25.         } else if(pids[2] == 0){
  26.                 //输入
  27.                 close(pipes[1][1]);
  28.                 dup2(pipes[1][0], STDIN_FILENO);
  29.                 close(pipes[1][0]);

  30.                 if(execvp(wc[0], wc) == -1){
  31.                         printf("exec %s failed\n", ls[0]);
  32.                 }
  33.         } else if(pids[2] > 0){
  34.         }
  35.         /*
  36.          * more
  37.          */
  38.         if((pids[1] = fork()) == -1){
  39.                 perror("fork error");
  40.         } else if(pids[1] == 0){
  41.                 //输入
  42.                 close(pipes[0][1]);
  43.                 dup2(pipes[0][0], STDIN_FILENO);
  44.                 close(pipes[0][0]);
  45.                 //输出
  46.                 close(pipes[1][0]);
  47.                 dup2(pipes[1][1], STDOUT_FILENO);
  48.                 close(pipes[1][1]);

  49.                 if(execvp(more[0], more) == -1){
  50.                         printf("exec %s failed\n", more[0]);
  51.                 }
  52.         } else if(pids[1] > 0){
  53.         }
  54.         /*
  55.          * ls
  56.          */
  57.         if((pids[0] = fork()) == -1){
  58.                 perror("fork error");
  59.         } else if(pids[0] == 0){
  60.                 //输出
  61.                 close(pipes[0][0]);
  62.                 dup2(pipes[0][1], STDOUT_FILENO);
  63.                 close(pipes[0][1]);

  64.                 if(execvp(ls[0], ls) == -1){
  65.                         printf("exec %s failed\n", ls[0]);
  66.                 }
  67.         } else if(pids[0] > 0){
  68.                 waitpid(pids[0], NULL, 0);
  69.         }

  70.         close(pipes[0][0]);
  71.         close(pipes[0][1]);
  72.         waitpid(pids[1], NULL, 0);
  73.         close(pipes[1][0]);
  74.         close(pipes[1][1]);
  75.         waitpid(pids[2], NULL, 0);

  76.         return 0;
  77. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-14 17:39 |只看该作者
帮顶,如果只是1个管道的话,ls|more没问题,但2个管道就在more处阻塞了.希望高人指点下.
  1. #include <stdio.h>

  2. #include <unistd.h>

  3. #include <sys/wait.h>
  4. #include <sys/types.h>
  5. #include <cstdlib>
  6. #include <iostream>
  7. using namespace std;

  8. int isready(int fd)
  9. {
  10.         int rc;
  11.         fd_set fds;
  12.         struct timeval tv;

  13.         FD_ZERO(&fds);
  14.         FD_SET(fd, &fds);
  15.         tv.tv_sec = 1;
  16.         tv.tv_usec = 0;
  17.         rc = select(fd + 1, &fds, NULL, NULL, &tv);
  18.         if (rc < 0) //error
  19.                 return -1;

  20.         return FD_ISSET(fd, &fds) ? 1 : 0;
  21. }
  22. int main()

  23. {

  24.         pid_t pids[10];

  25.         int pipes[10][2];

  26.         char *ls[] =
  27.         { "ls", NULL };

  28.         char *more[] =
  29.         { "more", NULL };

  30.         char *wc[] =
  31.         { "wc", NULL };

  32.         int i, status;

  33.         for (i = 0; i < 10; i++)
  34.         {

  35.                 if (pipe(pipes[i]) == -1)
  36.                 {

  37.                         printf("pipe error\n");

  38.                 }

  39.         }

  40.         if ((pids[0] = fork()) == -1)
  41.         {

  42.                 perror("fork error");

  43.         }
  44.         else if (pids[0] == 0)
  45.         {

  46.                 //输出

  47.                 close(pipes[0][0]);
  48.                 close(STDOUT_FILENO);

  49.                 dup2(pipes[0][1], STDOUT_FILENO);

  50.                 close(pipes[0][1]);

  51.                 if (execvp(ls[0], ls) == -1)
  52.                 {

  53.                         printf("exec %s failed\n", ls[0]);

  54.                 }

  55.         }
  56.         else if (pids[0] > 0)
  57.         {

  58.                 wait(NULL);

  59.                 if ((pids[2] = fork()) == -1)
  60.                 {

  61.                         perror("fork error");

  62.                 }
  63.                 else if (pids[2] == 0)
  64.                 {

  65.                         //输入
  66.                         //sleep(2);
  67.                         if (isready(pipes[0][0]) > 0)
  68.                         {
  69.                                 cout << "ready" << endl;
  70.                                 close(pipes[0][1]);
  71.                                 close(STDIN_FILENO);
  72.                                 dup2(pipes[0][0], STDIN_FILENO);

  73.                                 close(pipes[0][0]);

  74.                                 //输出

  75.                                 close(pipes[1][0]);
  76.                                 close(STDOUT_FILENO);
  77.                                 dup2(pipes[1][1], STDOUT_FILENO);
  78.                                 dup2(pipes[1][1], STDERR_FILENO);

  79.                                 close(pipes[1][1]);

  80.                                 if (execvp(more[0], more) == -1)
  81.                                 {

  82.                                         printf("exec %s failed\n", more[0]);

  83.                                 }
  84.                         }

  85.                 }
  86.                 else if (pids[2] > 0)
  87.                 {
  88.                         //输入
  89.                         wait(NULL);
  90.                         ////waitpid( pids[2], &status, 0 );

  91.                         if (isready(pipes[1][0]) > 0)
  92.                         {
  93.                                 cout << "ready 2" << endl;
  94.                                 close(pipes[1][1]);
  95.                                 close(STDIN_FILENO);

  96.                                 dup2(pipes[1][0], STDIN_FILENO);

  97.                                 close(pipes[1][0]);

  98.                                 if (execvp(wc[0], wc) == -1)
  99.                                 {

  100.                                         printf("exec %s failed\n", ls[0]);

  101.                                 }
  102.                         }

  103.                 }

  104.         }

  105.         close(pipes[0][0]);

  106.         close(pipes[0][1]);

  107.         close(pipes[1][0]);

  108.         close(pipes[1][1]);

  109.         return 0;

  110. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-03-14 22:36 |只看该作者
回复 2# chllcy88


    是啊,我试两个的也没问题,一扩展就有问题了。

论坛徽章:
0
4 [报告]
发表于 2012-03-14 22:58 |只看该作者
回复 2# chllcy88


    看 http://bbs.chinaunix.net/thread-3683342-1-1.html  也是我问的,看10楼和11楼
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP