AK7033, First you need an explanation. You're forking which means all variables are copied into their separate spaces. This means that when you shift the array, each forked process shifts their own copy of the array. That is why you think the array doesn't shrink. To correctly get the different file names in the array, I've used a variable called $arrayIterator which changes every time you fork. This will allow you to access the index in the array for that forked process. Happy coding!
use strict;
use warnings;
print "Begin Main\n";
## we store the child process in this array
my @childArray;
## path to the files go in this array. It could be one or a dozen
## and is determined by another process
our @fileArray = qw(fileone.txt filetwo.txt filethree.txt);
## This is the number of forks we will allow
my $forkCeiling = "5";
my $pid;
my $fileName;
for ( my $forkCount = 1; $forkCount <= $forkCeiling; $forkCount++) {
my $arrayIterator = $forkCount-1;
$pid = fork();
if ($pid) {
push(@childArray, $pid);
} elsif ($pid == 0) {
# call the download subroutine and pass the number of
++forks, parent pid and array
print "Sending fork number $forkCount to the \"process
+ files\" subroutine...\n";
print $arrayIterator."\n";
my $file = $fileArray[$arrayIterator];
if(defined $file)
{
print "DEBUG: $file \n";
#procFiles($forkCount, $$, $file);
}
exit 0;
}
}
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.