I've made all this work, but every time I send a signal to it to get the current status it kills the current process

Well, not for the code below on my box. I've added/changed some bits.

use IPC::Open3; my $status; sub status () { print "Status is: $status\n"; $SIG{USR1} = \&status; } $SIG{USR1} = \&status; print "$$\n"; my @paths; my $c = 10; push @paths,"sleep ".$c++ for 0 .. 3; $c = 0; foreach $path (@paths) { print "loop ",++$c,"\n"; my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $path); $status = $path; waitpid($pid, 0); print "after waitpid\n"; }

Running it and killing the process several times yields

3297 loop 1 Status is: sleep 10 Status is: sleep 10 Status is: sleep 10 after waitpid loop 2 Status is: sleep 11 Status is: sleep 11 Status is: sleep 11

so yes, waitpid is re-entrant (at least on my box) and sending USR1 doesn't advance the loop and re-open CMD_* (which would kill the child process implicitly). Did you notice the line

print "Status is: $status\n";

above? Do you have unbuffered I/O on STDOUT?

update: yes, some signals get propagated to the children (USR1 isn't among them). Adding these bits to the script

sub default { print "got SIG ".shift()."\n"} $SIG{$_} = \&default for keys %SIG; $SIG{USR1} = \&status;

and invoking a perl script

#!/usr/bin/perl # have to print to a terminal directly, # since default file handles are re-directed open O ,'>', '/dev/pts/1'; sub default { my $sig = shift; print O "child Status: got SIG$sig\n"; $SIG{$sig} = \&default; } $SIG{$_} = \&default for keys %SIG; sleep $_ for 1..shift;

yields, killing the main script with <Ctrl>-C:

3437 loop 1 child Status: got SIGINT got SIG INT got SIG CHLD after waitpid loop 2 ...

You might want to wrap your call to open3() into a subroutine which resets $SIG{USR1} to 'IGNORE' using local. I suspect your problem being elsewhere. BTW, on which platform do you run?


In reply to Re: Signal to parent-process. Does it affect it's children? by shmem
in thread Signal to parent-process. Does it affect it's children? by rapide

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.