tran_sonic has asked for the wisdom of the Perl Monks concerning the following question:

I've got two processes A and B. A spawns B by using the
open2 method from the IPC::Open2 module which gives A the
file handles READ and WRITE. I'm using the pipe to send
text strings between A and B.

The problem is that when A tries to send text to B, B
hangs.

Here are examples:

### A ### ... open2( \*READ, \*WRITE, 'B' ); print WRITE "Hello there.\n"; ... ### B ### $input = <STDIN>; print "echo back: $input"; ...

I know that <> waits for a '\n', by default, before it
returns and I'd like to keep it that way.

I'd appreciate any help I receive. Thanks.

Replies are listed 'Best First'.
Re: Pipe problems
by bluto (Curate) on Aug 31, 2004 at 15:30 UTC
    Make sure B has autoflush turned on before the print for STDOUT...
    $| = 1; print ...

    Also, terminate the print with a '\n'. Though you haven't shown it, I'm assuming A reads back the line printed from B. (If not, B can hang on the print waiting for A to read it if B prints a large enough string, filling the pipe.) Update: From your code, it looks like A sends the '\n' to B so the comment about terminting the print may not apply.

      Autoflush on the WRITE filehandle in A seems more likely to be the problem. In newer versions of Perl you can use:
      use IO::Handle; # ... WRITE->autoflush(1);
      In older versions you should use:
      $oldfh = select(WRITE); $| = 1; select($oldfh);

      Update: bluto's right, IPC::Open2 does this for you. I usually just use pipe, which doesn't.

        This won't help this problem. According to even older perl docs for IPC::Open2 autoflush is turned on for the parent's write handle.
Re: Pipe problems
by Random_Walk (Prior) on Aug 31, 2004 at 15:09 UTC
    I am trying this and it works fine for me, perl version 5.005_03. Is B up to anything else that makes it hang ? Can you run the test code below (with the right path to B) on your system ?
    #!/usr/local/bin/perl -w #### A #### use IPC::Open2; open2( \*READ, \*WRITE, '/home/random/B' ); print WRITE "Hello there.\n"; my $in=<READ>; print "$0 read: $in\n" #!/usr/local/bin/perl -w #### B #### $input = <STDIN>; print "echo back: $input";
    Regards,
    R.

    update

    I think the following reply has the answer, set autoflush with $|=1 in B or A may hang while the data from B is hanging around in buffer.