int main () {
fork ();
fork ();
fork ();
pf (" hi\n");
}
How many times the "hi" will print? What is sequence of execution for fork() ? os:linux compiler:gcc
int main () {
fork ();
fork ();
fork ();
pf (" hi\n");
}
How many times the "hi" will print? What is sequence of execution for fork() ? os:linux compiler:gcc
Question 1: How many times the "hi" will print?
Due to first Fork
|
-------------------------
| |
Due to second fork Due to second fork
| |
---------------------- ----------------------------
| | | |
Due to 3rd fork Due to 3rd fork Due to 3rd fork Due to 3rd fork
| | | |
------------------ --------------- ---------- ------------
| | | | | | | |
process-1 process-2 process-3 process-4 pr-5 pr-6 process-7 process-8
So
if all fork
goes well then total 8 process created so there will be 8 or lesser hi will be there in output
Question 2: What is sequence of execution for fork() ? os:linux compiler:gcc
There is no any guarantee which process will be scheduled first and which one second. Its all depends on Scheduler.