compile fork.c
gcc fork.c -o fork.out
Run the compiled code
./fork.out
The c program asks the user the number of times it should fork. After every fork the parent waits for the child to complete using wait system call and whenever a leaf fork child completes execution all the nodes in that path from the parent resume execution from bottom up and write to the
output.txt
file in the format PARENT parent_pid CHILD child_pid
this does not cause race condition because of the wait call.
Then the python file
tree_generator.py
is invoked which parses data in output.txt
and creates a dictionary where keys are pids of parents and value is list of pids of child processes, this list is then used to create a tree and the tree is dumped as a json object to tree.json
file which contains node values as the pid of every process.
The sample
tree.json
contains results for 7 consecutive fork calls.