Alarm?

Hello,

I see that there is a Signal class in the core for catching signals. Where
would the alarm system call be, for setting a SIGALRM? I can't seem to find
it.

Thanks,
Mike

···

--
Michael P. Soulier <msoulier@digitaltorque.ca>
"Any intelligent fool can make things bigger and more complex... It
takes a touch of genius - and a lot of courage to move in the opposite
direction." --Albert Einstein

% cat alarm.rb
trap("ALRM") do
  puts "Alarm!"
end

Process.kill("ALRM", $$)

% ruby alarm.rb
Alarm!

HTH.

···

On Mon, Sep 25, 2006 at 10:55:19AM +0900, Michael P. Soulier wrote:

Hello,

I see that there is a Signal class in the core for catching signals. Where
would the alarm system call be, for setting a SIGALRM? I can't seem to find
it.

But that doesn't tell the OS to send a SIGALRM at a predefined time. I've
found the timeout library, and implemented what I needed with that, but isn't
the alarm() system call exposed in Ruby? It is in Perl and Python.

Thanks,
Mike

···

On 25/09/06 Logan Capaldo said:

% cat alarm.rb
trap("ALRM") do
  puts "Alarm!"
end

Process.kill("ALRM", $$)

% ruby alarm.rb
Alarm!

--
Michael P. Soulier <msoulier@digitaltorque.ca>
"Any intelligent fool can make things bigger and more complex... It
takes a touch of genius - and a lot of courage to move in the opposite
direction." --Albert Einstein

Oops, sorry misunderstood the question. Is this better?
% cat alarm.rb
require 'dl/import'
module Alarm
  extend DL::Importable
  if RUBY_PLATFORM =~ /darwin/
    so_ext = 'dylib'
  else
    so_ext = 'so'
  end
  dlload "libc.#{so_ext}"
  extern "unsigned int alarm(unsigned int)"
end

trap("ALRM") do
  puts "Alarm!"
  exit
end

Alarm.alarm(3)

loop {}

% ruby alarm.rb
Alarm!

···

On Mon, Sep 25, 2006 at 08:21:44PM +0900, Michael P. Soulier wrote:

But that doesn't tell the OS to send a SIGALRM at a predefined time. I've
found the timeout library, and implemented what I needed with that, but isn't
the alarm() system call exposed in Ruby? It is in Perl and Python.

Thanks,
Mike

Impressive that you could write this so quickly. Still, shouldn't this be in
the standard library? I certainly don't want to do this with all of POSIX. :slight_smile:

Thanks,
Mike

···

On 25/09/06 Logan Capaldo said:

Oops, sorry misunderstood the question. Is this better?
% cat alarm.rb
require 'dl/import'
module Alarm
  extend DL::Importable
  if RUBY_PLATFORM =~ /darwin/
    so_ext = 'dylib'
  else
    so_ext = 'so'
  end
  dlload "libc.#{so_ext}"
  extern "unsigned int alarm(unsigned int)"
end

trap("ALRM") do
  puts "Alarm!"
  exit
end

Alarm.alarm(3)

loop {}

% ruby alarm.rb
Alarm!

--
Michael P. Soulier <msoulier@digitaltorque.ca>
"Any intelligent fool can make things bigger and more complex... It
takes a touch of genius - and a lot of courage to move in the opposite
direction." --Albert Einstein