Created
July 15, 2010 13:25
-
-
Save sandro/476933 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Simple test to see how subprocesses handle signals. | |
# The result is that the terminal sends the INTERRUPT signal to both the | |
# main process and subprocesses. | |
# If you use the `kill` command on the main process' pid, subprocesses don't | |
# respond. | |
def forking | |
Signal.trap('INT') { puts "INT Trapped"; exit } | |
5.times do | |
fork do | |
sleep 10 | |
end | |
end | |
Process.waitall | |
end | |
def execing | |
Signal.trap('INT') { puts "INT Trapped"; exit } | |
fork do | |
exec %(ruby -e "sleep 10; puts 'done'") | |
end | |
Process.waitall | |
end | |
def exec_with_handler | |
Signal.trap('INT') { puts "INT Trapped"; exit } | |
fork do | |
exec %(ruby -e "Signal.trap('INT') { puts 'INT Trapped'; exit }; sleep 10; puts 'done'") | |
end | |
Process.waitall | |
end | |
send(ARGV[0]) | |
__END__ | |
Results: | |
$ ruby signals_and_subprocesses forking | |
^CINT Trapped | |
INT Trapped | |
INT Trapped | |
INT Trapped | |
INT Trapped | |
$ ruby signals_and_subprocesses execing | |
^CINT Trapped | |
-e:1:in `sleep': Interrupt | |
from -e:1 | |
$ ruby signals_and_subprocesses exec_with_handler | |
^CINT Trapped | |
INT Trapped |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment