in reply to How to set pipe first and then use the forkmanager?

I think I must establish 4 pipes first(right?),

Why? Why not fork first and have each fork establish its own pipe?

Untested example:

for my $file ( @data_files ) { $pm->fork and next; open my $h, "| ...." or die $!; print $h ...; ... $pm->finish; } $pm->wait;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: How to set pipe first and then use the forkmanager?
by mlin (Novice) on Sep 22, 2016 at 10:51 UTC
    Because establish the pipe to the program may be a little bit time-consuming. So I would like to move the open pipe outside the foreach() in the serial type. Similarly I consider may be I should establish pipe first and then use fork.
      Presumably, you're forking because your input data files are very large. The time taken to open a pipe is then negligible compared to the time needed for processing your data.

      Don't forget that premature optimization is the source of almost all evil.

        Thanks! I'll use this form at the moment, maybe I solve the problem when I know more about the fork. I have a new question: ----- complement: while the program A runs, it usually prints some information or warnings to the screen? Will it be a trouble for the forked processes? or I must throw the output of A, like:
        foreach (@data_files) { open $h, "| program_A 2>& /dev/null" or die ...; print $h ...; print $h ...; ... } close($h);
        Can I hold those information? Thanks!