0
0
Rubyprogramming~10 mins

Process forking for parallelism in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a child process using fork.

Ruby
pid = [1]
Drag options to blanks, or click blank then click option'
Afork
Bspawn
Cfork()
Dexec
Attempts:
3 left
💡 Hint
Common Mistakes
Using spawn instead of fork.
Adding parentheses after fork.
2fill in blank
medium

Complete 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'
Anil
B0
C-1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for nil instead of 0.
Using 1 or -1 as the child process ID.
3fill in blank
hard

Fix 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'
Awaitpid
Bwait
Cwaitall
Dwaitchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using Process.wait which waits for any child process.
Using non-existent methods like waitchild.
4fill in blank
hard

Fill 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'
Afork
B0
C1
Dspawn
Attempts:
3 left
💡 Hint
Common Mistakes
Using spawn instead of fork.
Checking if pid == 1 instead of 0.
5fill in blank
hard

Fill 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'
Afork
Bwaitpid
CPID
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using wait instead of waitpid for specific PIDs.
Using incorrect words in the message.