如何分叉多个进程,当一个使用wait()函数? [英] How does one use the wait() function when forking multiple processes?

查看:141
本文介绍了如何分叉多个进程,当一个使用wait()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习使用的叉()命令和管道如何到父之间的数据和它的子女。我目前正在写一个简单的程序来测试叉和管道的功能是如何工作的。我的问题似乎是等待功能的正确使用/位置。我希望家长等待其两个孩子完成处理。这里是code我到目前为止有:

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?

谢谢,

托梅克

推荐答案

这似乎大多是OK(我没有运行它,请注意)。你的逻辑错误是假设孩子会在某些特定的顺序结束;不检查的结果等待(0)针对特定的PID,除非你确信你知道你要找回其中之一!

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!

编辑:

我跑你的程序;你必须至少有一个错误,你的第二个孩子进程调用等待(),你可能根本不想做的事。我建议打破你的一些code的伸到功能,让你可以更清楚地看到你表演没有所有的混乱操作的顺序。

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.

这篇关于如何分叉多个进程,当一个使用wait()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆