Hi Monks, brothers,
I just fall into problems with forking. I have to write some pinging machine which will ping about 300 devices in very short time about 1-2 minute. So I need to fork, especially because I can't wait for ping timeout. To ensure max efficiency I tried create some MAX number of child procceses, each to ping one device and save output values into File::Cache to be accesible for independeng aplication which is running as client. I am trying to control proccesses to imediately start new one, if one of childs is done. So keep in each time MAX number of procceses running. Not so easy as I thought. Here is my code:
#!/usr/bin/perl
use File::Cache;
$address_path = "C:/Temp/address.txt";
$command = "ping.exe";
$max_processes = 2;
$cache = new File::Cache( { namespace => 'ping',
expires_in => 3600,
filemode => 0666 } );
if ( not open(ADDRESS, $address_path) ) {
die "Cannot open source file";
die;
}
$i = 0;
while ( $temp = <ADDRESS> ) {
chomp($temp);
push( @address, $temp );
$i++;
}
$processes = 0;
$i = 0;
while ( $i < @address ) {
if ( $processes < $max_processes ) {
if ( $pid = fork() ) {
$processes++;
$i++;
}
elsif ( defined $pid ) {
$command .= " $address[$i]";
@list = qx/$command/;
if ( $list[10] =~ /\s(\d+)\%/ ) { $temp = $1; }
$cache->set( $i, $address[$i]. ";" . $temp );
exit;
}
}
else {
wait();
$processes--;
}
}
As you can see it's very simple. After start script will read config file - each line is one ip address, and then it will fork to child ping to address. If there are more than max procceses running it will wait to children death and than it will continue.
Unfortunately this script don't do what I suppose. :-) have anybody idea how to get it to work? I also visit this node to get isnpired but I need litle bit more efficient way.
Thanks for help. Litin.
Li Tin O've Weedle
mad Tsort's philosopher
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.