Created
May 19, 2013 02:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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