It creates a new child process that runs in parallel with the parent process. The child is a copy of the parent but runs independently.
Click to reveal answer
beginner
How can you tell if you are in the child or parent process after a fork?
The fork method returns nil in the child process and the child's process ID (PID) in the parent process.
Click to reveal answer
intermediate
Why use Process.wait after forking?
It makes the parent process wait for the child process to finish, ensuring the child completes before the parent continues or exits.
Click to reveal answer
beginner
What is a simple example of using fork to run two tasks in parallel?
You can fork to create a child process that runs one task, while the parent runs another. Both run at the same time.
Click to reveal answer
intermediate
What happens if you don't handle the child process after a fork?
The child process may become a 'zombie' process, which wastes system resources until the parent collects its exit status.
Click to reveal answer
What does the fork method return in the parent process?
Anil
BThe child's process ID (PID)
C0
DAn error
✗ Incorrect
In the parent process, fork returns the child's PID, allowing the parent to track the child.
Which method should you use to wait for a child process to finish?
Asleep
Bfork
Cexit
DProcess.wait
✗ Incorrect
Process.wait pauses the parent until the child process ends.
What is the main benefit of using fork in Ruby?
ATo run code in parallel using separate processes
BTo create threads
CTo handle exceptions
DTo read files faster
✗ Incorrect
fork creates a new process so tasks can run at the same time.
What value does fork return in the child process?
A1
Bchild's PID
Cnil
Dparent's PID
✗ Incorrect
In the child process, fork returns nil.
What problem occurs if the parent does not wait for the child process?
AZombie processes may accumulate
BThe child process runs twice
CThe parent crashes immediately
DThe child process never starts
✗ Incorrect
Without waiting, child processes can become zombies, using system resources unnecessarily.
Explain how fork helps run tasks in parallel in Ruby.
Think about how two people can work on different parts of a project at the same time.
You got /4 concepts.
Describe why it is important to use Process.wait after forking.
Imagine waiting for your friend to finish before leaving a party together.
You got /3 concepts.
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?