I'm having a problem in a rather large program, so I've written this small
example to illustrate.
What this does is fork a process, then exec in the child to run a separate
program without waiting. In reality my main program doesn't end, so I need
to reap the children as they finish. After I exec the child process, I move
on and open another program as a pipe, using its output.
My problem is that this other program I open as a pipe, when it finishes,
sends the CHLD signal to my main program, so my signal processing happens
before the close statement.
#!/usr/bin/perl -w
use strict;
use POSIX ":sys_wait_h";
$SIG{CHLD} = sub {
my $kid;
do {
$kid = waitpid(-1, WNOHANG);
print "Child process $kid finished\n" if $kid > 0;
} until $kid == -1;
};
my $child_pid;
if ($child_pid = fork) {
print "Forking process $child_pid\n";
} elsif (defined $child_pid) {
exec('banner test');
exit; #just in case
} else {
die "Cannot fork: $!";
}
open LS, "ls |" or die "Cannot open ls: $!";
while (<LS>) {
print;
}
close LS or warn "Cannot close ls: $!";
print "\n";
My output looks like this:
##### ###### #### #####
# # # #
# ##### #### #
# # # #
# # # # #
# ###### #### #
Child process 29348 finished
Forking process 29348
Child process 29349 finished
limits.cfg
pumpWarn.pl
pumpd.pl
sigTest.pl
Cannot close ls: No child processes at sigTest.pl line 29.
So, the signal sub is reaping my piped program before the close, which then
warns the process is already gone.
Should I do anything about this or just not warn on this close statement?
-Mark
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]