Thanks in large part to Chipmunk's efforts, I was able to figure out a few optimizations to make this work without resorting to the 'full-blown forking process handler' approach. Perhaps this is oversimplified and I missed something important; if that's the case, please please let me know before I put this code into production.
After correcting the logic to use the child status ($?) to increment/not increment the counter for status, I fixed the forking so that the parent wouldn't wait for each child, to improve speed. To prevent the resulting fork()bomb, I devised a clever (to me) way to subdivide the array @targets into chunks that I could use to get exactly as many clients as I needed, but no more. I then used a global variable ($i) to index the array so that each child got called on a separate target, and no target got checked more than once.
If anyone has any further optimizations, please share!
$maxprocs = 50;
my $prox = int(scalar(@targets)/$maxprocs);
my $prox_r = int(scalar(@targets)%$maxprocs);
my $i = 0;
if ($prox) # call the forking routine as many times as required to
+get most of the targets
{
for ( 1 .. $prox )
{
&breed($maxprocs);
}
}
if ($prox_r) # Then call the forking routine but this time only enoug
+h to catch the modulo
{
&breed($prox_r);
}
# Calculate results and print to STDOUT
my $success = ( $main::avail / scalar(@targets) ) * 100; # return r
+esults as a percentage
$success = sprintf('%.2f',$success); # rounded
+to 2 decimals
print "$success\n";
#########################
sub breed ($)
#########################
{
my $loop = shift;
for ( 1 .. $loop ) #make a bunch of kids
{
if ($pid=fork) {
push @pids, $pid;
$i ++;
} elsif (defined $pid) {
my $next = @targets[${i}];
&check_status($next);
exit;
}
}
foreach my $childpid (@pids) # wait for all kids to return before
+ continuing
{
waitpid($childpid, 0);
$main::avail += ! $?; # Increment the master counter, 0 means su
+ccess, non-zero means failure
}
}
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.