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.


In reply to Re: Simple Parallel Processing Question by hbm
in thread Simple Parallel Processing Question by bichonfrise74

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.