How many times fork() function is executed

Main()
{
if(fork()||fork())
return fork();
else return 0;
}

how many times fork() function is executed?

Questions by poojashetty

Showing Answers 1 - 7 of 7 Answers

There are so many answer to ur question. The number of execution depends on  is the fork function is returning 0 or not. actually scheduler will decide what to return.
 If the first fork in if condition returns non zero value, then  executes 2 times.
 If the first fork in if condition returns zero then It will execute next fork in the if condition only. If that also returns zero, then 2 times only.
if the second fork returns one then if statement will execute. so it is 3 times.

Finally I could conclude, we can't tell how many times it executes fork call.

  Was this answer useful?  Yes

geekeeg

  • Jun 30th, 2010
 

4 times.

Main thread
fork()
         fork()     /* In Child process second fork call */
                       end  /* Child of child, returns */
         fork()   /* Child process, return fork() */
                       end /* Child of child, returns */
fork() /* Main thread, return fork() . . .further no susequent calls */
fork()

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions