Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 [ip(] \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 ( \n"; print "USAGE: perl [ip(] \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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://363316]
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-08-17 20:04 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (292 votes). Check out past polls.