2.1k views

Consider the following code snippet:

if(fork()&& fork())
{
fork();

}
if(fork()||fork())
{
fork();
fork();

}
printf("GATE 2017");

How many times GATE 2017 printed ?

edited | 2.1k views
0
Is the answer 36 ?
0
i am not getting 24 even without one fork in second if loop.

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

by Boss (26.3k points)
selected by
0
@Arjun sir, @Kapilp, can you please check it ??
0
hi,i am getting only 32 calls by your method,from where are you getting extra 4??why have you included 1 in bracket??
0
if(fork() || fork() ) // creates2 more process and out of 3, only 2 goes into if body.

first fork creates one more child and that only executes 2nd fork and creats one more process.

now ,  (child of first fork )|| (child of 2nd fork) does not enter into if body.

Parent process of first fork does not execute 2nd fork because if candition is already true for parent.

hope it clears everything.
0
i have got it,actually i ws not calculating the four remaning prcesses which did nt go into second if loop

thanks for help
+5

yes 36, @vijay chk

0
@vijay,kapil why only c4 ,c8 exeecute last fork from second of last level why not all
+1
@asu,

if(A or B)
{

print(hello);

}

here if A is true then we don't need to check B and will print hello.

And if A is false ( or returning 0) then only we will check ( or execute ) B. And now if B returns non-zero then only hello will be printed and if B is again returning 0 then it will not print hello.

If you understood this part, then use same logic for parent(returns non zero) and child(returns 0) process after a fork.

if A

if(fork()||fork())

// here possible combinations are - {1 ( parent of first fork),} {after child of first fork. ( 0, 1) , (0, 0) }
{
// 2 process will be here 1. parent of first fork  2. Child from first fork and parent from 2nd fork.
fork();
fork();
}
0
yes....thanku very much
0

vijaycs how can i use this logic with fork

0
+1 vote
I Think 36 Times
by Active (4.5k points)
edited
0
the answer is 24,i dun know  how..if anyone can explain
0
Is the answer 28?

OR

The code is

if(fork()||fork()){
fork();
fork();

printf("GATE 2017");   //print inside the loop??

}
+1
Leaving last fork , total 20 processes are there

and after executing last fork, total 36 processes will be there.
0
no,orintf is outside lopp only,and answer given is 24
0
Can Someone tell the exact method to solve this problem
+1 vote

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

by Boss (12.5k points)
edited
0
thanks for help,i have got it now