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


In reply to Select on child output problem by topher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.