0
我尝试编辑我的文章,因为它存在一些问题。C中的多个管道,等待输入的程序
我仍然迷失,试图扩大我的程序。当我运行程序时,进入只需要一些输入的状态 - 就像,也许是因为我没有在我的管道过程中输入第二个程序。
我试图遵循这个职位代码:Does this multiple pipes code in C makes sense?
我的代码如下所示:
int status;
int newpipe[2];
int oldpipe[2];
pid_t pid;
int countcmds = 0;
while (firstCmd != NULL) {
printf("En iteration \n");
if (firstCmd -> next != NULL) {
pipe(newpipe);
}
pid = fork();
if(pid == 0){
if (firstCmd -> prev != NULL) {
dup2(oldpipe[0],0);
close(oldpipe[0]);
close(oldpipe[1]);
}
if (firstCmd -> next != NULL) {
close(newpipe[0]);
dup2(newpipe[1],1);
close(newpipe[1]);
}
char** file = firstCmd -> cmd;
char* specfile = *file;
execvp(specfile, file);
}
else{
waitpid(pid, &status, 0);
if (firstCmd -> prev != NULL) {
close(oldpipe[0]);
close(oldpipe[1]);
}
if(firstCmd -> next != NULL){
oldpipe[0] = newpipe[0];
oldpipe[1] = newpipe[1];
}
countcmds++;
firstCmd = firstCmd -> next;
}
}
if(countcmds){
close(oldpipe[0]);
close(oldpipe[1]);
}
请问[这个问题]的答案(http://stackoverflow.com/questions/948221/does-this-multiple-pipes-code-in-c-makes-sense)有帮助吗? – legoscia
我看到你接受的答案,谢谢。现在,你能准确解释“不起作用”是什么意思吗?你得到了什么,你期望什么? – netcoder
现在自己发现了,现在工作:)! – user1090614