ISRO Scientist/Engineer CSE 2017Operating Systems › Not yet assigned to a topic
ISRO Scientist/Engineer CSE 2017 PYQ: Wha is the output of the following program? main() { int a = 10; if(fork()) == 0)) a++; printf("%d\n",a); }
Wha is the output of the following program?
main()
{
int a = 10;
if(fork()) == 0))
a++;
printf("%d\n",a);
}- A10 and 11Correct answer
- B10
- C11
- D11 and 11
Answer
(A) 10 and 11
Approach
The program calls fork(), which creates a child process. The parent receives the child's PID (non-zero) from fork(), so its if condition is false and it prints the original value 10. The child receives 0 from fork(), so its if condition is true, it increments a to 11, and prints 11. Thus the two processes print 10 and 11 in some order.
Reviewed by ISRO IQ Editorial Team.