public
Last active

threads across fork Ruby demo

  • Download Gist
threads-across-fork.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/usr/bin/env ruby
 
# Pass 'fork' as the first argument to use Process.fork, otherwise Process.daemon is used.
 
do_fork = (ARGV[0] == 'fork')
 
thread = Thread.new do
loop { sleep(1) }
end
 
puts "using Process.#{do_fork ? 'fork' : 'daemon'}"
puts "before pid = #{$$}, thread.alive? = #{thread.alive?.inspect}"
 
if do_fork
pid = Process.fork
if pid.nil?
puts "after in child, pid = #{$$}, thread.alive? = #{thread.alive?.inspect}"
else
puts "after in parent, pid = #{$$}, thread.alive? = #{thread.alive?.inspect}"
end
else
Process.daemon(true, true)
puts "after, pid = #{$$}, thread.alive? = #{thread.alive?.inspect}"
exit(0)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.