-4

first of all i would like to ask you how to create this process tree listed below with using only fork() function.

|____1____
|___2__   |
|_4_   |  |_3_
|   |  |  |   |


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
   if (fork()) {

The thing is I don't know what to do next and how it works.

SNAREGOD
  • 13
  • 6

2 Answers2

0
if(fork()){
    if(fork()){
        if(fork()){
        }
        else{}
    }
    else{}
}
else{
    if(fork()){}
    else{}
    }

Is it good? I worked it out alone...

SNAREGOD
  • 13
  • 6
0

Let's try that with a bit more whitespace and some placeholder comments for you to fill in:

/* initial process */
if (fork()) {
    /* process ? */

    if (fork()) {
        /* process ? */

        if (fork()) {
            /* process ? */

        } else {
            /* process ? */

        }
    } else {
        /* process ? */

    }
} else {
    /* process ? */

    if (fork()) {
        /* process ? */

    } else {
        /* process ? */
    }
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72