#include #include #include int main () { char str[100]; int i; setbuf(stdout,0); fork(); for (i=0; i<2; i++){ if (fork()!=0) { sprintf (str, "echo system with i=%d", i); system (str); } else { sprintf (str, "exec with i=%d", i); execlp ("echo", "myPgrm", str, NULL); } } return (0); } The process generation tree for the program is the following: P1 / | \ P2 P3 P4 / \ P5 P6 Where P1 is the process that starts the execution of the program, P2 is the process created by the first fork (the one outside the for cycle). Processes P3 and P4 are created by P1 inside the for cycle. Both processes execute the echo command and terminate (in fact, the execlp system call completely substitutes the code segment of the calling process with a new code segment). In turn, the process P2 creates two new processes, P5 and P6, inside the for cycle. In the same way as P3 and P4, also P5 and P6 execute the echo command and terminate. The output of the program will therefore consist of: - The print of "echo system with i=0" executed by the process P1 by calling the system during the first iteration in the cycle. - The print of "echo system with i=1" executed by the process P1 by calling the system during the second iteration in the cycle. - The print of "echo system with i=0" executed by the process P2 by calling the system during the first iteration in the cycle. - The print of "echo system with i=1" executed by the process P2 by calling the system during the second iteration in the cycle. - The print of "echo exec with i=0" executed by the process P3 by calling the execlp during the first iteration in the cycle. - The print of "echo exec with i=1" executed by the process P4 by calling the execlp during the second iteration in the cycle. - The print of "echo exec with i=0" executed by the process P5 by calling the execlp during the first iteration in the cycle. - The print of "echo exec with i=1" executed by the process P6 by calling the execlp during the second iteration in the cycle. The order in which these eight lines can appear in the output it is non-deterministic and depends on how the processes are scheduled by the operating system. A possible output is the following: system with i=0 system with i=0 system with i=1 system with i=1 exec with i=0 exec with i=1 exec with i=1 exec with i=0