I haven't had need for Unix IPC in a while, so I think there's some easy way to what I need that I'm overlooking. So anything that works is great, but especially clever solutions as always, get bonus points.

I have a load-testing script that makes a specific web request repeatedly until a stop condition is satisfied (the server is supposed to eventually get fed up and stop sending the normal response when too many requests come in from the same IP... and yes, I know that's a terrible way to fend off a DoS). I'd like to be able to run many copies of the script in parallel without actually opening ten xterms and running the script in each. This is easy with fork as long as the children and parent don't need to communicate, but I'd like to have each child report back to the parent periodically saying how many requests it's made so far. The parent would then sum up the requests made from the children and report to stdout on how many requests had been made in total. Execution order of the children isn't relevant.

Looking at perlipc and answers to older questions, I'm thinking about something in the neighborhood of this:

# ... setup code my $NUM_CHILDREN = 10; my $total_count; my @pids; for (my $i = 0; $i < $NUM_CHILDREN; $i++) { $pids[$i] = open(CHILD, "-|"); if ($pids[$i]) { next; } elsif (defined $pid) { # child handler, $pid=0 my $count = 0; my $done = 0; while (!$done) { # make web request; set $done somewhere $count++; } } else { die "Fork failed at number $i: $!\n"; } }
As written, the parent process spawns all the children and exits, which 1) doesn't print hit counts like I want, and 2) doesn't allow me to cancel all the children at once, which I'd like to do. I'm unsure of where and how to send $count up to the parent from the children, and how the parent would make use of that. I'm also not quite sure how to have the parent kill the children when it dies abnormally. Any clues?

In reply to Methods for Asynchronous IPC by athomason

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.