topher has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to have a process start a couple of child processes, and then continually watch the output from the child processes to monitor status. However, when I try looping through the filehandles associated with the child processes (using IO::Select), the parent reads from the first child, and then seems to (internally) close() the filehandle and waitpid() on the child PID.
I feel like I'm probably missing something small or I have an error I'm not seeing here, but I'm running out of ideas. Here's a stripped down piece of code that shows what I'm seeing:
use strict; use warnings; use IO::Select; use IO::File; use Data::Dumper; my @childs; foreach my $kid(qw(foo bar baz quux)) { # Create and gather child processes/fd's push @childs, child_labor($kid); } my @results; my @files_ready; my $file_iter = IO::Select->new(); $file_iter->add(@childs); while (@files_ready = $file_iter->can_read(1)) { for (@files_ready) { my $fd = shift @{$_}; my $child = shift @{$_}; chomp(my $data = <$fd>); if ($data) { push @results, [ split ":", $data ]; } sleep 10; } } # Printing some results... print Dumper(@results); sub child_labor { my $name = shift; my $fd; defined (my $pid = open $fd, "-|") # Indirect fork, read child st +dout or die "Couldn't fork: $!"; # Parent... if ($pid) { return [ $fd, { pid => $pid, name => $name } ]; } # Child... else { $0 .= " $name"; my $counter = 0; while (1) { # Generate some output for our parent to read print "$name:" . 100 * (int(rand(100))+1) . "\n"; sleep int(rand(10)); last if $counter++ > 13; } # Important to exit from the child. exit 0; } }
And here's what's happening "under the hood" with the parent process when I run it:
ioctl(6, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff0f84f2f0) = -1 EINVAL (I +nvalid arg ument) lseek(6, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) fcntl(6, F_SETFD, FD_CLOEXEC) = 0 select(8, [3 4 5 6], NULL, NULL, {1, 0}) = 1 (in [3], left {0, 999994} +) read(3, "foo:7100\n", 8192) = 9 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 nanosleep({10, 0}, 0x7fff0f84f7d0) = 0 close(3) = 0 rt_sigaction(SIGHUP, {SIG_IGN, [], SA_RESTORER, 0x7fdd25eb2030}, {SIG_ +DFL, [], 0 }, 8) = 0 rt_sigaction(SIGINT, {SIG_IGN, [], SA_RESTORER, 0x7fdd25eb2030}, {SIG_ +DFL, [], 0 }, 8) = 0 rt_sigaction(SIGQUIT, {SIG_IGN, [], SA_RESTORER, 0x7fdd25eb2030}, {SIG +_DFL, [], 0}, 8) = 0 wait4(10129,
You can see the select, followed by the read. That part works fine. You can also see the sleep call that I put at the end of the loop as a marker. However, as soon as it finishes the sleep and hits the end of the for loop, it closes the filehandle and wait()'s for the child PID to end. I'm just not sure why, or how to fix this.
If there's an overall better way of doing this, I'd love to hear it. However, I'd still like to figure this out so I understand why it's happening.
UPDATE: I think I figured it out. At the least, I've got things working now. For details, see my response below: Re: Select on child output problem
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Select on child output problem
by topher (Scribe) on May 03, 2012 at 04:13 UTC | |
by chromatic (Archbishop) on May 03, 2012 at 05:19 UTC | |
by topher (Scribe) on May 03, 2012 at 06:19 UTC | |
Re: Select on child output problem
by chromatic (Archbishop) on May 03, 2012 at 02:09 UTC | |
by topher (Scribe) on May 03, 2012 at 02:31 UTC | |
Re: Select on child output problem
by BrowserUk (Patriarch) on May 03, 2012 at 03:11 UTC |