edited by
8,727 views
13 votes
13 votes

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

edited by

4 Answers

9 votes
9 votes

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 

3 votes
3 votes

                                           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

 

 

 

2 votes
2 votes

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.

solved sum img

 

Related questions

2 votes
2 votes
3 answers
3
Philosophical_Virus asked Dec 10, 2023
765 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
3 votes
3 votes
1 answer
4
24aaaa23 asked Oct 1, 2023
719 views
Consider the following code segment :int main (void) { for (int i=2; i<=5; i++) { fork( ); }printf (“GOCLASSES”);How many times “GOCLASSES” is printed by the ab...