987 views
0 votes
0 votes
Consider the following program segment of C-programming language:

#include<stdio.h>

 

int main() {

 

if (fork() || fork())

 

fork();

 

printf("GATE\n");

 

return 0;

 

}

 

Number of times the above program prints 'GATE' is_?

3 Answers

2 votes
2 votes

@jayadev

The answer is 5 ( 1 1 1 1 1 )...…

 

1. It will create two process one parent P (has process ID of child process)and other is child C1 (process ID = 0)....

2. In if statement we used OR operator( || ) and in this case second condition is evaluated when first condition is false....

3. Parent process P will return positive integer so it directly execute statement and create two more processes (one parent P and other is child C2)…

Child process C1 will return 0 so it checks for second condition and second condition again create two more processes(one parent C1 and other is child C3)…

 

4. C1 return positive integer so it will further create two more processes (one parent C1 and other is child C4). Child C3 return 0 so it will directly print 1…

 

0 votes
0 votes
p

                                 (>0) p--------------------------------c(=0)

                     (>0)p----------------c1(=0)-----------------c(>0)-----------------c2(=0)

                     print(gate)       print(gate)        c(>0)-----------c3(=0)     print(gate)  

                                                                  print(gate)      print(gate)  

5 times gate will be printed

Related questions

0 votes
0 votes
1 answer
1
jayadev asked Dec 7, 2021
440 views
Why the operating system has the knowledge about kernel level threads but not about user level threads?
1 votes
1 votes
1 answer
2
jayadev asked Dec 6, 2021
384 views
Does kernel level threads have separate PCB(process control block)?
9 votes
9 votes
2 answers
3
junaid ahmad asked Jul 13, 2017
852 views
if(fork() && fork()) {fork();}if(fork()||fork()) {fork();fork();}printf("XYZ");How many time XYZ gets printed ?
11 votes
11 votes
4 answers
4
junaid ahmad asked Jul 13, 2017
1,031 views
int main() { if(fork() == 0) printf("GATE2018"); if(fork() == 0) printf("GATE2018"); }How many times GATE2018 printed?