Consider the program below include include int counter 0 in

Consider the program below:

#include <stdio.h>

#include <unistd.h>

int counter = 0;

int main()

{

int i;

for (i=0; i<2; i++) {

   fork();

   counter++;

   printf(\"counter = %d\ \", counter);

}

printf(\"counter = %d\ \", counter); /* LINE Y */

return 0;

}

Please answer the questions 3a-3c below. In one line for each part, explain the reason why.




3a. What is the total number of lines that will be printed and why?


____________________________________________________________________________



3b. What is printed in the first line and why?


_____________________________________________________________________________

  

3c. What is printed in the last line (LINE Y) and why?

Solution

output:
counter = 1
counter = 2
counter = 2
counter = 1
counter = 2
counter = 2
counter = 2
counter = 2
counter = 2
counter = 2

3a) Total lines printed are 10 due to the various child process created by thw fork command during the for loop
3b) first line printed is the counter value in the for loop in the child process created by the fork command. counter = 1
3c) Last line will be counter = 2 printed by the parent process as all the child process are finished executing and parent process will fininsh last.

Consider the program below: #include <stdio.h> #include <unistd.h> int counter = 0; int main() { int i; for (i=0; i<2; i++) { fork(); counter++;
Consider the program below: #include <stdio.h> #include <unistd.h> int counter = 0; int main() { int i; for (i=0; i<2; i++) { fork(); counter++;

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site