in Operating System closed by
9,294 views
11 votes
11 votes
closed as a duplicate of: fork system call

Consider the following code snippet:
 

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


How many times GATE $2017$ printed?

How to solve this using tree diagram?

in Operating System closed by
by
9.3k views

4 Answers

8 votes
8 votes
Best answer

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

4 Comments

@bikram sir thank you so much sir for easy explanation.....

0
0

@bikram sir please explain

if(fork()||fork()) this in more details. i mean how 2 child process is created here.Please explain the logic.

thanks sir,

0
0
fork() returns the processid of the child for the caller, and 0 for the created child process. Short circuit rule works on the basis of this.
1
1
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
*/
by

4 Comments

how does first and consecutive forks within if loop are generating 2 child processes plz explain? 2^(1) will be total  processes but not child processes
0
0
sir,why only 6 child created....why not 7 child
0
0

@Debashish Deka amazing solution sir.

0
0
1 vote
1 vote

Answer: 36 times

edited by
0 votes
0 votes

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

edited by

4 Comments

Tried my best to make it clear...
But I used simple logic like  
1st fork = 2 process gets created..
2nd Fork = only gets exwcuted when 1st fork get 1 and we know tha child retruns 0 so we dont create process for child... we create only for parent...
3rd fork ---> gets executed only we get 1 from both fork and only for that process we create two more process..
and So on I created the tree.......
0
0
My solution plz...have look
0
0
for if(fork() || fork())

{

fork();

fork();

}

here, i am getting total 11 processes can anyone explain with help of diagram without tree one
0
0
Answer:

Related questions