Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Child process inter communication

by tybalt89 (Monsignor)
on Jun 14, 2017 at 01:42 UTC ( [id://1192752]=note: print w/replies, xml ) Need Help??


in reply to Child process inter communication

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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192752]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2025-01-25 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (70 votes). Check out past polls.