O High Masters, I humbly implore you to share your wisdom:
I have apparently gained enough perl knowledge to become a serious threat to myself and others. I am trying to create a program that will fork a number of children, run a subroutine for each child (in parallel), and then, once all the children have returned, tally how many children come back successfully. (return = 1)
My logic is apparently flawed; my code will work as advertised *EXCEPT* the tallying part - each child keeps its own set of variables and I haven't figured out how to get the child to increment a parent variable.
I am constrained to avoid using additional CPAN modules if at all possible, since this has to be rolled out to a large number of client machines..
my $prox = scalar(@targets) -1;
my ($i, $ret, $pid, @pids);
for ($i = 0; $i <= $prox; $i++) {
if ($pid=fork) {
push @pids, $pid;
waitpid($pid,0);
} elsif (defined $pid) {
my $next = @targets[${i}];
$ret = &check_status($next);
if ($ret) { $avail ++; }
exit;
}
}
my $success = ( $avail / scalar(@targets) ) * 100;
$success = sprintf('%.2f',$uptime);
print "$success% successful\n";
Also, I would like to know if there is a way to prevent this from becoming a fork() bomb if the @targets array contains a LOT of targets (it could easily contain 500+ targets), so how do I limit the number of children while still covering all the @targets?
I've looked through the documentation on fork() and wait() and waitpid() and searched this site, but I'm still not finding anything that helps.
I apologize in advance if I'm asking a blind-stupid question, I've been up for too long and my caffeine levels are dangerously low.
Signature void where prohibited by law.
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.