pipes from forked processes

Discussion in 'Perl Misc' started by Corey_G, Jul 6, 2004.

  1. Corey_G

    Corey_G Guest

    I have some code that forks processes which then go off and do some
    work. I am trying to make it so child processes can communicate back
    to the parent that spawned them. From what I have read, it looks like
    I want to use Pipes.

    I also want to make sure this is portable to Windows. In the perlfork
    documentation, it states: "The open(FOO, "|-") and open(BAR, "-|")
    constructs are not yet implemented. This limitation can be easily
    worked around in new code by creating a pipe explicitly." which then
    goes on to give some sample code that doesn't work.. great.

    So I am now looking at using either IO::pipe or POE:pipe to do this
    for me. Documentation for both is pretty sparse and the samples
    aren't enough to get me going.

    Does anyone have a sample piece of code using either of these modules
    along with forking?

    -Corey Goldberg
    Corey_G, Jul 6, 2004
    1. Advertisements

  2. Rocco Caputo

    Rocco Caputo Guest

    On 5 Jul 2004 18:58:41 -0700, Corey_G wrote:
    > So I am now looking at using either IO::pipe or POE:pipe to do this
    > for me. Documentation for both is pretty sparse and the samples
    > aren't enough to get me going.

    If you're using POE, you may want to look at POE::Wheel::Run. Also
    IPC::Run if you're not. Both modules implement the pipe/fork/exec
    pattern, although the POE one does for spawning processes from within
    that framework.

    Rocco Caputo - http://poe.perl.org/
    Rocco Caputo, Jul 6, 2004
    1. Advertisements

  3. Corey_G

    Corey_G Guest

    I finally figured out what I am doing thanks to this post from Bob


    he does this and it works great:


    use strict;
    $| = 1;
    my $nc = 3; # number of children to create
    pipe READER, WRITER; # pipe for communication

    for my $c (1 .. $nc) {
    # create a child process
    defined(my $pid = fork) or die "Couldn't fork: $!";
    next if $pid; # parent loops to create next child

    # child does it's thing and writes back to parent through pipe
    close READER;
    select WRITER;
    $| = 1;
    print "Hello, I am child $c, and my PID is $$\n";
    sleep rand(5) + 1;
    print "Goodbye from child $c\n";
    exit; # child exits (IMPORTANT!)

    # parent reads from children
    # pipe will close when last child exits
    close WRITER;
    while(<READER>) {
    print $_;

    1 while wait() > 0; # reap all exit statuses

    Sample output:

    $ perl myscript.pl
    Hello, I am child 1, and my PID is 16774
    Hello, I am child 2, and my PID is 16775
    Hello, I am child 3, and my PID is 16776
    Goodbye from child 2
    Goodbye from child 1
    Goodbye from child 3
    Corey_G, Jul 6, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kmkz

    Kill forked processes

    kmkz, Feb 26, 2006, in forum: Python
  2. Pierre Morel
    Pierre Morel
    Feb 17, 2010
  3. Tim Uckun
    Tim Uckun
    Oct 14, 2010
  4. Zbigniew Fiedorowicz

    Getting STDERR from forked child processes?

    Zbigniew Fiedorowicz, Feb 5, 2004, in forum: Perl Misc
    Aaron Sherman
    Feb 5, 2004
  5. it_says_BALLS_on_your forehead

    question about forked processes writing to the same file

    it_says_BALLS_on_your forehead, Oct 17, 2005, in forum: Perl Misc
    it_says_BALLS_on_your forehead
    Oct 29, 2005

Share This Page