Process forking lets a program do many things at the same time by creating copies of itself. This helps finish tasks faster.
Process forking for parallelism in Ruby
pid = fork do # code to run in the child process end Process.wait(pid) # wait for the child process to finish
The fork method creates a new child process.
The block after fork runs only in the child process.
pid = fork do puts "Child process running" end Process.wait(pid) puts "Parent process finished"
pid = fork do # Child does nothing end Process.wait(pid) puts "Child done"
pid = fork do puts "Child start" sleep 2 puts "Child end" end puts "Parent continues" Process.wait(pid)
This program shows the parent process creating a child process. The child prints messages and sleeps for 1 second to simulate work. The parent waits for the child to finish before exiting.
puts "Parent process ID: #{Process.pid}" pid = fork do puts "Child process ID: #{Process.pid}" puts "Child is working..." sleep 1 puts "Child finished work" end puts "Parent is waiting for child to finish" Process.wait(pid) puts "Child process has finished. Parent exiting."
Time complexity depends on the tasks inside the child process, but forking itself is fast.
Space complexity increases because the child process is a copy of the parent.
Common mistake: forgetting to wait for child processes, which can cause zombie processes.
Use forking when tasks are independent and can run at the same time. Use threads if tasks share memory and need communication.
Forking creates a new process to run code in parallel.
The child process runs the block given to fork.
The parent can wait for the child to finish using Process.wait.
Practice
fork method do in Ruby?Solution
Step 1: Understand the purpose of
Theforkforkmethod 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 toforkindependently, allowing parallel execution.Final Answer:
It creates a new child process that runs concurrently with the parent. -> Option BQuick Check:
forkcreates child process = A [OK]
- Thinking fork pauses the process
- Confusing fork with process termination
- Assuming fork merges processes
fork in Ruby?Solution
Step 1: Recall the correct
Theforksyntaxforkmethod 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 DQuick Check:
Use block with fork = B [OK]
- Omitting braces for the block
- Assigning fork incorrectly
- Using wrong order of arguments
pid = fork do puts 'Child process' end Process.wait(pid) puts 'Parent process'
Solution
Step 1: Understand the fork and wait behavior
Theforkcreates a child process that prints 'Child process'. The parent waits for the child to finish usingProcess.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 AQuick Check:
Child prints before parent after wait = C [OK]
- Assuming parent prints first
- Ignoring Process.wait effect
- Confusing output order
fork:
pid = fork do puts 'Child running' end puts 'Parent running' Process.wait
Solution
Step 1: Check usage of Process.wait
TheProcess.waitmethod should be called with the child's PID to wait for that specific process.Step 2: Identify the error
In the code,Process.waitis 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 AQuick Check:
Always pass PID to Process.wait = A [OK]
- Calling Process.wait without PID
- Confusing fork return value
- Misplacing puts statements
fork and wait for both to finish before continuing. Which code correctly achieves this?Solution
Step 1: Understand how to fork multiple processes
Eachforkcreates a child process. To run two tasks in parallel, fork twice and store both child PIDs.Step 2: Wait for both child processes
UseProcess.wait(pid)for each PID to ensure both finish before continuing.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.Final Answer:
pids = [] pids << fork { task1 } pids << fork { task2 } pids.each { |pid| Process.wait(pid) } puts 'Both done' -> Option CQuick Check:
Fork twice, wait both PIDs = D [OK]
- Not waiting for child processes
- Calling Process.wait inside child
- Forking once for multiple tasks
Ready to prove it?
Pass the test and this pattern moves unknown
