📖 Scenario: You have a list of tasks that take some time to complete. To finish faster, you want to run some tasks at the same time using Ruby's process forking.
🎯 Goal: Build a Ruby program that uses fork to run two tasks in parallel and then waits for both to finish before printing a message.
📋 What You'll Learn
Create an array called tasks with two strings: 'Task 1' and 'Task 2'.
Create an empty array called pids to store process IDs.
Use a for loop with variable task to iterate over tasks.
Inside the loop, use fork to create a child process that prints "Starting: #{task}", sleeps for 2 seconds, then prints "Finished: #{task}".
Store each child process ID in pids.
After the loop, use Process.wait(pid) for each pid in pids to wait for all child processes to finish.
Finally, print "All tasks completed.".
💡 Why This Matters
🌍 Real World
Running multiple tasks at the same time can save time in programs that do many independent jobs, like downloading files or processing data.
💼 Career
Understanding process forking is useful for backend developers and system programmers who need to optimize performance by running tasks in parallel.
Progress0 / 4 steps
1
Create the tasks array
Create an array called tasks with these exact strings: 'Task 1' and 'Task 2'.
Ruby
Hint
Use square brackets [] to create an array with two strings.
2
Create the pids array
Create an empty array called pids to store process IDs.
Ruby
Hint
Use [] to create an empty array.
3
Fork processes for each task
Use a for loop with variable task to iterate over tasks. Inside the loop, use fork to create a child process that prints "Starting: #{task}", sleeps for 2 seconds, then prints "Finished: #{task}". Store each child process ID in pids.
Ruby
Hint
Use fork do ... end to create a child process. Use pids << pid to add the process ID to the array.
4
Wait for all child processes and print completion
After the loop, use Process.wait(pid) for each pid in pids to wait for all child processes to finish. Then print "All tasks completed.".
Ruby
Hint
Use Process.wait(pid) to wait for each child process. Then print the final message.
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?