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

Greetings Perl Monks! Using standard Perl on CentOS 5.5. I have an array that I create in main, a list of file names. The array is just a list of items to process. My problem is that after I fork (max of five forks), I try to set the $file variable to the result of shift (@fileArray). It does get the variable, but it's always the first element, which means it's not shifting. Is there something I have to do to make it shift?
#!/usr/bin/perl 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++) { $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"; my $file = shift(@fileArray); print "DEBUG: $file \n"; procFiles($forkCount, $$, $file); exit 0; } else { die "couldnt fork: $!\n"; } } ## we will stand by here and wait for each of those pids to complete ## The log entry for each fork will go here. foreach (@childArray) { my $procId = waitpid($_, 0); print "LOG: process number $procId\n"; } print "Main Terminated\n"; sub procFiles { ## simulate delay in processing dissimilar files srand(); my $minDelay = 1; my $maxDelay = 2; my $timeDelay = int(rand($maxDelay)) + $minDelay; ## get the fork number, process id and filename my $forkCount = $_[0]; my $procId = $_[1]; my $fileName = $_[2]; ## get the process id for this fork my $processIDnum = shift; ## sleep $num; print "Completed child process ($procId), fork number $forkCount, f +ile name $fileName\n"; ## retun the process id, which will be picked up return $processIDnum; }
I can get the first element of the array, but the shift appears to have no affect at all. All five forks have the same filename, so I suppose I've done something wrong here. Everything works aside from the file name, which does not change from How can I remove that element from the array once I have assinged the value to my $file variable? I can't get either of these two to work:
my $file = shift @fileArray; my $file = shift(@fileArray);
Thanks to everyone for the help! -AK

Replies are listed 'Best First'.
Re: assign variable and shift array
by fidesachates (Monk) on May 06, 2011 at 19:00 UTC
    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; } }
      Alternatively, shift before forking.
      my $file = shift(@fileArray); my $pid = fork(); ...
      That fixed the problem. Thanks very much for your help, I've been on this problem for a couple of days. Have a good weekend, -AK
Re: assign variable and shift array
by NetWallah (Canon) on May 06, 2011 at 18:55 UTC
    Each child process gets the "frozen" @fileArray, but it is "Copy on write" - which means that any child's attempt (or the parent's attempt) to modify the array creates a local copy.

    What you need to do is to pass each child an index to work on.

    You will probably also need to pass back completion information to the parent.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      I'm not sure how to proceed but: I guess it will work something like this: my $file = $fileArray[$i];. So, what will be the best way to update that array once I assign each value? I would think this has to be done inside that loop. Thanks very much for your assistance.
Re: assign variable and shift array
by ikegami (Patriarch) on May 06, 2011 at 21:02 UTC