How to create unix process binary trees?

前端 未结 1 737
不思量自难忘°
不思量自难忘° 2021-01-07 01:14

Can some one help me out here, not necessarily complete my homework.

I need to create a process tree using fork(); in Unix/C so far the best I can get to to the 4th

相关标签:
1条回答
  • 2021-01-07 02:03

    The waitpid() function requires arguments.

    The initial childpid = fork(); is unexpected.

    You can sensibly write:

    for (int i = 0; i < 1; i++)
    

    for the loops unless you insist on using C89 instead of C99 syntax.

    Attempt 2

    Example output

    Start 46480
    i am 46480, my parent is 30775
    i am 46482, my parent is 46480
    i am 46481, my parent is 46480
    i am 46483, my parent is 46481
    i am 46484, my parent is 46482
    i am 46486, my parent is 46482
    i am 46487, my parent is 46483
    Exit 46487
    i am 46488, my parent is 46484
    Exit 46488
    i am 46485, my parent is 46481
    i am 46491, my parent is 46484
    Exit 46491
    i am 46489, my parent is 46483
    Exit 46489
    Exit 46483
    i am 46492, my parent is 46486
    Exit 46492
    Exit 46484
    i am 46490, my parent is 46486
    Exit 46490
    i am 46493, my parent is 46485
    Exit 46493
    Exit 46486
    Exit 46482
    i am 46494, my parent is 46485
    Exit 46494
    Exit 46485
    Exit 46481
    Exit 46480
    

    Example source

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    static pid_t fork_ok(void)
    {
        pid_t pid;
        if ((pid = fork()) < 0)
        {
            fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
            exit(1);
        }
        return pid;
    }
    
    int main(void)
    {
        fprintf(stderr, "Start %d\n", (int)getpid());
    
        for (int level = 0; level < 3; level++)
        {
            if (fork_ok() == 0 || fork_ok() == 0)
                continue;
            break;
        }
        fprintf(stderr, "i am %d, my parent is %d\n",
                (int)getpid(), (int)getppid());
        while (wait(0) > 0)
            ;
        fprintf(stderr, "Exit %d\n", (int)getpid());
        return(0);
    }
    

    This uses a loop, a somewhat unusual loop. At each level, the parent process forks two children; the children each 'continue' to the next level, while the parent is done and exits the loop. The clean up code is the same as before (see below).

    Recursive variant

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    static pid_t fork_ok(void)
    {
        pid_t pid;
        if ((pid = fork()) < 0)
        {
            fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
            exit(1);
        }
        return pid;
    }
    
    static void new_level(int level)
    {
        if (level > 3)
            return;
        if (fork_ok() == 0 || fork_ok() == 0)
            new_level(level+1);
        else
        {
            printf("i am %d, my parent is %d\n",
                    (int)getpid(), (int)getppid());
            while (wait(0) > 0)
                ;
            printf("Exit %d\n", (int)getpid());
        }
    }
    
    int main(void)
    {
        printf("Start %d\n", (int)getpid());
        fflush(0);
        new_level(0);
        return(0);
    }
    

    Attempt 1

    I'm not convinced you need any loops. This seemed to do the trick for me:

    Example output

    Start 44397
    i am 44397, my parent is 30775
    i am 44400, my parent is 44397
    i am 44401, my parent is 44397
    Exit 44401
    i am 44399, my parent is 44397
    i am 44398, my parent is 44397
    i am 44404, my parent is 44400
    Exit 44404
    i am 44403, my parent is 44399
    i am 44402, my parent is 44398
    i am 44405, my parent is 44398
    i am 44407, my parent is 44398
    Exit 44407
    Exit 44400
    i am 44406, my parent is 44399
    Exit 44406
    i am 44408, my parent is 44402
    i am 44410, my parent is 44402
    i am 44411, my parent is 44405
    Exit 44410
    Exit 44411
    i am 44409, my parent is 44403
    Exit 44409
    Exit 44405
    i am 44412, my parent is 44408
    Exit 44412
    Exit 44403
    Exit 44399
    Exit 44408
    Exit 44402
    Exit 44398
    Exit 44397
    

    Source Code

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    int main(void)
    {
        fprintf(stderr, "Start %d\n", (int)getpid());
        if (fork() < 0 || fork() < 0 || fork() < 0 || fork() < 0)
        {
            fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
            exit(1);
        }
        fprintf(stderr, "i am %d, my parent is %d\n",
                (int)getpid(), (int)getppid());
        while (wait(0) > 0)
            ;
        fprintf(stderr, "Exit %d\n", (int)getpid());
        return(0);
    }
    
    0 讨论(0)
提交回复
热议问题