Question: ( 4 0 points ) Simple C programs on fork ( ) . Suppose all printf ( ) will be followed by fflush ( stdout
points Simple C programs on fork Suppose all printf will be
followed by fflushstdout even if it is not presented in the code. For all the
C programs, you can assume that necessary header files are included
points Consider the following code segments:
int main
pidt pid;
pidt pid;
pid fork;
pid fork;
printfpid:d pid:d
pid pid;
a How many processes are there in total when this code finishes?
Give the answer.
b If one process prints "pid: pid: one process prints
"pid: pid: write down other processes' pid and their
outputs.
points Consider the following code segments:
int main
int i ;
int cnt ;
for ; i; i
pidt pid fork;
if pid
printfd
cnt;
else
cnt ;
return ;
a Who will print the cnt the child process or parent process? Why?
How many times of "printf" will this code execute? Give the answer
with explanation.
b Theoretically, based on Linux fork mechanism introduced in the
lecture, how many memory copies of the variable cnt will there
be Explain your answer.
c Directly running this piece of code may produce different outputs
every time. Briefly explain why.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
