C++ C++;多个子对象之间的管道

C++ C++;多个子对象之间的管道,c++,fork,pipe,C++,Fork,Pipe,我正在做一个项目,除了一个小(大)问题外,我基本上解决了它。我似乎不知道如何在任意数量的孩子之间创建管道 例如,我使用命令行参数来确定将生成多少子级。第一个子级没有输入,但有输出,最后一个子级输出到STD输出。我需要将值依次传递给第一个子级和之后的每个子级。以下是我得到的: #include <errno.h> #include <cstdio> #include <iostream> #include <sys/wait.h> using na

我正在做一个项目,除了一个小(大)问题外,我基本上解决了它。我似乎不知道如何在任意数量的孩子之间创建管道

例如,我使用命令行参数来确定将生成多少子级。第一个子级没有输入,但有输出,最后一个子级输出到STD输出。我需要将值依次传递给第一个子级和之后的每个子级。以下是我得到的:

#include <errno.h>
#include <cstdio>
#include <iostream>
#include <sys/wait.h>

using namespace std;

int main(int argc, char *argv[]) {
    pid_t childpid;
    int x2ypipe[2];
    pipe(x2ypipe);
    if(x2ypipe==0) {
        cout<<"ERROR:"<<errno<<endl;
    }
    int y2zpipe[2];
    pipe(y2zpipe);
    if(y2zpipe==0) {
        cout<<"ERROR:"<<errno<<endl;
    }
    pid_t xchild =fork();
    if(xchild==0) {
        dup2(x2ypipe[1],STDOUT_FILENO);
        close(x2ypipe[0]);
        close(x2ypipe[1]);
        int a=execl(argv[1],argv[1], (char*)NULL);
        if(a==-1) {
            perror("The following error occurred at A");
        }
    }
    for(int i=2; i<(argc-1); i++) {
        childpid =fork();
        if(childpid==0) {
            dup2(x2ypipe[0],STDIN_FILENO);
            close(x2ypipe[0]);
            close(x2ypipe[1]);
            //direct y2z pipe to standard output and replace the child with the program part2
            dup2(x2ypipe[1],y2zpipe[1]);
            dup2(y2zpipe[1],STDOUT_FILENO);
            close(y2zpipe[0]);
            close(y2zpipe[1]);
            int b=execl(argv[i],argv[i],(char *)NULL);
            if(b==-1) {
                perror("The following error occurred at B");
            }
        }
    }
    pid_t zchild =fork();
    if(zchild==0) {
        dup2(y2zpipe[0],STDIN_FILENO);
        close(y2zpipe[0]);
        close(y2zpipe[1]);
        int c=execl(argv[argc-1],argv[argc-1],(char *)NULL);
        if(c==-1) {
            perror("The following error occurred at C");
        }
    }
    close(x2ypipe[0]);
    close(x2ypipe[1]);
    wait(NULL);
    wait(NULL);
    wait(NULL);
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
pid_t childpid;
int-x2ype[2];
管道(x2ype);
如果(x2yppe==0){

cout在每对连接的进程之间需要一个单独的管道。

这可能会有所帮助。注意我如何在for循环中调用
pipe()
,因此我不必为管道对想出新的“x2y”、“y2z”、“z2omega”等名称

还要注意我是如何使用for循环外部的变量
prevfd
将上一次迭代的管道文件描述符带入下一次迭代的,以及它是如何指向“/dev/null”的

最后,请注意我在循环中调用
wait()
的次数与我需要的次数一样精确,而不是写3次(或4次、5次或…1397次)

#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
int-prevfd;
prevfd=open(“/dev/null”,仅限ordu);
if(prevfd<0){
perror(“/dev/null”);
出口(1);
}
对于(int i=1;i哇,18行的代码< < /代码>确实占用了80%的屏幕房地产。你听说过最小的工作示例吗?请不要<代码>包含了C++和C++版本的标题。请选择一个并坚持它。我以前回答过C这个问题:这可能对你有帮助,因为它看起来不像你。真的使用C++(提示:使用代码> CUT<代码>不构成C++)。FTFY:14冗余包括FEG(G++,Linux)。skills@sehe当前位置当你写有趣的评论时,我喜欢你的名字看起来像“呵呵”
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <cstdlib>
#include <cstdio>
#include <sys/wait.h>

int main(int argc, char *argv[]) {

  int prevfd;

  prevfd = open("/dev/null", O_RDONLY);

  if(prevfd < 0) {
    perror("/dev/null");
    exit(1);
  }

  for(int i = 1; i < argc; ++i) {
    int pipefd[2];
    int kid;

    if(i != argc-1 && pipe(pipefd)) {
      perror("pipe");
      break;
    }

    if(!fork()) {
      dup2(prevfd, 0);
      close(prevfd);
      if(i != argc-1) {
        dup2(pipefd[1], 1);
        close(pipefd[0]);
        close(pipefd[1]);
      }
      execl(argv[i], argv[i], (char*)0);
      perror(argv[i]);
      exit(1);
    }

    close(prevfd);
    prevfd = pipefd[0];
    close(pipefd[1]);
  }
  while(wait((int*)0) != -1)
    ;
  return 0;
}