Skip to content

Instantly share code, notes, and snippets.

@morhekil
Created May 19, 2013 02:54
Show Gist options
  • Save morhekil/5606500 to your computer and use it in GitHub Desktop.
Save morhekil/5606500 to your computer and use it in GitHub Desktop.
Ruby threads and processes fun. Creating new thread, then forking off a child inside it and waiting for the child to finish. It works when using fork with a block argument, and it works when not creating a thread. But it doesn't work (hangs) when creating a thread and using fork with if/else syntax.
t = Thread.new do
pid = fork do
puts "I'm the child #{Process.pid}"
raise 'hello'
end
puts "Thread #{Thread.current.inspect}, pid: #{pid.inspect}"
puts "I'm the parent #{Process.pid}"
Process.waitpid(pid)
end
t.join
# t = Thread.new do
pid = fork
puts "Thread #{Thread.current.inspect}, pid: #{pid.inspect}"
if pid
puts "I'm the parent #{Process.pid}"
Process.waitpid(pid)
else
puts "I'm the child #{Process.pid}"
raise 'hello'
end
# end
# t.join
t = Thread.new do
pid = fork
puts "Thread #{Thread.current.inspect}, pid: #{pid.inspect}"
if pid
puts "I'm the parent #{Process.pid}"
Process.waitpid(pid)
else
puts "I'm the child #{Process.pid}"
raise 'hello'
end
end
t.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment