Hello Monks !
I am trying to write some code that will handle several child process at once.
I am working in Linux/Solaris environment, using Perl 5.6.1.
After reading all of the IPC documentation I could find, I came to the
decision that using
Safe Pipes Opens
would be the
right answer for me, since the parent process suppose to update the
childs with information during the execution.
I created a skeleton to try and see how it works, but for some reason
I have a problem - only the last child behaves properly and get the
input sent by the parent.
Help would be appreciated,
Below is the code itself (I guess a little ugly...).
TIA,
Shushu
use strict;
use warnings;
use Carp;
use Data::Dumper;
use English '-no_match_vars';
use FileHandle;
$| = 1;
my $no_of_childs = 3;
my @fh;
my $index = 0;
while ($index < $no_of_childs) {
print "PID $PID\n";
my $pid;
$fh[$index]{'fh'} = new FileHandle;
$fh[$index]{'fh'}->autoflush(1);
$pid = $fh[$index]{'fh'}->open("|-");
if ($pid) { # parent
$fh[$index]{'pid'} = $pid;
print "parent pid $PID creating child " . $index+
++ . "\n";
} else { # child
print "child pid $PID creating child " . $index++ . "\n
+";
while (1) {
# Here we will do the child work, and read inpu
+t from STDIN
print "$PID checking for input\n";
my $line = <STDIN>;
if ($line) {
print "$PID got something from father !
+!! $line\n";
}
sleep 1;
}
exit;
}
}
print Dumper \@fh;
my $fh;
sleep 5;
$fh = $fh[0]{'fh'};
print "send hi to child 1\n";
print $fh "shalom yeled 1";
sleep 2;
$fh = $fh[1]{'fh'};
print "send hi to child 2\n";
print $fh "shalom yeled 2";
sleep 2;
$fh = $fh[2]{'fh'};
print "send hi to child 3\n";
print $fh "shalom yeled 3";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.