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

code
#!/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; }
result
$ ./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

    Glad to see you figured it out, and it's working. I started to play around with a proof of concept, and then I went looking for some nice way to do it (hiding the nasty flags under the hood). I found a very promising module: Proc::Simple. It looks like something that could be useful to you.

    As for the zombies, don't worry: if a parent terminates without getting the child exit status (without wait()ing for the child), the init process will wait() for the child instead, so it does not become a zombie. You can look at the notes section in man 2 wait, the whole thing is described there very precisely.

    All the best.

    regards,
    Luke Jefferson

      Thanks for the hint about Proc::Simple. I was hoping that maybe I could remove the 'sleep 1' in the parent process but unfortunately that is not the case. Still need to wait a bit before calling $proc->poll().

      For the moment I'll stay with manually doing the forking but if there's a bug or otherwise strange behavior it's good to know I can easily replace it with a generic solution.