简体   繁体   English

Unix管道-三个进程之间的管道

[英]Unix Pipes - Pipeline between three processes

I'm creating a small program which contains three processes; 我正在创建一个包含三个进程的小程序; a source process, a filter process and a sink process. 源过程,过滤过程和接收过程。 The stdout of the source process is redirected to the stdin of the filter process, and the filter process' stdout is redirected to the sink process' stdin. 源进程的标准输出重定向到过滤器进程的标准输入,过滤器进程的标准输出重定向到接收器进程的标准输入。

My problem is that no output is printed to stdout from the sink process. 我的问题是没有输出从接收器进程输出到stdout。 Can any of you see the problem in the following tiny snippet of code? 在下面的一小段代码中,你们中的任何人都能看到问题吗?

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char** argv)
{
     // Pipes
     // pipe1 is from source to filter, pipe2 is from filter to sink
     int pipe1[2], pipe2[2];

     // Create pipes
     if (pipe(pipe1) < 0 || pipe(pipe2) < 0)
     {
          perror("Creating pipes failed!");
     }

     if (fork() == 0)
     {
          close(1);
          dup(pipe1[1]);
          close(pipe1[0]);

          close(pipe2[0]);
          close(pipe2[1]);

          execlp("ls", "ls", NULL);
          exit(0);
     }
     else
     {
          if (fork() == 0)
          {
               close(0);
               dup(pipe1[0]);
               close(pipe1[1]);

               close(1);
               dup(pipe2[1]);
               close(pipe2[0]);

               execlp("sort", "sort", NULL);
               exit(0);
          }
          else
          {
               if (fork() == 0)
               {

                    close(0);
                    dup(pipe2[0]);

                    execlp("more", "more", NULL);
                    exit(0);
               }
          }
     }


     wait(NULL);
     printf("Done.\n");

     return 0;
}

BR BR

Jacob 雅各布

I think problem may be, wait will only wait for one process. 我认为可能是问题, wait只会等待一个过程。 And when the parent exits after first child returns, I suspect more command also decides to terminate, because it may get SIGHUP (speculation, not sure). 当第一个孩子返回后父母退出时,我怀疑more命令决定终止,因为它可能会收到SIGHUP(推测,不确定)。

But, check for errors from on all system calls! 但是,请检查所有系统调用中的错误! Also for wait calls which succeeded, print why the child exited (was it signal or normal exit, and if it was normal exit, what was exit code). 同样对于成功的wait调用,打印孩子退出的原因(发出信号还是正常退出,如果是正常退出,则是退出代码)。

Also note, perror does not exit, it only prints. 另请注意, perror不会退出,只会打印出来。

It is kind of pointless trying to see why some code fails, if it does not have error handling in it... 尝试查看为什么某些代码失败(如果其中没有错误处理)是毫无意义的……

Some easy way to do pipes for your scenario: 一些简单的方法来为您的方案做管道:

char cmd[MAX_LEN];
sprintf(cmd, "%s | %s | %s", app1, app2, app3); //app123 holds app name + args
system(cmd);

if you want to capture the output of the last app, use popen: 如果要捕获最后一个应用程序的输出,请使用popen:

FILE pPipe = popen(cmd, "rt"); /* same access flag as fopen()*/
while (NULL != fget(buf, buf_len, pPipe)) {
    // do something with the read line in 'buf'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM