Bird
0
0

Identify the error in this Ruby code using fork:

medium📝 Debug Q14 of 15
Ruby - Concurrent Programming
Identify the error in this Ruby code using fork:
pid = fork do
  puts 'Child running'
end
puts 'Parent running'
Process.wait
AProcess.wait called without PID argument
BMissing block for fork
Cfork does not return a PID
Dputs statements are inside the fork block
Step-by-Step Solution
Solution:
  1. Step 1: Check usage of Process.wait

    The Process.wait method should be called with the child's PID to wait for that specific process.
  2. Step 2: Identify the error

    In the code, Process.wait is called without any argument, which waits for any child process but can cause unexpected behavior or errors if multiple children exist.
  3. Final Answer:

    Process.wait called without PID argument -> Option A
  4. Quick Check:

    Always pass PID to Process.wait = A [OK]
Quick Trick: Pass child PID to Process.wait to avoid errors [OK]
Common Mistakes:
  • Calling Process.wait without PID
  • Confusing fork return value
  • Misplacing puts statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes