-3
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

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Usr1
  • 369
  • 1
  • 6
  • 15

1 Answers1

0

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.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222