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

Re: [PATCH] Attempt at improving the perlipc docs

Thread Previous | Thread Next
From:
Maik Hentsche
Date:
April 22, 2010 11:44
Subject:
Re: [PATCH] Attempt at improving the perlipc docs
Message ID:
20100422204433.52ad8ade@hermes
Leon Timmermans <fawaka@gmail.com> wrote:

> 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).

I think a short piece of code using waitpid would be a good
explanation. From my experience forking is most often used in daemon
environments so the code below should be a good start for most people.
It prevents the race condition in Eric's code and I don't see any new
issues. I'd appreciate your comments on the example below. I'm going to
improve my documentation changes with this:

$SIG{CHLD}=sub{$child_is_gone=1;};

sub do_cleanup
{
        do {
                $kid = waitpid(-1, WNOHUP);
                cleanup_child($kid) if $kid > 0;
        } while $kid > 0;
}                                

while(1){
        my $pid = fork();
        if ($pid == 0){ 
             ...
             exit 0;
        } else {
	     ...
             system($command);
             do_cleanup if $child_is_gone;
        }
}

> Also, adding it three times may be an overkill. Personally don't think
> adding it to the system() documentation too adds much. You shouldn't
> be thinking about this when using system(), you should when using
> wait() IMO.

I agree. In perlfunc a link from wait() to perlipc should suffice. 

so long
Maik

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