fork() and wait() with two child processes

后端 未结 3 443

I need to use the fork() and wait() functions to complete an assignment. We are modelling non-deterministic behaviour and need the program to fork() if there is more than on

相关标签:
3条回答
  • 2020-12-02 11:53

    brilliant example Jonathan Leffler, to make your code work on SLES, I needed to add an additional header to allow the pid_t object :)

    #include <sys/types.h>
    
    0 讨论(0)
  • 2020-12-02 12:02

    It looks to me as though the basic problem is that you have one wait() call rather than a loop that waits until there are no more children. You also only wait if the last fork() is successful rather than if at least one fork() is successful.

    You should only use _exit() if you don't want normal cleanup operations - such as flushing open file streams including stdout. There are occasions to use _exit(); this is not one of them. (In this example, you could also, of course, simply have the children return instead of calling exit() directly because returning from main() is equivalent to exiting with the returned status. However, most often you would be doing the forking and so on in a function other than main(), and then exit() is often appropriate.)


    Hacked, simplified version of your code that gives the diagnostics I'd want. Note that your for loop skipped the first element of the array (mine doesn't).

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

    Example output (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)
    
    0 讨论(0)
  • 2020-12-02 12:08

    Put your wait() function in a loop and wait for all the child processes. The wait function will return -1 and errno will be equal to ECHILD if no more child processes are available.

    0 讨论(0)
提交回复
热议问题