So I've been away from perl for a long time, now I have the chance to get back and I'm jumping all in. But to the problem, I've been playing with a program to test some equipment and essentially it'll fork a bunch of children and have them do a task eg. check a webpage. The parent process writes the webpage url for the child to check. This I all have working fine based on some examples from Proc::Fork.
What I'm having alot of trouble with is getting the children to reply back to the parent so that I can aggregate stats and such. I have a feeling I'm not reading from the pipes properly.
I've boiled down the code to the basics. I've commented out the parts that I believe are wrong.
#!/usr/bin/perl
use strict;
use Proc::Fork;
use IO::Pipe;
my $num_children = shift; # How many children we'll create
my %children; # Store connections to them
my %childrenin; # Store connections from children
$SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies
# Spawn off some children
for my $num ( 1 .. $num_children ) {
# Create a pipe for parent-child communication
my $pipe = IO::Pipe->new;
my $pipein = IO::Pipe->new;
# Child simply echoes data it receives, until EOF
run_fork {
child {
print "[$num] Child Start\n";
$pipe->reader;
$pipein->writer;
my $data;
my $dataout = $pipein;
while ($data = <$pipe>)
{
chomp($data);
print STDERR "Child [$num]: $data \n";
print $dataout "[$num]: $data \n";
}
exit;
}
};
# Parent here
$pipe->writer;
$children{$num} = $pipe;
$pipein->reader;
$childrenin{$num} = $pipein;
}
# Send some data to the kids
for my $i ( 1 .. $num_children ) {
my $child = $children{$i};
print $child "Line 1\n";
}
# Read data from children
# this is where I believe I'm going wrong.
=comment
for my $i ( 1 .. $num_children ) {
my $childin = $childrenin{$i};
my $datain;
while ( $datain = <$childin> )
{
print "[Parent] $datain \n";;
}
}
=cut
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.