Search code examples
cprocesstreeforkqnx

How to create this process tree using fork()?


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.


Solution

  • 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 ? */
        }
    }