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??
In the below perl script, I fork, then have the child start "cat" via open3(). The child does some I/O with the "cat" process, then exits. I'm using Cygwin's cat.exe .
my $CMD = 'cat'; use IO::Handle; use IPC::Open3 qw( open3 ); use strict; my $pid = fork(); if ( !defined($pid) ) { die( "$$: Fork failed: $!\n" ); } # parent if ( $pid ) { print( "$$ FORKED $pid\n" ); } # child else { my( $pw, $pr, $pe, $pipe ); foreach $pipe ( $pw, $pr, $pe ) { $pipe = new IO::Handle; } my $pipe_pid = open3( $pw, $pr, $pe, $CMD ) or die( "$$: failed to handle to $CMD: $!\n" ); print( "$$:\tpipe pid = $pipe_pid\n"); $pw->print( "hostinfo\n" ); $pw->print( "quit\n" ); print( "$$: out: ", $pr->getline(), "\n" ); exit(0); } waitpid( $pid, 0 ); print( "$$ caught $pid: $?\n" ); print( "PARENT $$ exiting\n" );
This works fine on Unix (perl 5.6.0):
  $ ./t_iohand2
  8629 FORKED 8630
  8630:   pipe pid = 8631
  8630: out: hostinfo

  8629 caught 8630: 0
  PARENT 8629 exiting
However, it does this on Win32 (ActiveState Perl 5.6.0-623 and 5.6.1-625):
    D:>perl -w t_iohand2
    304 FORKED -1392
    am I in cat?
    #hangs here, so I type in "am I in cat?" to see if this is cat's STDIN/STDOUT
    am I in cat? #cat repeated the line back
    (hit ^D here to end cat)
    Use of uninitialized value in print at t_iohand2 line 35.
    -1392: pipe pid = 1400
    -1392: out:
    304 caught -1392: 0
    PARENT 304 exiting

The process tree for the Win32 output looks like this: 304 -(fork)-> -1392 -(open3)-> 1400

It looks like the STDIN/STDOUT of process 1400 (the open3() child) is getting mixed up with the STDIN/STDOUT of it's parent, -1392.

I'm using "cat" as an example here. In the actual script, $CMD is the path to a shell-like program (similar to csh) running on Win32.

This works fine on Cygwin perl 5.6.1-1, but my project wants to use ActiveState for a variety of reasons.

Thanks for any input!


In reply to fork and open3 on Win32 by this_hamster

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 meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found