http://qs1969.pair.com?node_id=515603

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

Hey. Could someone explain to me how to use pipes?
I know it seems like the type of thing I should just google, but I've looked all over and can't find a good explanation.

If I have:

if($pid = fork) {
#parent
} else {
#child
}
how would I use pipes to send messages from the parent to the child and vice verca? Thanks.

Replies are listed 'Best First'.
Re: Pipes (for interprocess communication)
by robin (Chaplain) on Dec 09, 2005 at 18:13 UTC
    Here is a simple example:
    pipe(my $reader, my $writer); # Create a pipe if (my $pid = fork()) { # In parent: read one line from the pipe and print it print "Message from child: ".<$reader>; } else { # In child: print a line of text to the pipe print $writer ("Hello, parent!\n"); }
      Sorry, one more question.
      Why is it that "print ''.<$reader>" works, but not just "print <$reader>" ?

        .<$reader> forces scalar context while <$reader> uses the context supplied by the print, which is array context. In array context, the <> operator sucks in the whole rest of the file.

        Why does this not work? Because it is going to wait indefinately the the end of the file to come. On pipes, the end of file comes when the other end of the pipe is closed. In robin's example, the pipe is never closed.

        Mostly for this reason, the usual paradigm is this:

        1. Create the pipe
        2. fork
        3. in the parent, close the reader and use only the writer
        4. in the child, close the writer and use only the reader.
        or, of course,
        1. Create the pipe
        2. fork
        3. in the parent, close the writer and use only the reader
        4. in the child, close the reader and use only the writer.

        Trying to use both the reader and the writer end of the same pipe in the same process leads to deadlock. See the documentation of things like IPC::Open2 for how this can occur with a longer chain of pipes too.

      Thanks

      And the parent could also write to $writer with the child reading from $reader, right?
        Right.

        Update: I should have been more specific. Unless you really know what you're doing, you should choose a direction (i.e. parent -> child or child -> parent) and use the pipe only in that direction. If you want to communicate both ways, just make two pipes.

Re: Pipes (for interprocess communication)
by talexb (Chancellor) on Dec 09, 2005 at 17:48 UTC

    Check out perldoc perlipc for documentation on how to do that.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Pipes (for interprocess communication)
by Old_Gray_Bear (Bishop) on Dec 09, 2005 at 17:57 UTC
    Take a look at recipe 16.10 in the Perl Cookbook, "Communications Between Related Processes". There are examples of pipes used to communicate between a parent/child process in various configurations.

    For a much more complete discussion of how pipe() is used for inter-process communications, see Stevens -- Advanced Programming in the UNIX Environment.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Pipes (for interprocess communication)
by rinceWind (Monsignor) on Dec 09, 2005 at 18:00 UTC

    If you want some general explanation on IPC and pipes - not in Perl but in C, see http://www.cim.mcgill.ca/~franco/OpSys-304-427/lecture-notes/node28.html. (found this with Google)

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: Pipes (for interprocess communication)
by tphyahoo (Vicar) on Dec 09, 2005 at 18:10 UTC
    The recent conversion at output from an external program may be enlightening. In particular see merlyn's comment. (I still don't fully understand what he was saying but I'm working on it :) )
Re: Pipes (for interprocess communication)
by olecs (Scribe) on Dec 12, 2005 at 08:13 UTC
    In a comment to this post: output from an external program, brother merlyn gives a pretty good example on how to use pipes when you want to write to and read from the same program without having to think about deadlocks. It should easily be adaptable to other scenarios as well.

    rgds,
    Ole C.