CS FundamentalsVery hardGATE CS · Product-company placement

f.txt does not exist at the start. Program X: fd = open("f.txt", O_WRONLY|O_CREAT, 0644); then fork(); the child writes "cd" and exits; the parent wait()s and then writes "ab" to fd. Program Y is the same except that the open() call is made separately by the child (before its write) and by the parent (after wait()). What does f.txt contain after each program?

  1. A. X: "ab"; Y: "ab"
  2. B. X: "cdab"; Y: "cdab"
  3. C. X: "cdab"; Y: "ab"
  4. D. X: "abcd"; Y: "ab"

Correct answer: C. X: "cdab"; Y: "ab"

In X the child inherits the same open-file description, so the file offset is shared: the child's write moves it to 2 and the parent appends, giving cdab. In Y each open() creates its own offset starting at 0, so the parent overwrites cd with ab.

More Operating Systems questions

Exams that ask this topic