in reply to SIGALRM and gethostbyaddr

Safe signals. Signals will only be delivered between opcodes. gethostbyaddr is one opcode - Perl defers the querying to the C library, which might go off to the network to perform a query. This may take a long time, and all that time, the signal will be delayed. Only after the result comes back, Perl will deliver the signal.

With the most recent versions of 5.8, one can set an environment variable that gives you back the old behaviour.

Abigail

Replies are listed 'Best First'.
Re: Re: SIGALRM and gethostbyaddr
by chip (Curate) on Mar 18, 2004 at 01:40 UTC
    it's also possible to set an unsafe signal with POSIX::sigaction, without switching the entire Perl program to unsafe signals. (Recent sigactions will actually let you set a signal safe or unsafe.)

    It's worth noting, BTW, that unsafe signals are called "unsafe" for a reason. What you really need is a C signal handler in an XS or with Inline::C that can just return without causing even the slightest disturbance to Perl's interpreter state.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Here is the new code using use Sys::SigAction. Thanks!!!
      use Sys::SigAction qw( set_sig_handler ); .... eval { my $code = sub { die "alarm clock restart"; }; my $h = Sys::SigAction::set_sig_handler( 'ALRM', $code, { +flags => 0, safe => 1 } ); alarm 10; # schedule alarm for every 10 seconds eval { ($my_name, $my_aliases, $addrtype, $my_length, @my_add +r) = gethostbyname($my_hostname); }; alarm 0; }; alarm 0; die if $@ && $@ !~ /alarm clock restart/; #reraise
      thanks to both of you... I'm doing mass lookups of about 4,000 ip addresses and a tiny amount don't resolve within a reasonable amount of time.. there are a tiny number that need to be looked up on other nameservers...