Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Parallel::ForkManager Problem

by broquaint (Abbot)
on Jun 11, 2004 at 05:46 UTC ( #363316=note: print w/ replies, xml ) Need Help??


in reply to Parallel::ForkManager Problem

This is because you are trying to modify the @arr in the parent process from the child process, which won't work as the child process is only modifying its own copy of @arr. You either need to communicate to the parent process the information or store the information somewhere than can be accessed by the parent process. A simple solution would be to have the children write to a file e.g

use strict; use warnings; use Fcntl ':flock'; use Parallel::ForkManager; my $PORT_NO = '80'; # Default port of HTTP my $SERVICE = "http"; my $pm = new Parallel::ForkManager(20); # At any instant maximum 20 pr +ocesses can run simultaneously my $ip; # Stores the IP address supplied by user if(!$ARGV[0]) { print "Program intended to check the machines where webserver is run +ning. \n"; print "USAGE: perl wst.pl [ip(xxx.xxx.xxx)] \nInput only 3 octets\n" +; exit; } if($ARGV[0] =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}$/) { $ip = $ARGV[0]; } else { print "Enter IP in valid format (xxx.xxx.xxx) \n"; print "USAGE: perl wst.pl [ip(xxx.xxx.xxx)] \n"; exit; } ## file to write to open(my $fh, '+>', "ipaddresses.txt") or die "ack: $!"; foreach my $i (1..254) { my $ip_add = sprintf("%s.%s",$ip,$i); my $pid = $pm->start and next; my $val = `nmap -sT -p $PORT_NO $ip_add | grep $SERVICE`; if ($val) { my $test = (split(/\s+/,$val))[1]; if ($test =~ m/open/) { flock $fh, LOCK_EX; print "$ip_add\n"; flock $fh, LOCK_UN; } } $pm->finish; } print "reaping....might take some time\n"; $pm->wait_all_children; seek $fh, 0, 0; print "Web Server is running at : ", <$fh>, "\n"; exit 0;
So there I've replaced the push with a simple write to a file and once the children are finished the parent process simply reads the file. See. flock and seek for more info on the functions used and perlipc for info on communication between processes.
HTH

_________
broquaint


Comment on Re: Parallel::ForkManager Problem
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-08-29 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls