0
0
Rubyprogramming~10 mins

Process forking for parallelism in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Process forking for parallelism
Start main process
Fork new process
Child process
Child runs code
Child finishes
Processes end
The main process creates a child process by forking. Both run code independently. The parent can wait for the child or continue.
Execution Sample
Ruby
pid = fork do
  puts "Child process: PID=#{Process.pid}"
end
puts "Parent process: PID=#{Process.pid}, Child PID=#{pid}"
Process.wait(pid)
This Ruby code forks a child process that prints its PID. The parent prints its PID and the child's PID, then waits for the child to finish.
Execution Table
StepProcessActionPIDOutputParent Waits?
1MainStart main process1000No
2MainFork new process1000No
3ChildRun child block1001Child process: PID=1001No
4ParentPrint parent info1000Parent process: PID=1000, Child PID=1001No
5ParentWait for child1000Yes
6ChildFinish execution1001No
7ParentChild finished, continue1000No
💡 Parent waits for child to finish, then both processes end.
Variable Tracker
VariableStartAfter forkAfter child runsAfter parent printsAfter waitFinal
pidnil1001 (child PID)1001100110011001
Process.pid1000 (parent)1001 (child)10011000 (parent)1000 (parent)1000 (parent)
Key Moments - 3 Insights
Why does the child process print a different PID than the parent?
Because after fork, the child is a new process with its own unique PID, shown in execution_table row 3 and variable_tracker Process.pid changes.
What does the 'pid' variable hold in the parent and child after fork?
In the parent, 'pid' holds the child's PID (1001), but in the child, 'pid' is nil or not used. See execution_table rows 2 and 3 and variable_tracker 'pid' values.
Why does the parent call Process.wait(pid)?
To pause and wait for the child process to finish before continuing, ensuring orderly execution. This is shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the PID of the child process?
A1001
B1000
Cnil
D2000
💡 Hint
Check the 'PID' column in execution_table row 3 where the child runs.
At which step does the parent wait for the child process to finish?
AStep 3
BStep 5
CStep 2
DStep 7
💡 Hint
Look at the 'Parent Waits?' column in execution_table to find when waiting happens.
If the parent did not call Process.wait(pid), what would change in the execution?
AParent would wait longer
BChild would not run
CParent might finish before child
DPID values would swap
💡 Hint
Refer to execution_table row 5 and the role of Process.wait in synchronizing processes.
Concept Snapshot
fork do ... end creates a child process running the block.
Parent gets child's PID from fork return.
Both run independently after fork.
Parent can wait for child with Process.wait(pid).
Child has a unique PID different from parent.
Use fork for parallelism in Ruby.
Full Transcript
This example shows how Ruby uses fork to create a child process. The main process starts with PID 1000. When fork is called, a new child process with PID 1001 is created. The child runs the block printing its PID. The parent prints its own PID and the child's PID, then waits for the child to finish using Process.wait. Variables like 'pid' hold the child's PID in the parent. The child and parent run independently after fork, enabling parallelism.