Question: Understand the fork() regarding its returning values, global/local variables declared in the parent's process, and processing Attached is a lab.c file containing a fork() statement.
Understand the fork() regarding its returning values, global/local variables declared in the parent's process, and processing
Attached is a lab.c file containing a fork() statement. Compile and run it. Questions below
-Lab.c
#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; } Questions:
1) When fork() is executed, what is happening to the returning values in parent and in child?
2) After fork() is executed, how many more process is created? What is the PID?
3) 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
