the problem is that waitpid exits (with -1) the moment the Alarm handler is executed
That'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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.