分叉多个进程并使父进程等待所有进程(在 C 中)

发布于 2024-08-14 12:41:05 字数 2234 浏览 5 评论 0原文

我正在创建各种流程(准确地说是 3 个流程)并让它们做不同的事情。 到目前为止,一切都很好。我试图在父级中等待,直到所有子级都完成。我已经尝试过许多选项(例如下面列出的选项),但要么父级等待,但我必须按 Enter 键返回到 shell(意味着某些子级在父级之后完成?),要么父级永远不会返回到 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;

}

I'm creating various processes (3 to be precise) and making them do different things.
So far so good. I'm trying to wait in the parent until all children are completed. I've played around with many options (such as the one listed below) but either the parent waits but I have to press enter to return to the shell (meaning that some child completes after the parent?) or the parent never returns to the shell. Any ideas? pointers to where to look for more help? Thanks

 #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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-08-21 12:41:05

grepwc 永远不会退出。

为什么?他们永远不会在 stdin 上收到 EOF。

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

解决方案:在等待之前关闭主进程中的所有管道fd。

grep and wc never exit.

Why? They never receive an EOF on stdin.

Why? Because, even though ls has exited and closed the write end of pipe(fd), the main process still has the write end of pipe(fd) open, thus the read end of pipe(fd) is still waiting for more data. Similar thing goes for fd2: even if grep exited, wc wouldn't get an EOF on stdin.

Solution: close all the pipe fds in the main process before you wait.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文