fork 多个进程时如何使用 wait() 函数?

发布于 2024-08-21 15:23:49 字数 1839 浏览 3 评论 0原文

学习使用 fork() 命令以及如何在父级和子级之间传输数据。我目前正在尝试编写一个简单的程序来测试 fork 和 pipeline 函数的工作原理。我的问题似乎是等待函数的正确使用/放置。我希望父级等待其两个子级完成处理。这是我到目前为止的代码:

int main(void)
{
    int n, fd1[2], fd2[2];
    pid_t pid;
    char line[100];

    if (pipe(fd1) < 0 || pipe(fd2) < 0)
    {
        printf("Pipe error\n");
        return 1;
    }

    // create the first child
    pid = fork();

    if (pid < 0)
        printf("Fork Error\n");
    else if (pid == 0)  // child segment
    {
        close(fd1[1]);  // close write end
        read(fd1[0], line, 17); // read from pipe
        printf("Child reads the message: %s", line);

        return 0;
    }
    else    // parent segment
    {
        close(fd1[0]);  // close read end
        write(fd1[1], "\nHello 1st World\n", 17);   // write to pipe

        // fork a second child
        pid = fork();

        if (pid < 0 )
            printf("Fork Error\n");
        else if (pid == 0) // child gets return value 0 and executes this block
            // this code is processed by the child process only
        {
            close(fd2[1]);  // close write end
            read(fd2[0], line, 17); // read from pipe
            printf("\nChild reads the message: %s", line);
        }
        else
        {
            close(fd2[0]);  // close read end
            write(fd2[1], "\nHello 2nd World\n", 17);   // write to pipe

            if (wait(0) != pid)
                printf("Wait error\n");
        }

        if (wait(0) != pid)
            printf("Wait error\n");

    }

    // code executed by both parent and child
    return 0;
}   // end main

目前我的输出看起来类似于:

./fork2 
Child reads the message:  Hello 1st World 
Wait error

Child reads the message:  Hello 2nd World 
Wait error

哪里是让父母等待的适当位置?

谢谢,

托梅克

Learning to use the fork() command and how to pipe data between a parent and it's children. I am currently trying to write a simple program to test how the fork and pipe functions work. My problem seems to be the correct use/placement of the wait function. I want the parent to wait for both of its children to finish processing. Here is the code I have so far:

int main(void)
{
    int n, fd1[2], fd2[2];
    pid_t pid;
    char line[100];

    if (pipe(fd1) < 0 || pipe(fd2) < 0)
    {
        printf("Pipe error\n");
        return 1;
    }

    // create the first child
    pid = fork();

    if (pid < 0)
        printf("Fork Error\n");
    else if (pid == 0)  // child segment
    {
        close(fd1[1]);  // close write end
        read(fd1[0], line, 17); // read from pipe
        printf("Child reads the message: %s", line);

        return 0;
    }
    else    // parent segment
    {
        close(fd1[0]);  // close read end
        write(fd1[1], "\nHello 1st World\n", 17);   // write to pipe

        // fork a second child
        pid = fork();

        if (pid < 0 )
            printf("Fork Error\n");
        else if (pid == 0) // child gets return value 0 and executes this block
            // this code is processed by the child process only
        {
            close(fd2[1]);  // close write end
            read(fd2[0], line, 17); // read from pipe
            printf("\nChild reads the message: %s", line);
        }
        else
        {
            close(fd2[0]);  // close read end
            write(fd2[1], "\nHello 2nd World\n", 17);   // write to pipe

            if (wait(0) != pid)
                printf("Wait error\n");
        }

        if (wait(0) != pid)
            printf("Wait error\n");

    }

    // code executed by both parent and child
    return 0;
}   // end main

Currently my output looks something along the lines of:

./fork2 
Child reads the message:  Hello 1st World 
Wait error

Child reads the message:  Hello 2nd World 
Wait error

Where is the appropriate place to make the parent wait?

Thanks,

Tomek

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

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

发布评论

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

评论(2

月寒剑心 2024-08-28 15:23:49

这看起来基本上没问题(请注意,我没有运行它)。你的逻辑错误在于假设孩子们将以某种特定的顺序结束;不要根据特定的 pid 检查 wait(0) 的结果,除非您确定知道要返回哪一个!

编辑:

我运行了你的程序;您确实至少有一个错误,您的第二个子进程调用 wait(),您可能不想这样做。我建议将一些代码分解为函数,这样您就可以更清楚地看到正在执行的操作顺序,而不会出现任何混乱。

That seems mostly ok (I didn't run it, mind you). Your logic error is in assuming that the children will end in some particular order; don't check the results of wait(0) against a particular pid unless you're sure you know which one you're going to get back!

Edit:

I ran your program; you do have at least one bug, your second child process calls wait(), which you probably didn't want to do. I recommend breaking some of your code out into functions, so you can more clearly see the order of operations you're performing without all the clutter.

ゞ花落谁相伴 2024-08-28 15:23:49

我认为最好使用这样的东西,以便等待所有的孩子。

int stat;
while (wait(&stat) > 0)
   {}   

i think its better to use something like this, in order to wait for all the childrens.

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