Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Forking - max number of procceses at each time

by LiTinOveWeedle (Scribe)
on Feb 27, 2002 at 17:00 UTC ( [id://147966]=perlquestion: print w/replies, xml ) Need Help??

LiTinOveWeedle has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Forking - max number of procceses at each time
by Zaxo (Archbishop) on Feb 27, 2002 at 19:18 UTC
      THX for help. Another problem which I have, is that system command dnping (which is used in my real aplication instead of ping.exe) to DECnet ping, havn't implemented timeout. So if destination is unreachable the child proccess won't be terminated. To termintate any child procces from their parent after some timeout I can implement some array in which I can save $pid and time() after forking and then I will periodically check in parent procces if this timestamp + timenout is or isn't bigger than time(). But I don't know, if parent can termintate it's children procceses. Also no idea if I can do this by this way. And as adition because qx// do forking for system call, I have to grep pid of dnping proccess and kill it to. Seems really dificult. Or no?

      I wil be glad for any help or comment. THX. Litin

      Li Tin O've Weedle
      mad Tsort's philosopher

Re: Forking - max number of procceses at each time
by dhable (Monk) on Feb 27, 2002 at 18:45 UTC
    Ping a host with a large number of processes real fast? Hmmmmm...sounds like an evil use of Perl. Why would you want to do such a thing?
      It's simple, I can do it only in Perl. No skills in other languages. Sorry :-))) And sure, I am using regex to parse output from command (in fact it isn't ip ping but decnet ping on Decnet patched Linux).

      But THX anyway :-)

      Li Tin O've Weedle
      mad Tsort's philosopher

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://147966]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-21 01:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found