How to use signal blocking in Ruby

Viewed 9170

How does one block certain signals in Ruby? (I.e. sigprocmask, as described here: http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_21.html#SEC371)

Here is my example code

pid = fork do
  trap('INT') do
    puts "Graceful shutdown..."
    puts "goodbye"
    exit 0
  end

  loop do
    this_could_be_interrupted
    something_that_must_not_be_interrupted
    this_could_be_interrupted_too
  end
end

sleep 5
Process.kill('INT', pid)

Can I wrap something_that_must_not_be_interrupted in a ruby block or something to ensure that SIGINT won't be process while that method is running?

I guess the ideal would be if I could do something like this:

Process.block_signal('INT') do
  something_that_must_not_be_interrupted
end

UPDATE: I'm currently doing something like this:

trap('INT') do
  @interrupted = true
end

loop do
  exit 0 if @interrupted
  something_that_must_not_be_interrupted
end
2 Answers
Related