Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here's my second guess, using named pipes (fifos);

#!/usr/bin/perl # http://perlmonks.org/?node_id=1192662 use strict; use warnings; use IO::Select; use POSIX qw(mkfifo); my $childcount = 3; my $hasterminal = 1; my %who; my $fifodir = '/tmp'; for my $me (1 .. $childcount) { if( my $pid = fork ) # parent { $who{$pid} = $me; } elsif( defined $pid ) # child { my $mypath = "$fifodir/$$.fifo"; mkfifo( $mypath, 0700 ) or die "$! on making mypath"; open my $fifo, '+<', $mypath or die "$! opening $mypath"; my $sel = IO::Select->new($fifo); $me == $hasterminal and $sel->add(*STDIN); while(1) { for my $handle ($sel->can_read) { defined( my $command = <$handle> ) or exit; print "$$ got $command"; if( $command =~ /^(\d+)\s+(.*\n)/ ) { my $otherpath = "$fifodir/$1.fifo"; if( -p $otherpath and open my $otherchild, '>', $otherpath ) { print $otherchild $2; close $otherchild; } else { warn "child $1 does not have a fifo\n"; } } } } } 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; unlink map "$fifodir/$_.fifo", keys %who; print "$who{$pid} exited\n" while ($pid = wait) > 0;

Enter a pid followed by white space and a message, it will forward the message to that pid.

Example - enter:

25910 somemessage

to get

25908 got 25910 somemessage 25910 got somemessage

Of course, using your actual pids.


In reply to Re^2: 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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-29 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found