GATE / CS ARCHIVE

GATE CSE 2012 / QUESTION 8

Children created by repeated fork

Operating Systems · Processes, Threads & System Calls · Full GATE CSE 2012 paper

GATE 2012, Q8 1 mark · MCQ
Report issue

Processes, Threads & System Calls

Question 8, GATE 2012: A process executes the code
                fork();
                fork();
                fork();
        The total number of child processes created is
        (A) 3                   (B) 4                      (C) 7                  (D) 8
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (9 steps)

Assuming each fork succeeds, the first invocation creates one child, the second is executed by two processes and creates two more, and the third by four processes creates four more. There are 1+2+4=7 children, or eight total processes including the original. Hence C.

Check: Track the live population after each call as 1→2→4→8. Every new process is a child of an existing process, but the original process is not its own child, so subtracting it from the final population avoids distractor D.

Original paper, page 3 · Answer key