jafoba has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a script that will fork a controlled number of subprocesses. Each subprocess will execute a command line via system so that I can reap the logs created by that process. It is possible for the executable to stop itself or so it seems. The problem is when the subprocess stops like this the parent also stops. I can force them to complete by sending SIGCONT to the child pid and then the parent pid. I thought that WNOHANG would keep the parent from hanging. I tried to capture the stop in both the parent and child but no luck. Any ideas how to get around this? I have read perlipc and have written other similar scripts that are working.
Included is some code that shows what I am attempting to do. It only forks one child but the real one will fork multiple. Perl 5.8.2 on AIX 5.3.
#!/usr/bin/perl use strict; use lib '/homegrown/lib'; use MY_Logging; use MY_File qw(slurp); use POSIX qw(:signal_h :errno_h :sys_wait_h); select STDERR; $|=1; our ($me) = $0 =~ m/\/?([^\/]+)$/; my %children; my $child; $SIG{CHLD} = \&REAPER; sub REAPER { my $reaped_pid = waitpid(-1, &WNOHANG); if ($reaped_pid == -1) { } elsif (WIFEXITED($?)) { delete $children{$reaped_pid}; print STDERR slurp("$me.$reaped_pid.log"); } else { logMsg("False alarm on $reaped_pid"); } $SIG{CHLD} = \&REAPER; } if ($child = fork()) { $children{$child} = 'TEST'; logMsg("spawned pid $child"); } else { close(STDERR); my $LOG = "$me.$$.log"; open (STDERR, ">>$LOG") or dieScreaming("$LOG open failure: $!"); select STDERR; $|=1; foreach my $host ('eidspstd01', 'eiqspstd01') { my $cmd = "..."; logMsg("executing $cmd"); my $CMD_LOG = "$me.$$.load.log"; system("$cmd >$CMD_$LOG 2>&1"); logLog($CMD_LOG); # pull in cmd log contents to STDERR } logMsg("completed TEST"); close(STDERR); exit(0); } # wait for any children that may be running still while (%children) { logMsg("WATING FOR: ", join(',', values %children)); sleep(5); } logMsg("$me complete");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stopped child hangs parent
by Argel (Prior) on Aug 06, 2009 at 23:20 UTC | |
by jafoba (Novice) on Aug 07, 2009 at 13:17 UTC | |
by Argel (Prior) on Aug 08, 2009 at 01:57 UTC | |
|
Re: Stopped child hangs parent
by ig (Vicar) on Aug 08, 2009 at 10:24 UTC | |
by jafoba (Novice) on Aug 10, 2009 at 14:38 UTC | |
by ig (Vicar) on Aug 10, 2009 at 20:49 UTC | |
by jafoba (Novice) on Aug 11, 2009 at 14:21 UTC | |
by almut (Canon) on Aug 11, 2009 at 23:00 UTC | |
| |
by ig (Vicar) on Aug 11, 2009 at 18:38 UTC |