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

I have a set of code below:
use Parallel::ForkManager qw( ); my $pm = Parallel::ForkManager->new(int(@files/4)); for my $file (@files) { my $pid = $pm->start and next; ... do something with $file ... $pm->finish; # Terminates the child process }
I want to have something like this but without forkmanager, just with forking in perl.. I have an example of the EXACT type of forking i want below, but i cant seem to figure out how to use it for my own purposes. *note im new to perl Heres the code:
#!/usr/bin/perl use Net::IP; use LWP::UserAgent; use vars qw( $PROG ); ( $PROG = $0 ) =~ s/^.*[\/\\]//; #Usage if ( @ARGV == 0 ) { print "Usage: ./$PROG [START-IP] [END-IP] [THREADS] [TIMEOUT] +[OUTPUT]\n"; exit; } my $threads = $ARGV[2]; my @ip_team = (); $|= 1; my $ip = new Net::IP ("$ARGV[0] - $ARGV[1]") or die "Invaild IP Rang +e.". Net::IP::Error() ."\n"; print "[!]Starting with $threads threads\n[!]Scanning $ARGV[0] to $ARG +V[1]\n"; while ($ip) { push @ip_team, $ip++ ->ip(); if ( $threads == @ip_team ) { Scan(@ip_team); @ip_team = () } } Scan(@ip_team); sub Scan { my @Pids; foreach my $ip (@_) { my $pid = fork(); die "Could not fork! $!\n" unless defined $pid; if (0 == $pid) { my $ua = LWP::UserAgent->new; $ua->timeout($ARGV[3]); my $response = $ua->get("http://$ip"); if ($response->is_success) { print "Found one $ip!\n"; open (MYFILE, ">>$ARGV[4]"); print MYFILE "$ip\n"; close (MYFILE); } else { die "[-] No Webserver Found!"; } exit } else { push @Pids, $pid } } foreach my $pid (@Pids) { waitpid($pid, 0) } }

Replies are listed 'Best First'.
Re: Forking list in perl without Parallels ForkManager
by hippo (Archbishop) on Apr 05, 2014 at 09:33 UTC

    As you say you are new to perl, here are some handy hits:

    1. use strict;
    2. use warnings;
    3. Employ a consistent indentation policy
    4. Don't refer to processes as "threads" when they aren't actually threads.

    Having said that, it isn't clear from your post what your "own purposes" might be or in what way your sample code fails to address them. Nor is it clear why you aren't using Parallel::ForkManager for this when it would appear to suit the bill. Some further explanation and detailed statement of the problem would be better.

Re: Forking list in perl without Parallels ForkManager
by zentara (Cardinal) on Apr 05, 2014 at 11:20 UTC
    I have an example of the EXACT type of forking i want below, but i cant seem to figure out how to use it for my own purposes. *note im new to perl Heres the code:

    This isn't a code writing service, but here is a very good set of routines written by the esteemed monk Abigail, which is very Perl4ish, but works great. Modify them to suit your EXACT needs. :-)

    #!/usr/bin/perl #by Abigail of perlmonks.org #Some times you have a need to fork of several children, but you want +to #limit the maximum number of children that are alive at one time. Here #are two little subroutines that might help you, mfork and afork. They + are very similar. #They take three arguments, #and differ in the first argument. For mfork, the first #argument is a number, indicating how many children should be forked. +For #afork, the first argument is an array - a child will be #forked for each array element. The second argument indicates the maxi +mum #number of children that may be alive at one time. The third argument +is a #code reference; this is the code that will be executed by the child. +One #argument will be given to this code fragment; for mfork it will be an + increasing number, #starting at one. Each next child gets the next number. For afork, the + array element is #passed. Note that this code will assume no other children will be spa +wned, #and that $SIG {CHLD} hasn't been set to IGNORE. mfork (10,10,\&hello); sub hello{print "hello world\n";} print "all done now\n"; ################################################### sub mfork ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { wait unless $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($c) unless $pid; } 1 until -1 == wait; } ################################################## sub afork (\@$&) { my ($data, $max, $code) = @_; my $c = 0; foreach my $data (@$data) { wait unless ++ $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($data) unless $pid; } 1 until -1 == wait; } #####################################################

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh