Bob Showalter wrote:
> You want to have some communication between the
> children and the parents so the child can tell the parent when he's
> ready to receive the next message.

Here's an example using a pipe for the children to signal the parent:

    #!/usr/bin/perl

    use strict;

    use IO::Pipe;
    use IO::Handle;
    use Proc::Fork;

    my $num_children = 3;               # no. of children
    my $control = IO::Pipe->new;        # control channel

    # Spawn off some children
    my @child;
    for my $num (1 .. $num_children) {
        my $data = IO::Pipe->new;       # data channel

        child {
            $data->reader;
            $control->writer;
            $control->autoflush(1);

            print $control "$num\n";
            while (<$data>) {
                chomp;
                print "Child $num: received [$_]\n";
                sleep rand(3) + 1;
                print $control "$num\n";
            }
            exit;
        };

        # parent
        $data->writer;
        $data->autoflush(1);
        push @child, $data;
        next;

    }

    # send data to children as they become ready
    my $n = 1;
    $control->reader;
    while(<$control>) {
        chomp;
        my $fh = $child[$_ - 1];
        print $fh "Data $n\n";
        $n++;
        last if $n > 20;
    }

    # reap the children
    close $_ for @child;
    1 while wait > 0;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to