Skip to content

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);  
}
  1. A
    10 and 11
    Correct answer
  2. B
    10
  3. C
    11
  4. D
    11 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.