Question 1. [4 marks]
Consider the execution of the following function (written in the C language).
// all the needed headers are included
int main()
{
int pid;
if (fork() == 0){
printf(“A”); fflush(stdout);
exit(1);
}
if ((pid = fork()) == 0){
printf("B"); fflush(stdout);
exit(2);
}
printf("C"); fflush(stdout);
printf("D"); fflush(stdout);
waitpid(pid, NULL, 0);
printf("E"); fflush(stdout);
exit(0);
}
1(a) Draw the process graph for the execution of “main()”.
In a process graph, each function, including main(), fork(), printf(), waitpid(), and exit(), is represented by a vertex. You can omit the fflush() function in the process graph. For each vertex, please write the function name below the vertex and for printf() write the output character above the vertex. Each edge must be directed, with the direction representing the happen before relationship.
1(b) List all the feasible output of the program.
For example, if there are two feasible outputs, please give the answer as follows (listing only one feasible output in each line and number them):
(1) A B C D E
(2) E D C B A
Note that the above two outputs are only used to demonstrate the format to give your answers for question 1(b), they do not have any indication to the correct answers
Sample Solution