CS FundamentalsModerateGATE CS (PYQ pattern)

A parent creates a pipe, forks, and the child loops on read(fd[0], buf, 1) until it returns 0. The parent writes 3 bytes into fd[1] and then calls wait() without closing either end of the pipe (neither does the child). What happens?

  1. A. The child reads the 3 bytes, gets 0 and exits, so wait() returns
  2. B. The child reads the 3 bytes and then blocks forever, so the parent also blocks in wait()
  3. C. The child gets 0 immediately because nothing is buffered yet
  4. D. The write fails with SIGPIPE because the child has the read end open

Correct answer: B. The child reads the 3 bytes and then blocks forever, so the parent also blocks in wait()

read returns 0 (EOF) only when every write end is closed. The parent still holds fd[1] (and the child inherited a copy), so after the data is consumed the read blocks; the parent's wait() then never returns: a classic deadlock.

More Operating Systems questions

Exams that ask this topic