closed by
9,492 views

4 Answers

Best answer
8 votes
8 votes

// start from parent process consider it 1st process


if(fork()&& fork())  // 2 child process created


{
    fork(); // 1 child process  created

}      // in total 1+2+1= 4 process created  ( among which 1 parent and 3 child process )

from 1 parent process 

if( fork() || fork() )    // 2 child process is created here from above parent process ,

// it is short circuit so in case of OR (||), after evaluation of left operand, right operand will be evaluated only if left operand evaluates to zero. But here left operand gives value 2( 21 = 2 by using the formula {2n -1 }+1 where n =1 ) so right operand is not evaluated.

{

fork(); // 2 child process is created

fork(); // again 2 child process is created

   }  // for each of fork() call in OR (||) 4 child process created , there are 2 fork() so total 4+4 = 8 child process created

  //   so in total ( 2+2) + (2 +2) = 8 [ for each fork() call in if() condition created 4 child process so total 4+4 = 8 child process ]

Total number of process created = 4 * ( 1+8) =  4 * 9 = 36  [add 1 for parent process ] 

so total 36 times "GATE 2017" will be printed .

selected by
7 votes
7 votes

Total $36$ Times print.

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
	if(fork()&& fork()) {
	    fork();
	}
	if(fork()||fork()) {
	    fork();
	    fork();
	}
	printf("GATE 2017");
}
/*
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
GATE 2017
*/
0 votes
0 votes

Sorry for the bad diagram ....but I guess this method is correct 
Correct me if I am Wrong

edited by
Answer:

Related questions

2 votes
2 votes
3 answers
2
Philosophical_Virus asked Dec 10, 2023
882 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
3
Erwin Smith asked Apr 11, 2023
781 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 ...