read

That's a pretty simple thing to do in Ruby.

pids = []  
10.times do |i|  
  pids << fork do
    stop = false
    trap("INT") { stop = true }

    loop do
      s = rand(10) + 1
      puts "#{i} will sleep for #{s} seconds"
      sleep s

      if stop
        puts "#{i} quitting nicely."
        exit
      end
    end
  end
end

# Allow to quit nicely
trap("INT") { pids.each {|pid| Process.detach pid } }

Process.waitall  
Blog Logo

Endel Dreyer

Full-stack developer. Loves Ruby and JavaScript.


Published

Image

bugfixer / @endel

About programming, tools and solving problems.

Back to Overview