Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: pipe fork win32

by BrowserUk (Patriarch)
on Aug 26, 2012 at 01:44 UTC ( [id://989769]=note: print w/replies, xml ) Need Help??


in reply to pipe fork win32

Also try:

use strict; use warnings; sub debuf{ select( ( select( $_[0] ), $|++ )[0] ) } my ($child, $parent); pipe($child, $parent) or die; debuf( $parent ); my $pid = fork(); die "fork() failed: $!" unless defined $pid; if ($pid) { close $child; } else { close $parent; print "child waiting\n"; my $read; read($child, $read, 1); print "child exiting\n"; exit(0); } print $parent "exit now\r\n\r"; print "parent going to wait\n"; waitpid($pid, 0);

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".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: pipe fork win32
by bulk88 (Priest) on Aug 26, 2012 at 02:38 UTC
    This also works. Why? what did you do?

    I read select but why? My sockets and File I/O knowledge is not very good. Wouldn't changing default handle break "print "parent going to wait\n";" line and make it go to the child (it doesn't in real life, I got that line in the console) instead of to console? "print $parent "exit now\r\n\r";" says an explicit handle, it is not using a default handle why would changing the default handle affect it?
      I read select but why?

      All the debuf() does is apply $|++ to $parent, thus making the handle line-buffered rather than block buffered.

      Hence, the \n in print $parent "exit now\r\n\r"; causes the buffer to be flushed through and the read then completes.

      (BTW: The \rs do nothing! As the pipe is in text mode, the \n will be translated to \cM\cJ on write and then back to \cJ when read back.)

      why would changing the default handle affect it?

      if you look closely at debuf(), it selects the handle ($parent) that is passed to it; does the $++ whilst that handle is selected, and then re-selects the original default handle. Ie. It is equivalent to:

      sub debuf{ my $old = select( $_[0] ); $|++; select( $old } }

      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      /code
        While both solutions of yours work, I am going to use this solution, it seems the most reliable (compared to guessing the IO buffer size of day). Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://989769]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found