develooper Front page | perl.perl5.porters | Postings from April 2010

Re: [PATCH] Attempt at improving the perlipc docs

Thread Previous | Thread Next
From:
Eric Brine
Date:
April 19, 2010 11:26
Subject:
Re: [PATCH] Attempt at improving the perlipc docs
Message ID:
z2nf86994701004191126z993811ect4ce23164111207c2@mail.gmail.com
On Mon, Apr 19, 2010 at 1:38 PM, Leon Timmermans <fawaka@gmail.com> wrote:

> On Mon, Apr 19, 2010 at 7:02 PM, Maik Hentsche <maik@mm-double.de> wrote:
> > Hi,
> > I was recently confused by the way signal handlers using wait() behave
> > in collaboration with qx() or system().
> >
> > When you have a signal handler for SIGCHLD that calls wait() and use
> > qx() or system() in your parent, the signal handler will be called.
> > Yet, because qx/system already wait()ed for their child the wait in the
> > signal handler would block. I doubt everyone will understand this in
> > the first place. Thus, I wrote an improvement for the documentation to
> > tell people about this issue. Please find the patch attached. It
> > applies to the current git HEAD.
> >
>
> Mentioning a problem is a good start, but what I'm missing is how to
> deal with this. That is far more valuable (and probably more
> complicated to do in a portable manner).
>

Solution with a race condition:

my %handlers;

$SIG{CHLD} = sub {
    my $pid = wait();
    return if $pid == -1;  # ...
    return if !defined($handlers{$pid}));
    my $fh = delete($handlers{$pid});
    print($fh $?);
    close($fh);
};

sub pseudo_system {
    my $pid = open3('<&STDIN', '>&STDOUT', '>&STDER', @_);
    pipe(my $fh_r, my $fh_w) or die "pipe: $!";
    $handlers{$pid} = $fh_w;
    $? = <$fh_r>;
}

pseudo_system('command', 'arg');
pseudo_system('command arg');

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About