Why doesn't die in the alarm handler to kill the process?

From How do I specify a timeout limit for a Perl system call?

eval { 
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required 
    alarm $timeout; 
    $nread = sysread SOCKET, $buffer, $size; 
    alarm 0; 
}; 
if ($@) { 
    die unless $@ eq "alarm\n";   # propagate unexpected errors 
    # timed out 
} 
else { 
    # didn't 
} 

If a timeout occurs, it sub { die "alarm\n" };will end the process. I guess I can’t understand die. This http://www.cs.cf.ac.uk/Dave/PERL/node111.html says that "The die () function is used to exit your script and display a message for reading by the user." However, in the case of the script above, the script will process the code in #timed out. Also sysread continues to work. Instead of sysread, I had a perl script that slept for 30 seconds. My timeout was set to 10 seconds. As expected, the code in #timed out was executed, but the script continued to sleep. All inputs were rated.

+3
source
4

die , .

, , , , .

, .


, , : Windows Perl.

alarm - Unix. ( ) Windows, Windows .

Perl alarm , . sleep , alarm. - .

, sysread, sysread , Perl , - , .

+10

   alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds.

sigalarm , . STDIN sysread, sigalarm .

+1

" sysread perl script, 30 . 10 . , #timed out script .

?

#!/usr/bin/perl
use strict;
use warnings FATAL => qw(all);

eval {
    open my $fh, '<', $0 || die;
    local $SIG{ALRM} = sub {
        print STDERR "hello!\n";
        die "bye!";
    };
    alarm 3;
    while (<$fh>) {
        print $_;
        sleep 1;
    }
    close $fh;
};

if ($@) {
    print "HERE: $@\n";
}

:

#!/usr/bin/perl
use strict;
use warnings FATAL => qw(all);
hello!
HERE: bye! at ./test.pl line 9, <$fh> line 3.

3 ; , "sleep 100" . : , , . "!" , , , , .

+1

Linux Perl script Windows.

...

$recsock = IO::Socket::INET->new(
                             LocalPort => 68,
                             Proto => "udp",
                             Broadcast => 1,
                            Blocking => 0,
                             ) or die "socket: $@";

$continue -

# Timeout handle
$SIG{ALRM} = sub { 
    print "timeout\n";
    $continue = 1;
};

, $ , -:

    alarm($timeout);
     while(1){
         $recsock->recv($newmsg, 1024);
         eval {
            $packet = Net::Package->new($newmsg); 
             ...
        };
        sleep 0.1;
        last if ($continue);
    }
    alarm(0);
0

All Articles