tatsu-zine.com
第12章 孤児プロセス
fork do
5.times do
sleep 1
puts "I'm an orphan!"
end
end
abort "Parent process died..."
第13章 プロセスは優しい
arr = [1, 2, 3]
fork do
p arr
end
arr = [1, 2, 3]
fork do
arr << 4
p arr
end
p arr
- MRI 2.0 以上は CoW フレンドリな作法で実装されているが、1.9以下はそうではない
第14章 プロセスは持てる
fork do
5.times do
sleep 1
puts "I'm an orphan!"
end
end
Process.wait
abort "Parent process died..."
5.times do
fork do
exit 111 if rand(5).even?
exit 112
end
end
5.times do
pid, status = Process.wait2
str = status.exitstatus == 111 ? "even" : "odd"
puts "#{pid} encounterd an #{str} number"
end
- Process.waitpid2 で特定のプロセス指定
favourite = fork do
exit 77
end
middle_child = fork do
abort "I want to be waited on!"
end
pid, status = Process.waitpid2(favourite)
puts status.exitstatus
第15章 ゾンビプロセス
pid = fork { sleep 1 }
puts pid
sleep 5
% ps -ho pid,state -p 57633
PID STAT
57633 Z+
github.com
github.com