Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You are connecting to both stdin & stdout, but never processing stdout. Consequently, if the child process produces any output, it will eventually fill the pipe that you are not reading and the OS will block the child from writing anymore until you do something in the parent to reduce the content of the pipe.

The number of iterations it will run will depend upon the amount of output produced by the child. Ie. How quickly it fills the pipe buffer. Eg. In the following which just echos the 'some command's it reads, the iteration count gets to 83 before it blocks.

#! perl -slw use strict; use IPC::Open2; $|++; my $pid = open2 \*READ, \*WRITE, 'perl -wne "$|++; chomp; print"'; my $read = ''; for ( 1 .. 1e4 ) { Win32::Sleep( 10 ); printf "\r$_\tGot'$read'\t"; print WRITE 'some command'; } __END__ c:\test>junk Name "main::READ" used only once: possible typo at c:\test\junk.pl lin +e 7. 83 Got'' Terminating on signal SIGINT(2)

83 * 13 = 1079, suggesting roughly a 1k buffer.

Reduce the output to a single char:

#! perl -slw use strict; use IPC::Open2; $|++; my $pid = open2 \*READ, \*WRITE, 'perl -wne "$|++; chomp; print 1"'; my $read = ''; for ( 1 .. 1e4 ) { Win32::Sleep( 10 ); printf "\r$_\tGot'$read'\t"; print WRITE 'some command'; } __END__ c:\test>junk Name "main::READ" used only once: possible typo at c:\test\junk.pl lin +e 7. 553 Got'' Terminating on signal SIGINT(2)

This time the iteration count gets to 553, which means that the size of the buffer is a little more complicated.

But if you process the output from the child within the parent's loop:

#! perl -slw use strict; use IPC::Open2; $|++; my $pid = open2 \*READ, \*WRITE, 'perl -wne "$|++; chomp; print 1"'; my $read = ''; for ( 1 .. 1e6 ) { # Win32::Sleep( 10 ); printf "\r$_\tGot'$read'\t"; print WRITE 'some command'; sysread( READ, $read, 1024 ); } __END__ c:\test>junk 1000000 Got'1'

Now the loop happily runs for as long as you care to leave it running. The OS is simply protecting you from yourself, by preventing you from consuming all your memory by filling a pipe with output that you are never processing.

Note: I used the non-blocking sysread to process the output as the child process is never producing newlines, so <READ> or read would block.


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.

In reply to Re: open2() in Windows by BrowserUk
in thread open2() in Windows by bhaveshbp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found