I simply want to open a compressed/uncompressed file in the background and produce a new file based on the processing done on the compressed file.
I could do it with Parallel::ForkManager
, but I believe that is not available.
I found this, but am not sure how to use it:
sub backgroundProcess {
my $file = shift;
my $pid = fork;
return if $pid; # in the parent process
&process_file($file);
exit; # end child process
}
sub process_file {
my $file = shift;
my $outFile = $file . ".out";
# ...here...
open( readHandle, "<", $file ) or die print "failed $!";
open( writeHandle, ">", $outFile ) or die "failed write $!";
# some processing here.....
# and then closing handles...
}
The loop:
foreach my $file (@filesToProcess) {
&backgroundProcess($file);
}
My questions:
- does the child process created in
backgroundProcess
run even after thereturn
occurs (in the linereturn if $pid
? - in
process_file
, how do I make sure a unique file handle is open for each file, or will "fork" take care of it? - in the loop (going through
@filesToProcess
), I want to run only a certain number of processes at a time, so how do I check if number of background process is equal to$LIMIT
, and then open a new one as an old one finishes?