Skip to content

@benweint /threads-across-fork.rb
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
threads across fork Ruby demo
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.