Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Success!

by Clownburner (Monk)
on Mar 22, 2001 at 05:03 UTC ( [id://66203]=note: print w/replies, xml ) Need Help??


in reply to Tallying results from Forking processes...

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://66203]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found