develooper Front page | perl.perl5.porters | Postings from August 2005

Re: [perl #36976] system() always returns -1 in forked child with SIG{CHLD}='IGNORE' in parent

Thread Previous | Thread Next
From:
Rick Delaney
Date:
August 22, 2005 10:29
Subject:
Re: [perl #36976] system() always returns -1 in forked child with SIG{CHLD}='IGNORE' in parent
Message ID:
20050822172904.GI7674@localhost.localdomain
On Mon, Aug 22, 2005 at 03:29:42AM -0700, Chris wrote:
> 
> Calls to system() in a forked child always return -1 if the parent has
> set SIG{CHLD}='IGNORE'. I would expect/hope it to return 0.
> 
> Demonstrated by:
> perl -e '$SIG{CHLD} = "IGNORE"; fork or sleep 0xbeef; print
> "system:".system("true")."\n";'
> prints: system:-1

When $SIG{CHLD} is set to "IGNORE", wait(2) will return -1 [1].  Since
system returns the status as provided by wait and wait isn't providing a
status I think the -1 return is reasonable.  Check $! when system
returns -1; in this case it will say "No child processes" (ECHILD).

The patch after my .sig addresses this in perlfunc.

[1] On Linux this is not true and wait(2) will ignore the IGNORE and
    actually wait for the process to finish, returning the status and
    pid.  I personally don't think perl should support this behaviour.

-- 
Rick Delaney
rick@bort.ca


diff -ruN perl-current/pod/perlfunc.pod perl-current-dev/pod/perlfunc.pod
--- perl-current/pod/perlfunc.pod	2005-08-05 10:42:09.000000000 -0400
+++ perl-current-dev/pod/perlfunc.pod	2005-08-22 13:19:01.312460976 -0400
@@ -6017,6 +6017,8 @@
 the output from a command, for that you should use merely backticks or
 C<qx//>, as described in L<perlop/"`STRING`">.  Return value of -1
 indicates a failure to start the program (inspect $! for the reason).
+It could also mean that you have set C< $SIG{CHLD} = 'IGNORE' >, in which
+case the C<wait> call would return -1 (inspect $!).
 
 Like C<exec>, C<system> allows you to lie to a program about its name if
 you use the C<system PROGRAM LIST> syntax.  Again, see L</exec>.

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