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 :)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|