Process structure

Hello can anyone give me some tips how to create this kind of structure of process with recursion:
http://i.stack.imgur.com/uUnVi.png
I made this structure using fork() and pstree but for now all I am doing is bunch of if statements. Since I am reading input like 1 5 0 3 for the picture above I was wondering how could I make this use recursion. Any tips?
Anyone?
Im not sure what your question is? Reword your question so it makes some sort of sense.
1 5 0 3 input means that the main process has one child. This one child has 5 children and out of those 5 children the first one (and 3. 4. 5.) doesn't have any and second has 3 children. The image shows how the input should look after those proceses run. I need some help how to construct that tree recursively so I can draw any type of input. For now I can only create this picture using if statements. But this only works for this very case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>


int main() {

pid_t pid;
pid_t temppid;
pid_t temppid2;
int  root_pid;
int status;

root_pid = getpid();

pid = fork(); // creates a child from root
if (pid == 0) { // if child
    pid = fork(); // fork again (child#1)
    if (pid != 0) { // if not child of child#1
        temppid = getpid(); // get pid
        if (getpid() == temppid) { // create child#2
            pid = fork();
            if (pid == 0) {
                temppid2 = getpid();
                if (getpid() == temppid2) { // create child#1
                    fork();
                }
                if (getpid() == temppid2) { // create child#2
                    fork();
                }
                if (getpid() == temppid2) { // create child#3
                    fork();
                }
            }
        }
        if (getpid() == temppid) { // create child#3
            fork();
        }
        if (getpid() == temppid) { // create child#4
            fork();
        }
        if (getpid() == temppid) { // create child#5
            fork();
        }
    }
}
else {
    // create another child from root
    pid = fork();
        if (pid == 0) {
            // run pstree in this child with pid from root
        }
}

while (1) {
    sleep(1);
}
}
Topic archived. No new replies allowed.