Question: write in C program Understand the fork() about its return values, variables, and processing Instruction : Attached is a lab.c file containing a fork() statement.
write in C program
Understand the fork() about its return values, variables, and processing
Instruction:
Attached is a lab.c file containing a fork() statement. Compile and run it. Explain the output of this program.
Thing to submit:
Submit a word file to explain the following questions:
When fork() is executed, what is happening with the returning values in parent and in child?
After fork() is executed, how many more process is created? What is the PID?
Why the values of var_global and var_local are different in parent and child processes respectively?
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; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
