GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Count descendants from unconditional fork rounds

3 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Every extant process executes each effective fork round, so process population doubles and newly created children exclude the original. 2008/2012 use three unconditional calls;

1 mark · NAT
Report issue

Counting child processes across repeated forks

Processes, Threads & System Calls

Question 17, GATE 2019: A Unix/Linux program runs for(i=0; i<10; i++), calling fork() whenever i%2==0, then returns. Assuming all forks succeed, find the total number of newly created child processes, excluding the original process.
Tap to enlarge
Answer & solution

Official answer: 31

Step through it (33 steps)

Method: count the branching rounds; every process alive at a fork round executes that fork and then continues the same loop.

1. The loop values are 0 through 9. The even values 0,2,4,6,8 cause five fork rounds; odd iterations create no processes.
2. A successful fork creates one new child from each currently executing process. Both parent and child continue from immediately after the fork with their own copy of i, so both participate in later rounds.
3. Starting from one process, the total after successive rounds is 2,4,8,16,32. The numbers of new children produced at those rounds are respectively 1,2,4,8,16.
4. Adding the created children gives 1+2+4+8+16 = 31. Equivalently, subtract the original from the final total: 25−1 = 31.

Result: 31 child processes in total, including descendants created by children. Counting only the five calls made by the original parent would omit later generations. The result assumes no fork failure or external process termination.

Original paper, page 7 · Answer key

1 mark · MCQ
Report issue

Children created by repeated fork

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

2 marks · MCQ
Report issue

Process count after repeated fork

Processes, Threads & System Calls

Question 66, GATE 2008: A process executes for(i=0;i<n;i++) fork();. How many child processes are created in total, assuming all forks succeed?

(A) n
(B) 2^n−1
(C) 2^n
(D) 2^(n+1)−1
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (3 cases)

1. Both parent and new child continue the loop after each successful fork. Hence each iteration doubles the number of processes still executing.
2. From one process, n iterations yield 2n total processes. Equivalently the iterations create 1+2+⋯+2n−1 children.
3. Subtract the one original process: 2n−1, B. Counting only n forks by the original parent misses descendants’ forks.

Original paper, page 13