I'm trying to sub-invoke 2 programs then wait until both are done; sort of like this in a shell:
myprog1 & myprog2 & wait
Since Perl lets me actually open these as pipes so I can read their output (instead of having the programs write a file) I thought I'd try it using Perl. I'm doing something like this (error checking removed for brevity):
my $pid1 = open(PIPE1, "myprog1|"); my $pid2 = open(PIPE2, "myprog2|"); while ($pid1 >= 0 || $pid2 >= 0) { my $pid = wait; if ($pid == $pid1) { print "myprog1 completed.\n"; $pid1 = -1; } elsif ($pid == $pid2) { print "myprog2 completed.\n"; $pid2 = -1; } } my $output1 = <PIPE1>; # this works (I see the output of "myprog1") my $output2 = <PIPE2>; # this works as well close PIPE1; # this fails! close PIPE2; # this fails as well!
Why does the close fail? The help for "close" indicates that an implicit wait is done on the piped process before the close returns -- does the same thing happen if an explicit "wait" is done first? If so, how does the reading of the pipes (i.e. <PIPE1> and <PIPE2>) work if the file has been closed? Does Perl maintain the output of the program somewhere? It seems weird that you can read from something that you can't close. Am I missing something here? Thanks.

In reply to wait versus close on a pipe by daven7

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.