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
Step
Process
Action
PID
Output
Parent Waits?
1
Main
Start main process
1000
No
2
Main
Fork new process
1000
No
3
Child
Run child block
1001
Child process: PID=1001
No
4
Parent
Print parent info
1000
Parent process: PID=1000, Child PID=1001
No
5
Parent
Wait for child
1000
Yes
6
Child
Finish execution
1001
No
7
Parent
Child finished, continue
1000
No
💡 Parent waits for child to finish, then both processes end.
Variable Tracker
Variable
Start
After fork
After child runs
After parent prints
After wait
Final
pid
nil
1001 (child PID)
1001
1001
1001
1001
Process.pid
1000 (parent)
1001 (child)
1001
1000 (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.
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
Step 1: Understand the purpose of fork
The fork method creates a new process, called the child, which runs alongside the original parent process.
Step 2: Identify the behavior of the child process
The child process runs the code block given to fork independently, allowing parallel execution.
Final Answer:
It creates a new child process that runs concurrently with the parent. -> Option B
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
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.
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.
Final Answer:
child_pid = fork { puts 'Hello from child' } -> Option D
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
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).
Step 2: Determine output order
The child prints first, then the parent prints 'Parent process' after the child ends.
Final Answer:
Child process
Parent process -> Option A
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
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.
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.
Final Answer:
Process.wait called without PID argument -> Option A
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?