This is my guess as to what you want...

I worry about data from the four forked processes arriving interleaved on the pipe, maybe even in the middle of each other's lines :(

This solution caches each fork's response until complete and then forwards it to program_A in one piece. I wasn't sure on how to do that using ForkManager.

For testing's sake I combined program_A with the forking program.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1172353 use strict; use warnings; use IO::Select; $| = 1; my @data_files = qw( one two three four five six seven ); my $maxchildren = 4; my %data_for_handles; my $fh_A; if( open $fh_A, '|-' ) { # parent warn "pipe opened\n"; } else { # child print "program_A started\n"; print while <STDIN>; print "program_A ended\n"; exit; } my $sel = IO::Select->new; while( @data_files or $sel->count ) { while( @data_files and $sel->count < $maxchildren ) { my $file = shift @data_files; if( open my $fh, '-|' ) { # parent $sel->add($fh); } else { # child $| = 1; warn "child $file started\n"; print "$file\n"; sleep 1; print "$file\n"; sleep 1; print "$file\n"; exit; } } if( $sel->count > 0 ) { for my $fh ($sel->can_read) { if(0 < sysread $fh, my $buffer, 16 * 1024 ) { $data_for_handles{$fh} .= $buffer; } else { print {$fh_A} delete $data_for_handles{$fh}; $sel->remove($fh); } } } } close $fh_A;

In reply to Re: How to set pipe first and then use the forkmanager? by tybalt89
in thread How to set pipe first and then use the forkmanager? by mlin

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.