Bird
0
0

How can you modify this code to ensure the parent process waits for all child processes before continuing?

hard📝 Application Q9 of 15
Ruby - Concurrent Programming
How can you modify this code to ensure the parent process waits for all child processes before continuing?
3.times do
  fork { puts 'Child' }
end
puts 'Parent'
AAdd Process.wait after the loop without arguments
BStore PIDs in an array and call Process.wait(pid) for each
CCall exit inside each fork block
DReplace fork with Thread.new
Step-by-Step Solution
Solution:
  1. Step 1: Identify missing wait for children

    Without waiting, parent prints immediately without waiting for children.
  2. Step 2: Collect child PIDs and wait for each

    Store PIDs from fork calls and wait for each to ensure synchronization.
  3. Final Answer:

    Store PIDs in an array and call Process.wait(pid) for each -> Option B
  4. Quick Check:

    Wait all children by tracking PIDs [OK]
Quick Trick: Track child PIDs to wait for all before parent continues [OK]
Common Mistakes:
  • Assuming Process.wait without PID waits all children
  • Thinking exit in child affects parent wait
  • Confusing threads with processes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes