Last active
August 29, 2015 13:57
-
-
Save unclebilly/9629558 to your computer and use it in GitHub Desktop.
Handle the USR1 signal by printing backtraces
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
#!/usr/bin/env ruby | |
puts "PID: #{$$}" | |
trap("USR1") do | |
@dump = true | |
end | |
t = Thread.new do | |
loop do | |
print '<' | |
sleep 1 | |
end | |
end | |
f = Thread.new do | |
loop do | |
f = rand(10) + rand(10) + rand(10) + rand(10) | |
sleep 0.003 | |
end | |
end | |
printer = Thread.new do | |
loop do | |
if @dump | |
Thread.list.each do |t| | |
puts t.inspect | |
puts t.backtrace | |
end | |
@dump = false | |
else | |
sleep 0.5 | |
end | |
end | |
end | |
printer.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool! Signal handling FTW!