the problem is that waitpid exits (with -1) the moment the Alarm handler is executedThat's not a problem, it's a feature :) If you install a handler for a signal, and the handler gets invoked during interruptible system call (waitpid is interruptible), the system call fails with errno ($! in Perl) set to EINTR.
So you can install a handler for SIGINT, and abort waiting by pressing Control-C instead of 'A'. Then check if $! equals POSIX::EINTR.
If you must use 'A' rather than Control-C, you can use POSIX::Termios to change the interrupt character. Something like that (maybe there is a better way, but it should at least give you some food for thought):use strict; use warnings; use POSIX (); my $new_interrupt_char = 'A'; my $seconds_to_sleep = 5; if ( POSIX::isatty( POSIX::STDIN_FILENO ) ) { my $term = POSIX::Termios->new(); $term->getattr( POSIX::STDIN_FILENO ); my $old_interrupt_char = $term->getcc( POSIX::VINTR ); $term->setcc( POSIX::VINTR, ord $new_interrupt_char ); $term->setattr( POSIX::STDIN_FILENO ); END { # restore the interrupt char to default (Control-C) if ( defined $term ) { $term->setcc( POSIX::VINTR, $old_interrupt_char ); $term->setattr( POSIX::STDIN_FILENO ); } } } $SIG{INT} = sub { print "SIGINT handler called\n"; }; my $slept = sleep $seconds_to_sleep; # use waitpid instead of sleep if ( $slept != $seconds_to_sleep and $! == POSIX::EINTR ) { print "Sleep was interrupted, slept $slept secs\n"; }
In reply to Re: Allowing user to abort waitpid
by Anonymous Monk
in thread Allowing user to abort waitpid
by lab007
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |