Return to Operationg Systems home
Represent the control flow graph (CFG) and the process generation tree of the following pieces of code. In addition, indicate the output produced on the video. Check the results you predicted, by executing the program.
A.
for (i=1; i<=2; i++) { if (!fork ()) printf ("%d\n", i); } printf ("%d\n", i);
B.
for(i=3; i>1; i--) { if (fork ()) printf ("%d\n", i); } printf ("%d\n", i);
C.
for (i=0; i<2; i++) { if (fork ()) fork (); } printf ("%d\n", i);
D.
for (i=2; i>=1; i--) { if (!fork ()) printf ("%d\n", -i); else printf ("%d\n", i); }
A program receives two integer values on the command line, called n
and t
. The program (parent process) must produce 2 children and terminate. In turn, each child must produce 2 children and terminate. This sequence of operations must continue until 2 ^ n
processes on the leaves of the tree are produced/running. The leaf processes wait for t
seconds and display (on screen) a termination message. Note that each process (in the tree) produces two other processes. Only those on the leaves of the tree sleep and display a message. What is the order of termination of the processes? Is it always the same? How can they be recognized (ppid)?
Once the program has been executed:
Using the system calls fork and wait, realize the following precedence graph.
P1 / \ / \ P2 P3 /\ /\ / \ / \ P4 P5 | P6 \ / \ / \/ \/ P7 P8 \ / \ / P9
Note that all arcs are oriented downwards. Each Pi process corresponds to a print message (print the string “Pi” and the PID of the process).
Check the precedence are respected by inserting the system call sleep
in the various branches of the program.