Created
May 11, 2016 02:01
-
-
Save whitehat101/14b8ccbe789eee5abc5d1abc06065243 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
include Process | |
# puts " ROOT: #{pid} of #{ppid}" | |
# at_exit { puts " EXIT: #{pid} of #{ppid}" } | |
$running = true | |
$workers = [] | |
def start_worker | |
$workers << fork do | |
Signal.trap "INT", "DEFAULT" | |
# puts "START: #{pid} of #{ppid}" | |
sleep 2 + 5*rand | |
end | |
rescue # meh? | |
end | |
Signal.trap("INT") { | |
puts "caught INT; killing" | |
$running = false | |
$workers.each {|pid| kill 'KILL', pid } | |
# kill 0 kills, subprocesses AND this process | |
# use process groups to get better control | |
# kill 'KILL', 0 | |
puts "waitall - #{waitall}" | |
# sleep 2 | |
# exit | |
} | |
Thread.new do | |
loop do | |
puts "#{$workers.length}: #{$workers}" | |
system "ps ho pid,state,cmd -H -p #{pid},#{$workers.join ','}" | |
sleep 0.2 | |
end | |
end | |
begin | |
loop do | |
# puts "MAIN LOOP (#{$workers})" | |
if $running | |
start_worker until $workers.length >= 5 | |
end | |
$workers.delete wait | |
sleep 1 | |
end | |
rescue SystemCallError => e | |
puts "caught SystemCallError\ngg wp" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment