I have a C file that looks like this:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
printf ("The PID is %d\n", (int) getpid ());
child_pid = fork ();
if (child_pid != 0)
{
printf ("this is the parent process, with PID %d\n",
(int)getpid());
printf ("the child PID is %d\n", (int) child_pid);
}
else
printf ("this is the child process, with PID %d\n",
(int)getpid());
return 0;
}
I need to modify it to create a hierarchy that looks like
parent (0)
|
+
|
+
|
+
|
+
|
+
|
Basically a tree structure, where every second child creates two new children. As far as I understand, when I am a fork()process, each process will be launched simultaneously. Adding fork()to the statement ifseems to work and correctly creates processes 0 through 2, since only the parent will create a new fork. But I have no idea how to make process 2 a fork, not 1. Any ideas?
source
share