1,206 views
7 votes
7 votes
{
    if(fork()&& fork()) {
    fork();
}
if(fork()||fork()) {
}
printf("GATE\n");
 

How many times GATE will be printed? and also the explanation

4 Answers

Best answer
4 votes
4 votes

This code will print "GATE" 4 times...
#include <stdio.h>
#include <sys/types.h>
int main(){
    if(fork() && fork()) fork();
    //if(fork()||fork());
    
printf("GATE\n");
}

Why?
the first fork will create a new child thread... since fork() will return 0 for the child thread, it will jump to the print statement - first "GATE"
the main thread, on the other hand, will execute the second fork().. a child thread will be created which again will jump to the print statement - second "GATE"...
the main thread (being a stubborn one) will execute the third fork() (as it is now inside the if block).. a child thread will be created...both the main thread and child thread will execute the print statement - third and fourth "GATE".. 
 


and this code will print "GATE" 3 times...
#include <stdio.h>
#include <sys/types.h>
int main(){
    //if(fork() && fork()) fork();
    if(fork()||fork());
    
printf("GATE\n");
}

Why?
the first fork will create a new child thread... let us call this thread A...
since fork() will return a +ve value for the main thread, it will jump to the print statement - first "GATE"
thread A, on the other hand, will execute the second fork().. a new child thread will be created...let's call this thread B...
thread A will jump to the print statement - second "GATE"...
thread B will execute the print statement too - third "GATE".. 


since each thread from the first part will get to execute the second part..
Ans = 4 x 3 = 12

selected by
1 votes
1 votes

Point-:1.if(a&&b)  in this case if a=0 then b would not be checked 2.if(a||b) here if a=1 then be would not be checked...

  if(     fork()&& fork()    ) { will generate two process} 

                                                                          1.(0&&fork())                 2.(1&&fork())

                                                                                               |                                           /          \

                                                                Level P:  if(fork()||fork())       < <-<-<- (1&&0)    (1&&1)

                                                                                   /         \                                                /  fork()    \

                                                                       (0||fork())   (1||fork())                      Level P:        Level:P                               

                                                                           /      \                  |                                                               

                                                                (0||0)     (0||1)      GATE

                                                               GATE     GATE

3+3+3+3=12..here < <-<-< represent that start from Level P:...

reshown by
1 votes
1 votes

Interesting question though I am not sure whether it is worth while to spend a lot of time in in this kind of questions in the exam:

fork returns 0 to the child and a non zero to the parent, for this question we can assume that is always 1.

Lets number the lines of the code for easy reference:

1 if(fork()&& fork()) {
2    fork();
}
3 if(fork()||fork()) {
    // There should be a fork here, to make things interesting
    // What the hell we are going to put one here as the main aim is to understand 
    // the concept
4    fork();
}
5 printf("GATE\n");

After the first fork call the code will be like:

\\Parent
if(1 && fork()){
    \\ rest same
    
\\Child
if(0 && fork()){
    \\ rest same

And after the second fork call it will become:

\\Parent
if(1 && 1){
    \\ rest same

\Child 1  
if(1 && 0){
    \\ rest same
    
\\Child 2
if(0 && 1){
    \\ rest same

\\Child 3
if(0 && 1){
    \\ rest same

So we have 4 processes after line number 1.

The fork at line 2 will be executed only once (by original parent). Now there are 5 processes in total.

At line number 3, after the first fork the situation will like the same as earlier, instead we now have 10 processes, 5 with 1 and 5 with 0 return values.

These 5 with 1 return value will definitely call both of the remianing forks and hence there number will be 20.

The remaining 5, will call second fork at line 3 and make the count 10, 5 with 0 and 5 with 1. These 5 will only call the fork at line 4 and hence total count will be 5+10 = 15

Adding this 15 to the 20 from above, makes the total 35 and hence the line 5 will be printed 35 times. 

If we ignore the fork at line 4, as given in the question then the answer will be 20 

Related questions

1 votes
1 votes
2 answers
1
Warrior asked Nov 10, 2018
1,318 views
0 votes
0 votes
1 answer
3
Erwin Smith asked Apr 11, 2023
757 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 ...
0 votes
0 votes
0 answers
4