edited by
10,531 views
12 votes
12 votes

Consider the following code snippet:

if(fork()&& fork())
{
  fork();
    
}
if(fork()||fork())
{
  fork();
  fork();
    
}
printf("GATE 2017");


How many times GATE 2017 printed ?

edited by

3 Answers

Best answer
12 votes
12 votes

// 1 process

if(fork() && fork())  //  creates 2 more  // After first fork() only parent one will execute 2nd fork().
{
fork();    // very first parent process will get here and creates one more process.
}

===== here total process = 4.  ( 1 is new process created by 1st fork and total 3 by parent process)


if(fork() || fork())    // creates 2 more by each 4 process. Here only new child by first fork() will execute 2nd fork.
{

// 2 process reach here (original plus 1st child that tried 2nd fork above) 
fork();   
fork();

// here tota = 8 process inside this "if" body.
}

total process here = 4 * ( 1 + 8) = 36.
printf("GATE 2017");

Ans = 36

Refer the following tree diagram => 

selected by
2 votes
2 votes
I Think 36 Times
edited by
1 votes
1 votes

if (fork() || fork()) ====>in the if loop second fork will not be executed if 1st fork value is not 0 and we have 4 process like that

if the first fork value is zero second fork will be executed 

edited by
Answer:

Related questions

2 votes
2 votes
3 answers
1
Philosophical_Virus asked Dec 10, 2023
768 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
0 votes
0 votes
1 answer
2
Erwin Smith asked Apr 11, 2023
757 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not ...
0 votes
0 votes
2 answers
4
dd asked Sep 15, 2018
1,029 views
The fork system call creates new entries in the open file table for the newly created child process. [True / False][ what is open file table ? ]