Here's my guess.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1192662 use strict; use warnings; use IO::Select; my $childcount = 3; my $hasterminal = 1; my %who; my %pipes; for my $from (1 .. $childcount) { for my $to (1 .. $from - 1, $from + 1 .. $childcount) { pipe( my $readhandle, my $writehandle) or die "$! on pipe"; $writehandle->autoflush; $pipes{$from}{$to}{rh} = $readhandle; $pipes{$from}{$to}{wh} = $writehandle; } } for my $me (1 .. $childcount) { if( my $pid = fork ) # parent { $who{$pid} = $me; } elsif( defined $pid ) # child { my $sel = IO::Select->new; $me == $hasterminal and $sel->add(*STDIN); for my $from (1 .. $me - 1, $me + 1 .. $childcount) { $sel->add($pipes{$from}{$me}{rh}); close $pipes{$from}{$me}{wh}; } while(1) { for my $handle ($sel->can_read) { defined( my $command = <$handle> ) or exit; print "$me got $command"; $command =~ /^(\d+)\s+(.*\n)/ and $1 != $me and print { $pipes{$me}{$1}{wh} } $2; } } } else { die "fork failed with $!"; } } use Data::Dump 'pp'; pp \%who; my $pid = wait; # on first exit, kill rest print "$who{$pid} exited\n"; kill 15, keys %who; print "$who{$pid} exited\n" while ($pid = wait) > 0;

$hasterminal determines which child listens to the terminal for commands.
Commands with a leading number are forwarded to that child.
If you type in

2 3 foobar

you get back

1 got 2 3 foobar 2 got 3 foobar 3 got foobar

showing that child 1 got the message, then forwarded it to child 2, who then forwarded it to child 3.

Any child can send directly to any other child using the pipes set up in the hash %pipes before the children are forked.

Note that this is just a prototype and should not be considered production code (there's a bit of cheating going on in it :)


In reply to Re: Child process inter communication by tybalt89
in thread Child process inter communication by smarthacker67

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.