【问题标题】:forking multiple processes and making the parent wait for all of them (in C)分叉多个进程并使父进程等待所有进程(在 C 中)
【发布时间】:2009-12-14 00:42:16
【问题描述】:

我正在创建各种流程(准确地说是 3 个)并让它们做不同的事情。 到现在为止还挺好。我试图在父母中等待,直到所有孩子都完成。我玩过很多选项(例如下面列出的选项),但要么父母等待,但我必须按 Enter 键才能返回到 shell(这意味着某些孩子在父母之后完成?)或者父母永远不会返回到贝壳。有任何想法吗?指向何处寻求更多帮助的指针?谢谢

 #include <sys/types.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>

 #define READ_END 0
 #define WRITE_END 1


int main (int argc, char **argv) 
{
pid_t pid;
int fd[2];
int fd2[2];

pipe(fd);
pipe(fd2);

for (int i=0; i<3; i++) {
    pid=fork();

    if (pid==0 && i==0) {

        //never uses fd2, so close both descriptors
        close(fd2[READ_END]);
        close(fd2[WRITE_END]);

        printf("i'm the child used for ls \n");
        close(fd[READ_END]); /*close read end since I don't need it */
        dup2(fd[WRITE_END], STDOUT_FILENO);
        close(fd[WRITE_END]);
        execlp("ls", "ls", "-hal", NULL);


        break; /*exit for loop to end child's code */
    }

    else if (pid==0 && i==1) {
        printf("i'm in the second child, which will be used to run grep\n");
        close(fd[WRITE_END]);
        dup2(fd[READ_END], STDIN_FILENO);
        close(fd[READ_END]);

        close(fd2[READ_END]);
        dup2(fd2[WRITE_END], STDOUT_FILENO);
        close(fd2[WRITE_END]);
        execlp("grep", "grep","p",NULL);
        break;
    }
    else if (pid==0 && i==2) {

        //never uses fd so close both descriptors
        close(fd[READ_END]);
        close(fd[WRITE_END]);

        printf("i'm in the original process which will be replaced with wc\n");

        close(fd2[WRITE_END]);
        dup2(fd2[READ_END], STDIN_FILENO);
        close(fd2[READ_END]);
        printf("going to exec wc\n");
        execlp("wc","wc","-w",NULL);
        break;
        }
        else {
        //do parenty things
        }
        }

        wait(NULL); 
        while (1){
                wait(NULL);
                if(errno== ECHILD) {
                    printf("all children ended\n"); 
                    break;
                }
                }




        close(fd[READ_END]);
        close(fd[WRITE_END]);
        close(fd2[READ_END]);
        close(fd2[WRITE_END]);






return 0;

}

【问题讨论】:

  • 由于格式被搞砸了,你能修复一下代码吗?

标签: c unix posix fork wait


【解决方案1】:

grep 和 wc 永远不会退出。

为什么?他们永远不会在标准输入上收到 EOF。

为什么?因为,即使ls已经退出并关闭了pipe(fd)的写端,主进程仍然有pipe(fd)的写端打开,所以pipe(fd)的读端还在等待更多的数据。 fd2 也有类似的情况:即使 grep 退出,wc 也不会在标准输入上获得 EOF。

解决方案:close主进程中的所有管道fds,等待之前。

【讨论】:

  • 如果它回答了您的问题,您应该单击此答案旁边的复选标记
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多