Given a process tree
A
/ \
/ \
B C
/ \ / \
D E F G
I am asked to print the sequence in BFS order, i.e A-B-C-D-E-F-G using fork() system call, where each node represents a process with the same parent-child construct as shown in tree(i.e A is parent of B and C, B is parent of D and E like that).
I figured out this solution, but I really don't understand how to make it print recursively.
static int count;
char *msg[] = {"A", "B", "C", "D", "E", "F", "G"};
main(){
if(!fork()){ //Child 1
printf("%s\t\t%d\t\t%d\n", msg[count++], (int)getpid(), (int)getppid());
}
else{
if(!fork()){ //Child 2
printf("%s\t\t%d\t\t%d\n", msg[count++], (int)getpid(), (int)getppid());
}
}
}
This logic prints only A-B-C, How to make it recursive so that it will print till G? Please help. Thanks.
Following code does what you need but does not guarantee which leaf will be printed first (print order of the same level).