There are a couple issues with your posted code, with the biggest being that you've actually written blocking code rather than allowing the children to operate in parallel. You're also trying to reap children twice, which will, again, be problematic. Assuming you want to roll your own (good for learning, less good for production), you'll want something closer to:
use strict; use warnings; my @ips = (1 .. 10); my %pipe; for my $ip (@ips) { pipe my $reader, my $writer; my $pid = fork; die "Error: failed to fork\n" if not defined $pid; if ($pid) { close $writer; $pipe{$ip} = $reader; } else { sleep $ip; print $writer 365*$ip; exit; } } my @nums; for my $ip (@ips) { my $handle = $pipe{$ip}; # Necessary to avoid glob mapping push @nums, <$handle>; } 1 while (wait != -1); print "Numbers: @nums \n";

Note that since reading is a blocking operation, it can't happen in the normal flow. You also need to wait to reap until after the output channels have been read. Unfortunately, I've missed spec with the code above, because I've removed the maximum number of children constraint. This one is problematic, since it requires that you be able to poll your children to find out if anyone is ready to output and be reaped. You can't just call wait, since your children won't be ready for reaping until they are done with their I/O.

At this point, you have two choices. You can use flock and have each child write asynchronously to an output channel. This choice is nice, because then you can do your reaping in a manner similar to how you were working before:

if ( $children == $max ) { wait(); }

Alternatively, you can use IO::Select (CORE, though select works if you are self-destructive) to check your handles if they are ready to be read, and reap as you go. You could also implement something simpler here with threads, allowing you to bypass the need for pipes.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: Correct usage of fork and pipe? by kennethk
in thread Correct usage of fork and pipe? by CircusGimp

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.