I have been messing around with forking for a script I have in mind and I'm having trouble getting the basic flow going correctly. I have been doing a lot of reading on the subject but I guess if you have never worked with forking before it's a bit difficult to grasp.
Anyway, I have 10 tasks I want to do but I only want to do 5 of them at a time. If I have 5 running and 1 finishes, I want it to begin working on the 6th task and so on until all 10 tasks are complete.
Here is some very basic code I have been playing with but as you can see, it isn't complete. Perhaps someone can assist?
use strict;
# use this array to simulate 10 tasks
my @array = qw(zero one two three four five six seven eight nine ten);
my $count=0;
my $num_of_tasks = 5;
for (1..$num_of_tasks) {
my $pid = fork();
if ($pid) {
$count++;
waitpid($pid,0);
} elsif ($pid == 0) {
test();
} else {
die "couldn’t fork: $!\n";
}
}
print "Done\n\n";
sub test {
# do the work here
print "text=$array[$count]\n";
sleep 1;
exit(0);
}
Thank you.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.