lab007 has asked for the wisdom of the Perl Monks concerning the following question:

I am forking a process and need to wait for it to complete, but also need to let the user hit A to abort at any time. My Perl skills are quite limited. Environment: RH 3 (legacy server, with Perl 5.8.0).

I have tried to use an Alarm handler to check for keyboard input every second, so that I can kill the child pid from there if the user presses A, but the problem is that waitpid exits (with -1) the moment the Alarm handler is executed (without any keyboard input, and without executing the kill statement).

Below is a sample to illustrate the issue.

Help would be much appreciated!

#!/usr/bin/perl use strict; use Term::ReadKey; $SIG{ALRM} = "AlarmHandler"; my $childPid = fork(); die unless defined($childPid); if ( ! $childPid ) { # in child sleep 10; exit 0; } # in parent print "\nPlease do xyz (or hit A to abort).\n"; alarm (1); # Check for keystroke every second if (waitpid($childPid, 0) > 0) { my ($rc, $sig, $core) = ($? >> 8, $? & 127, $? & 128); if ($core) { print "PID $childPid dumped core\n"; } elsif ($sig == 9){ print "PID $childPid was killed!\n"; } else { print "PID $childPid returned $rc"; } } else { print "Where did PID $childPid go??"; # When AlarmHandler runs, wa +itpid exits with -1, so gets here. } alarm (0); exit 0; sub AlarmHandler() { open(TTY, "</dev/tty") or die "Failed to open /dev/tty: $!"; my $input = ReadLine -1, *TTY; if ($input ne "") { if ($input =~ /a.*/i) { print "\n\rAborting xyz\n\r"; kill $childPid; } } close(TTY) or die "Failed to close /dev/tty: $!"; alarm (1); # set alarm again }

Replies are listed 'Best First'.
Re: Allowing user to abort waitpid
by BrowserUk (Patriarch) on Mar 07, 2016 at 07:38 UTC

    Instead of using alarm to interrupt the waitpid once a second; use the non-blocking version of waitpid (with WNOHANG) and ReadKey() or ReadLine() from Term::ReadKey with a timeout to check for user input.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      That works great! Thanks!
Re: Allowing user to abort waitpid
by Athanasius (Archbishop) on Mar 07, 2016 at 06:30 UTC

    Hello lab007,

    Have you taken into account this caveat in the documentation for Perl’s built-in alarm function?

    It is usually a mistake to intermix alarm and sleep calls, because sleep may be internally implemented on your system with alarm.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      So armed with that information, change the sleep to  system $^X, '-e', 'sleep 9';

      Just an unfortunate choice for my sample - I am not actually using sleep there in the production code.
Re: Allowing user to abort waitpid
by salva (Canon) on Mar 07, 2016 at 07:33 UTC
    Just call waitpid again repeatly until it returns a non error code.

    Also, the way to call kill is kill $signal, $process;!

Re: Allowing user to abort waitpid
by Anonymous Monk on Mar 07, 2016 at 08:28 UTC
    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"; }
      Oh, I forgot to add: Control-C (or 'A') will send the signal to your child too. If you install the handler after fork, the child will get killed, which, it seems, is what you want? But if you don't want that, set $SIG{INT} to 'IGNORE' before fork, and after fork install your handler (it should probably be an empty sub, as in $SIG{INT} = sub {};)
        Moving the child to its own process group (setpgrp(0,0)) would also avoid getting the INT signal when the user presses a Control-c.

      This (waitpid returning) may be the case with OP's legacy environment.

      Current perl appears to set up the signal handlers with SA_RESTART. In this case, the (wait4) syscall returns ERESTARTSYS and is automatically restarted by the libc wrapper.

      #! /usr/bin/perl use strict; use warnings; $SIG{QUIT} = "IGNORE"; my $pid = fork() // die; unless ($pid) { exit !exec "sleep 100"; } $SIG{ALRM} = sub { warn "SIGALRM\n" }; $SIG{QUIT} = sub { warn "SIGQUIT\n" }; warn "childpid = $pid\n"; alarm(2); warn "interrupted\n" while waitpid(-1, 0) == -1 and $!{EINTR};

      Your advice is sound, however. Periodic polling is a hackish workaround. Unix has interrupts (^C ^\), and where possible, one ought to go with those. The alternative it to handle SIGCHLD and just keep reading/selecting on STDIN.

        This (waitpid returning) may be the case with OP's legacy environment... Current perl appears to set up the signal handlers with SA_RESTART
        Wow, anonymonk... I didn't know that. You're right (well, almost). It's not SA_RESTART, it's
        PP(pp_waitpid) { ... if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) result = wait4pid(pid, &argflags, optype); else { while ((result = wait4pid(pid, &argflags, optype)) == -1 && er +rno == EINTR) { PERL_ASYNC_CHECK(); } } ...
        (pp.c)

        There is some mention in perlipc: On systems that supported it, older versions of Perl used the SA_RESTART flag when installing %SIG handlers. This meant that restartable system calls would continue rather than returning when a signal arrived. In order to deliver deferred signals promptly, Perl 5.8.0 and later do not use SA_RESTART. Consequently, restartable system calls can fail (with $! set to "EINTR") in places where they previously would have succeeded. The default ":perlio" layer retries "read", "write" and "close" as described above; interrupted "wait" and "waitpid" calls will always be retried.

        Well, I stand corrected, then.

        nixers are a funny bunch.

        Ask about how to multiplex a few hundred tcp clients transferring gobs of data, and they'll almost universally suggest using a select loop, or other polled event mechanism; which in modern high-speed comms environments requires polling with millisecond or smaller resolution to be responsive to even tens of clients. And that can consume 60% to 70% of a cpu just polling.

        But ask about getting conditional input from the guy sitting at the keyboard, which requires polling no more than once every 1/10th of a second, which will consume so little cpu that it won't even show; and they call it hackish.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.