2

我正在创建各种流程(准确地说是 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;

}

4

1 回答 1

7

grep并且wc永远不会退出。

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

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

解决方法:close等待主进程中的所有管道fds。

于 2009-12-14T01:05:10.110 回答