26

我需要使用fork()andwait()函数来完成作业。我们正在对非确定性行为进行建模,fork()如果存在多个可能的转换,则需要该程序。

为了尝试弄清楚如何fork工作wait,我刚刚制作了一个简单的程序。我想我现在明白了调用是如何工作的,如果程序只分支一次就可以了,因为父进程可以使用单个子进程的退出状态来确定子进程是否达到接受状态。

正如您从下面的代码中看到的那样,我希望能够处理必须有多个子进程的情况。我的问题是您似乎只能使用一次_exit功能设置状态。因此,在我的示例中,父进程测试的退出状态显示第一个子进程发出 0 作为其退出状态,但没有关于第二个子进程的信息。

我尝试简单地_exit()拒绝拒绝,但随后该子进程将继续进行,实际上似乎有两个父进程。

对不起华夫饼,但如果有人能告诉我我的父进程如何获得多个子进程的状态信息,我将不胜感激,或者我很高兴父进程只注意到来自子进程的接受状态,但在那种情况下,我需要成功退出具有拒绝状态的子进程。

我的测试代码如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>

int main(void)  {

    pid_t child_pid, wpid, pid;
    int status = 0;
    int i;

    int a[3] = {1, 2, 1};
    for(i = 1; i < 3; i++)  {
        printf("i = %d\n", i);
        pid = getpid();
        printf("pid after i = %d\n", pid);
        if((child_pid = fork()) == 0)  {
            printf("In child process\n");
            pid = getpid();
            printf("pid in child process is %d\n", pid);
            /* Is a child process */
            if(a[i] < 2)  {
                printf("Should be accept\n");
                _exit(1);
            } else  {
                printf("Should be reject\n");
                _exit(0);
            }
        }
    }

    if(child_pid > 0)  {
        /* Is the parent process */
        pid = getpid();
        printf("parent_pid = %d\n", pid);
        wpid = wait(&status);
        if(wpid != -1)  {
            printf("Child's exit status was %d\n", status);
            if(status > 0)  {
                printf("Accept\n");
            } else  {
                printf("Complete parent process\n");
                if(a[0] < 2)  {
                    printf("Accept\n");
                } else  {
                    printf("Reject\n");
                }
            }
        }
    }
    return 0;
}
4

3 回答 3

37

在我看来,基本问题似乎是你有一个wait()电话,而不是一个等待直到没有更多孩子的循环。您也只等待最后一个fork()成功而不是至少一个fork()成功。

_exit()当您不想要正常的清理操作时才应使用 - 例如刷新打开的文件流,包括stdout. 有场合使用_exit();这不是其中的一个。(在这个例子中,当然,你也可以简单地让孩子们返回而不是exit()直接调用,因为从返回main()等同于以返回的状态退出。但是,大多数情况下你会在其他函数中进行分叉等比main(), thenexit()通常是合适的。)


您的代码的黑客简化版本,可提供我想要的诊断。请注意,您的for循环跳过了数组的第一个元素(我的没有)。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
    pid_t child_pid, wpid;
    int status = 0;
    int i;
    int a[3] = {1, 2, 1};

    printf("parent_pid = %d\n", getpid());
    for (i = 0; i < 3; i++)
    {
        printf("i = %d\n", i);
        if ((child_pid = fork()) == 0)
        {
            printf("In child process (pid = %d)\n", getpid());
            if (a[i] < 2)
            {
                printf("Should be accept\n");
                exit(1);
            }
            else
            {
                printf("Should be reject\n");
                exit(0);
            }
            /*NOTREACHED*/
        }
    }

    while ((wpid = wait(&status)) > 0)
    {
        printf("Exit status of %d was %d (%s)\n", (int)wpid, status,
               (status > 0) ? "accept" : "reject");
    }
    return 0;
}

示例输出(MacOS X 10.6.3):

parent_pid = 15820
i = 0
i = 1
In child process (pid = 15821)
Should be accept
i = 2
In child process (pid = 15822)
Should be reject
In child process (pid = 15823)
Should be accept
Exit status of 15823 was 256 (accept)
Exit status of 15822 was 0 (reject)
Exit status of 15821 was 256 (accept)
于 2010-04-25T14:20:21.513 回答
9

将您的 wait() 函数放在一个循环中并等待所有子进程。如果没有更多子进程可用,则等待函数将返回 -1 并且 errno 将等于 ECHILD。

于 2010-04-25T14:19:35.823 回答
1

出色的示例 Jonathan Leffler,为了使您的代码在 SLES 上运行,我需要添加一个额外的标头以允许 pid_t 对象:)

#include <sys/types.h>
于 2013-02-04T14:11:56.513 回答