http://qs321.pair.com?node_id=55958

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

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?