in reply to Re: fork, but not waiting on childs
in thread <solved> fork, but not waiting on children
Thanks for the suggestion Luke, I think I got it now.
I'm a bit surprised. I don't do anything special regarding the zombie issue but top does not list any zombies - although the documentation warned about this and the parent process has already died? (I executed `./forker.pl find /usr -type f >/dev/null`.)
coderesult#!/usr/bin/env perl use strict; use warnings; use 5.010_001; # essential libraries use English qw( -no_match_vars ); use POSIX qw(:sys_wait_h); my @cmd = @ARGV; my $cmd_str = join ' ', @cmd; my $pid = fork(); if (not defined $pid) { # fork failed die "Cannot fork: $ERRNO\n"; } elsif ($pid == 0) { # child process say("Executing $cmd_str"); my $retval = system(@cmd); my $exitcode = parse_retval($retval); # TODO add URL callback say("[child] Exiting with exit code $exitcode."); exit $exitcode; } else { # parent process # wait a bit to give child process a chance to die prematurely sleep 1; my $cstatus = waitpid($pid, WNOHANG); my $exitcode; if ($cstatus == 0) { $exitcode = 0; say("[parent] Child is still running."); } else { $exitcode = parse_retval(${^CHILD_ERROR_NATIVE}); } say("[parent] Exiting with exit code $exitcode."); exit $exitcode; } sub parse_retval { my $retval = shift; my $signal = $retval & 127; my $have_coredump = $retval & 128; my $exitcode; if ($retval == -1) { # program failed to execute $exitcode = 127; } elsif ($signal) { my $message = "W: Process died with signal $signal!"; if ($have_coredump) { $message .= ' (coredump available)'; } warn($message ."\n"); $exitcode = 255; } else { $exitcode = $retval >> 8; } return $exitcode; }
$ ./forker.pl true ; echo "EXIT $?" Executing true [child] Exiting with exit code 0. [parent] Exiting with exit code 0. EXIT 0 $ ./forker.pl false ; echo "EXIT $?" Executing false [child] Exiting with exit code 1. [parent] Exiting with exit code 1. EXIT 1 $ ./forker.pl sleep 10 ; echo "EXIT $?" Executing sleep 10 [parent] Child is still running. [parent] Exiting with exit code 0. EXIT 0 <10 seconds later> [child] Exiting with exit code 0. $ ./forker.pl bogus ; echo "EXIT $?" Executing bogus Can't exec "bogus": No such file or directory at ./forker.pl line 22. [child] Exiting with exit code 127. [parent] Exiting with exit code 127. EXIT 127 $ ./bogus ; echo "EXIT $?" bash: ./bogus: No such file or directory EXIT 127
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: fork, but not waiting on children
by blindluke (Hermit) on Sep 23, 2014 at 18:39 UTC | |
by Monk::Thomas (Friar) on Sep 25, 2014 at 08:06 UTC |