[Israel.pm] Interprocess using Safe Pipes Opens
Shushu Inbar שושו ענבר
shushu.i at gmail.com
Thu Aug 2 19:27:20 EEST 2007
Hello Mongers !
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
(http://perldoc.perl.org/perlipc.html#Safe-Pipe-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 input 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";
More information about the Perl
mailing list