require 'drb/drb'
main_pid = Process.pid
f_proc_id = fork do
puts "Started server fork with pid: #{Process.pid}, gpid: #{Process.getpgid(main_pid)}"
class Front
def initialize
@global = [1,2,3,4]
end
def pop
@global.pop
end
end
f = Front.new
DRb.start_service 'drbunix:/tmp/drb000', f
DRb.thread.join
end
puts 'preparing to fork'
until File.exist? '/tmp/drb000'
sleep 1
end
puts 'server ready'
pids = []
(1..3).each do
pids << fork do
puts "Hello from fork pid: #{Process.pid}"
r = DRbObject.new_with_uri 'drbunix:/tmp/drb000'
puts r.pop
end
end
# hold your breath until all children finish
until pids.all? { |p|
!system("ps --no-headers -p #{p} > /dev/null 2>&1") ||
system("ps --no-headers -p #{p} | grep defunct> /dev/null 2>&1")
}
sleep 1
end
puts 'shutting down main fork'
Process.kill 'HUP', f_proc_id
puts 'done'