Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a child process using fork.
Ruby
pid = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
spawn instead of fork.Adding parentheses after
fork.✗ Incorrect
In Ruby, fork creates a child process and returns its process ID.
2fill in blank
mediumComplete the code to check if the current process is the child process.
Ruby
if pid == [1] puts "Child process" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for
nil instead of 0.Using 1 or -1 as the child process ID.
✗ Incorrect
In Ruby, fork returns 0 in the child process.
3fill in blank
hardFix the error in the code to wait for the child process to finish.
Ruby
Process.[1](pid) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
Process.wait which waits for any child process.Using non-existent methods like
waitchild.✗ Incorrect
The correct method to wait for a specific child process is Process.waitpid.
4fill in blank
hardFill both blanks to create two child processes and print their PIDs.
Ruby
2.times do pid = [1] if pid == [2] puts "Child PID: #{Process.pid}" exit end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
spawn instead of fork.Checking if
pid == 1 instead of 0.✗ Incorrect
Use fork to create child processes and check if pid == 0 to identify the child.
5fill in blank
hardFill all three blanks to collect child PIDs and wait for them to finish.
Ruby
pids = [] 3.times do pids << [1] end pids.each do |pid| Process.[2](pid) puts "Child process [3] finished" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
wait instead of waitpid for specific PIDs.Using incorrect words in the message.
✗ Incorrect
Use fork to create child processes, waitpid to wait for each, and print a message when done.