CS FundamentalsHardGATE CS (PYQ pattern)

A C program runs: int i; for(i=0;i<4;i++){ if(fork()==0 && i%2==0) break; } printf("X"); Every process, including the original, reaches the printf exactly once. Assuming all fork calls succeed, how many X characters are printed in total?

  1. A. 7
  2. B. 5
  3. C. 8
  4. D. 16

Correct answer: A. 7

Let f(i) be processes reaching printf from loop index i: f(4)=1, f(3)=f(4)+f(4)=2 (odd i: the child continues), f(2)=f(3)+1=3 (even i: the child breaks), f(1)=2·f(2)=6, f(0)=f(1)+1=7.

More Operating Systems questions

Exams that ask this topic