Question: 1. Draw a process tree for the following code segment. c1 = 0; c2 = 0; c1 = fork(); /* fork number 1 */ if
1. Draw a process tree for the following code segment.
c1 = 0;
c2 = 0;
c1 = fork(); /* fork number 1 */
if (c1 > 0)
c2 = fork(); /* fork number 2 */
fork(); /* fork number 3 */
if (c2 >0)
c2= fork(); /* fork number 4 */
2
#include#include #include #include #include #include int var_global = 0; /* A global variable*/ int main(void) { pid_t forkRet; int var_local = 0; forkRet = fork(); if(forkRet >= 0) // fork was successful { if(forkRet == 0) // child process { var_local++; var_global++; printf(" Child Process :: var_local = %d, var_global = %d my PID = %d ", var_local, var_global, getpid()); } else //Parent process { var_local = 10; var_global = 20; printf(" Parent process :: var_local = %d, var_global = %d my PID = %d", var_local, var_global, getpid()); } } else // fork failed { printf(" Fork failed, quitting!!!!!! "); return 1; } return 0; }
a. When fork() is executed, what is happening with the returning values in parent and in child?
b. After fork() is executed, how many more process is created? What is the PID?
c. Why the values of var_global and var_local are different in parent and child processes respectively?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
