when perl execute line my $pid = open CMD, 'program.exe |' or die $!;. program.exe will run forever and my perl script hang.

The only way I'm aware of that can happen is if the program produces output without any newlines.

E.g: This (with -l so print adds newlines), returns control to the calling program immediately:

$pid = open CMD, '-|', q[perl.exe -le"print ++$i while sleep 1"] or di +e $!;;

But this (no newlines) won't return control to the caller until it fills and flushes the 4k buffer on STDOUT. Which given it only produces just a few characters every second, will take a long time:

$pid = open CMD, '-|', q[perl.exe -e"print ++$i while sleep 1"] or die + $!;;

If the program in question is buffering its output, is not adding newlines, and doesn't produce enough to fill the buffer, it will never return control to the caller.

In that case, you're pretty much screwed unless you can recompile the program.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re^3: Run and Stop another program in perl by BrowserUk
in thread Run and Stop another program in perl by fast

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.