4,340 views

How many times ‘4’ will be printed ?

int main()
{
if (fork() && (!fork())) {
if (fork() || fork()) {
fork();
}
}
printf("4 ");
return 0;
}

How to approach these kind of questions using Tree structure ??

I too not getting , how 7.

$4$ is printed by $p,c2,c4,c3,c6,c5,c1$

Firstly , fork will be called on p1 and it will return one parent p1  and one child c1 process . parent will contain some +ve value (let it be 1) and child will contain 0 .

Now there is and in between two forks and the property of and is if the first value will be 1 then only we will execute other else we will not execute other , so as p1 is 1 so we will call fork for only p1 not for  c1... And as a result p1 will give two process parent(p1)  and child (c2) .

Now as its not(fork) so c2 will return 1 and p1 will return 0 .and so c2 wil continue on next line as it will return 1

Now c2 will give two process parent (c2) and child (c3) and as it is or so if the first condition is true we will not execute 2nd condition , so c2 will dirctly go to next line and c3 as it returned 0 will execute 2nd condition fork .

so finally we will have 7 4's printed

awesome explaination
THANX  for detail explaination .

fork-1()

print(4)                 fork-2()

fork-3()                print(4)

fork-4()                  fork-5()

print(4)       fork-5()     print(4)       print(4)

print(4)     print(4)

Go through given code top-down, left-right and label your fork calls in increasing order

I haven't drawn arrows, but you can see the tree structure(ONLY FORK calls are internal nodes, print statements are leaf and hence have no descendants. As you already know FORK gives two values

0(to child) - LEFT SUBTREE
PID(positive value to parent) - RIGHT SUBTREE

@Anuj Mishra

Can u draw clearly? I am not getting how only 7 four will be printed

edited

let us change the given code as follows:

int main()
{
if (fork1() && (fork2()==0)) {
if (fork3() || fork4()) {
fork5();
}
}
printf("4 ");
return 0;
}

Let id be the return value of fork() function call.

By 4 i mean 4 is printed..

"p--fork1" means p called fork1()..

Here c1 won't call fork2 because, fork1() returns 0 for c1 and no need to call fork2() as it is already false(short circuited evaluation..).

Also c2 won't call fork4() because, fork3() return id>0. So no need to call fork4() as it is already true..

Hope this clears...

@chirudeepnamini Here, it should be mentioned that fork2() will not run for c1 because the first condition was false(logical operator is AND therefore no need to check 2nd condition).

And similarly for c2 fork4() will not run because first condition was TRUE (as the logical operator is OR therefore it no need not check 2nd condition i.e. fork4() )

@nadeshseen thank you.. edited now :)

Guys Fork concept is pretty easy.The only concept is when a process execute a fork call it will create another child process with all the value initialized as the previous value same as parent process and the pc(program counter) for both process (child and  parent) will point to next instruction.From the next instruction both the process (child and  parent) execute independently.At the time of fork parent receives pid (process id) and child process receives zero .

Here in this question along with concept of fork we have 'and' and 'or' operator.For 'and' operator if first value is true then only it will check for next value else it will not check for next value.For or it will check for both value because if atleast one is true then whole condition is true.

Here in this question 'and' operator will return true only for c2. 'or' operator will return true for c2 and c3 hence this two process will go inside the 'if ' condition and execute fork again. The number of times 4 will be printed is number of process created that is 7 times  .I hope i have explained enough rest all is self explanatory from solution image.

This is my way of answering such questions . Feel free to ask questions.

1 vote