in reply to Simple Parallel Processing Question

There's this, if you have IO::Socket:
# Perl Cookbook, 1st Edition, pg. 626 use strict; use IO::Socket; use Symbol; use POSIX qw(:signal_h :errno_h :sys_wait_h); my $PREFORK = 2; # number of kids to maintain my %kids = (); # child process ids my $kids = 0; # number of children my @jobs = qw(a b c d e f g h); sub REAPER { $SIG{CHLD} = \&REAPER; my $pid = waitpid(-1, &WNOHANG); if ($pid == -1) { # ignore - no child waiting } elsif (&WIFEXITED($?)) { $kids--; delete($kids{$pid}); } else { # false alarm } $SIG{CHLD} = \&REAPER; # in case of unreliable signals } sub HUNTSMAN { local($SIG{CHLD}) = 'IGNORE'; kill 'INT' => keys %kids; exit; } sub HEADCOUNT { $PREFORK +=5; print "I have $kids kids; increasing to $PREFORK...\n"; } # install signal handlers $SIG{CHLD} = \&REAPER; $SIG{INT} = \&HUNTSMAN; $SIG{HUP} = \&HEADCOUNT; print "starting $PREFORK users...\n"; for (1 .. $PREFORK) { &make_new_child(shift(@jobs)); } # and maintain population while (scalar @jobs) { # or 'while (1)' for perpetual sleep; # wait for a signal for ($kids .. $PREFORK) { &make_new_child(shift(@jobs)); } } sub make_new_child { my $pid; my $sigset; sleep 3; # block signal for fork $sigset = POSIX::SigSet->new(SIGINT); sigprocmask(SIG_BLOCK, $sigset) or die("Can't block SIGINT for fork +: $!\n"); die "fork: $!" unless defined ($pid = fork); if ($pid) { # parent records child and returns sigprocmask(SIG_UNBLOCK, $sigset) or die("Can't unblock SIGINT f +or fork: $!\n"); $kids{$pid} = 1; $kids++; return; } else { # child must *not* return from this subroutine $SIG{INT} = 'DEFAULT'; # make it kill us as before $SIG{HUP} = 'IGNORE'; # only parent needs this #unblock signals sigprocmask(SIG_UNBLOCK, $sigset) or die("Can't unblock SIGINT f +or fork: $!\n"); # do something my $job = shift; print "I am $$, doing job $job\n"; sleep 10; exit; } }

I used basically the same to stress an application by perpetually spawning connections and adding heat with HUP.

Update: Sorry! When I cut-and-pasted this, I missed three essential lines. See the three lines after the comment, "Install signal handlers".

Update2: I clarified my reference to the Cookbook. I now have the 2nd Edition, which doesn't have the same recipe.

Replies are listed 'Best First'.
Re^2: Simple Parallel Processing Question
by bichonfrise74 (Vicar) on Jan 22, 2009 at 05:41 UTC
    Hi,

    I tried to run your script but I'm getting this output...
    starting 2 users... I am 23687, doing job a I am 23688, doing job b
    I looked into the code and it should continue doing job c, d, etc... but it simply stopped after this. Any thoughts?