【发布时间】:2011-11-01 21:41:51
【问题描述】:
我正在做一个项目,除了一个小(大)问题外,我大部分都已经弄清楚了。我似乎无法弄清楚如何在任意数量的孩子之间创建管道。
例如,我使用命令行参数来确定将产生多少个孩子。第一个孩子没有输入但有输出,最后一个孩子输出到 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);
}
现在我只将三个程序传递给 argv[] 并且它工作正常。我将不得不在我的 for 循环中添加一个 if 语句来检查 i 的最后/最高可能值,以将 y2z 管道连接到 zchild。我在 for 循环中将孩子们相互连接起来时遇到了麻烦。我将如何从最后一个孩子为每个孩子创建一个新管道?
【问题讨论】:
-
哇,18 行 just
#includes确实占据了 80% 的屏幕空间。你听说过最小的工作示例吗? -
请不要
#includeC 和 C++ 版本的标头。选择其中一个并坚持下去。 -
我之前已经为 C 回答过这个问题:How do I chain stdout in one child process to stdin in another child in C? 这可能对你有帮助,因为它看起来不像你真的在使用 C++(提示:使用
cout没有' t 使它成为 C++)。 -
那里,FTFY:14 个冗余包括消失(g++,linux)。剩下的恐怕超出了我的维修能力
-
@sehe:我喜欢你的名字在你写有趣的 cmets 时看起来像“呵呵”