Bird
Raised Fist0
Rubyprogramming~20 mins

Process forking for parallelism in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fork Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of simple forked process

What will this Ruby code print?

Ruby
pid = fork do
  puts "Child process"
end
Process.wait(pid)
puts "Parent process"
AChild process
BChild process\nParent process
CParent process\nChild process
DParent process
Attempts:
2 left
💡 Hint

Remember the parent waits for the child to finish before printing.

❓ Predict Output
intermediate
2:00remaining
Variable value after fork

What is the value of x printed by this Ruby code?

Ruby
x = 10
pid = fork do
  x = 20
  puts x
end
Process.wait(pid)
puts x
A10\n20
B20\n20
C20\n10
D10\n10
Attempts:
2 left
💡 Hint

Child and parent have separate memory after fork.

🔧 Debug
advanced
2:00remaining
Why does this forked code hang?

What is wrong with this Ruby code?

Ruby
pid = fork do
  puts "Child"
end
puts "Parent"
AParent does not wait for child, so child may still run after parent exits
BFork block syntax is incorrect, causing infinite loop
CMissing <code>Process.wait(pid)</code> causes parent to hang
DChild process never prints because of missing <code>puts</code>
Attempts:
2 left
💡 Hint

Think about what happens if the parent exits before the child finishes.

❓ Predict Output
advanced
2:00remaining
Number of processes created

How many processes will be created and print "Hello" in this Ruby code?

Ruby
3.times do
  fork do
    puts "Hello"
  end
end
Process.waitall
A4
B1
C0
D3
Attempts:
2 left
💡 Hint

Each fork creates one child process.

❓ Predict Output
expert
3:00remaining
Output of nested forks with variable increments

What is the output of this Ruby code?

Ruby
x = 0
pid1 = fork do
  x += 1
  pid2 = fork do
    x += 1
    puts x
  end
  Process.wait(pid2)
  puts x
end
Process.wait(pid1)
puts x
A2\n1\n0
B1\n2\n0
C0\n1\n2
D2\n2\n2
Attempts:
2 left
💡 Hint

Remember each fork creates a new process with its own copy of variables.

Practice

(1/5)
1. What does the fork method do in Ruby?
easy
A. It pauses the current process until a condition is met.
B. It creates a new child process that runs concurrently with the parent.
C. It merges two processes into one.
D. It terminates the current process immediately.

Solution

  1. Step 1: Understand the purpose of fork

    The fork method creates a new process, called the child, which runs alongside the original parent process.
  2. Step 2: Identify the behavior of the child process

    The child process runs the code block given to fork independently, allowing parallel execution.
  3. Final Answer:

    It creates a new child process that runs concurrently with the parent. -> Option B
  4. Quick Check:

    fork creates child process = A [OK]
Hint: Fork means start a new process running alongside parent [OK]
Common Mistakes:
  • Thinking fork pauses the process
  • Confusing fork with process termination
  • Assuming fork merges processes
2. Which of the following is the correct syntax to create a child process using fork in Ruby?
easy
A. child_pid = fork puts 'Hello from child'
B. fork = child_pid { puts 'Hello from child' }
C. fork child_pid { puts 'Hello from child' }
D. child_pid = fork { puts 'Hello from child' }

Solution

  1. Step 1: Recall the correct fork syntax

    The fork method takes a block with curly braces or do-end to run code in the child process.
  2. Step 2: Check each option's syntax

    child_pid = fork { puts 'Hello from child' } correctly assigns the child process ID and uses a block with curly braces. Options A, C, and D have syntax errors or incorrect usage.
  3. Final Answer:

    child_pid = fork { puts 'Hello from child' } -> Option D
  4. Quick Check:

    Use block with fork = B [OK]
Hint: Fork needs a block with braces or do-end [OK]
Common Mistakes:
  • Omitting braces for the block
  • Assigning fork incorrectly
  • Using wrong order of arguments
3. What will be the output of this Ruby code?
pid = fork do
  puts 'Child process'
end
Process.wait(pid)
puts 'Parent process'
medium
A. Child process Parent process
B. Child process
C. Parent process Child process
D. Parent process

Solution

  1. Step 1: Understand the fork and wait behavior

    The fork creates a child process that prints 'Child process'. The parent waits for the child to finish using Process.wait(pid).
  2. Step 2: Determine output order

    The child prints first, then the parent prints 'Parent process' after the child ends.
  3. Final Answer:

    Child process Parent process -> Option A
  4. Quick Check:

    Child prints before parent after wait = C [OK]
Hint: Parent waits, so child prints first [OK]
Common Mistakes:
  • Assuming parent prints first
  • Ignoring Process.wait effect
  • Confusing output order
4. Identify the error in this Ruby code using fork:
pid = fork do
  puts 'Child running'
end
puts 'Parent running'
Process.wait
medium
A. Process.wait called without PID argument
B. Missing block for fork
C. fork does not return a PID
D. puts statements are inside the fork block

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]
Hint: Pass child PID to Process.wait to avoid errors [OK]
Common Mistakes:
  • Calling Process.wait without PID
  • Confusing fork return value
  • Misplacing puts statements
5. You want to run two tasks in parallel using fork and wait for both to finish before continuing. Which code correctly achieves this?
hard
A. pids = fork { task1; task2 } Process.wait(pids) puts 'Both done'
B. fork { task1 } fork { task2 } puts 'Both done'
C. pids = [] pids << fork { task1 } pids << fork { task2 } pids.each { |pid| Process.wait(pid) } puts 'Both done'
D. fork { task1; Process.wait } fork { task2; Process.wait } puts 'Both done'

Solution

  1. Step 1: Understand how to fork multiple processes

    Each fork creates a child process. To run two tasks in parallel, fork twice and store both child PIDs.
  2. Step 2: Wait for both child processes

    Use Process.wait(pid) for each PID to ensure both finish before continuing.
  3. Step 3: Analyze options

    pids = [] pids << fork { task1 } pids << fork { task2 } pids.each { |pid| Process.wait(pid) } puts 'Both done' correctly forks twice, stores PIDs, waits for both, then prints. Others either don't wait or misuse fork.
  4. Final Answer:

    pids = [] pids << fork { task1 } pids << fork { task2 } pids.each { |pid| Process.wait(pid) } puts 'Both done' -> Option C
  5. Quick Check:

    Fork twice, wait both PIDs = D [OK]
Hint: Store PIDs, wait each to sync parallel tasks [OK]
Common Mistakes:
  • Not waiting for child processes
  • Calling Process.wait inside child
  • Forking once for multiple tasks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard