my $log = IO::File->new(">$log_file_name");
$log->print("date()."--\n\n\n");
sub process {
my ($sub_dir, $file, $config, $log) = @_;
$log->print("-- Reading $file file\n");
}
my $pm = new Parallel::ForkManager($tc+1);
$pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; $tmp
+Files[$ident] = undef; } );
foreach my $i (0..$#tmp) {
# Forks and returns the pid for the child:
my $pid = $pm->start($i) and next;
$SIG{INT} = 'DEFAULT';
my $filename = $tmp[$i]->filename();
my $file = IO::File->new("<$filename") or die "Can't open $filen
+ame\n";
while((my $line) = $file->getline()) {
last unless defined($line);
chomp $line;
my ($dir, $file) = split(/\t/, $line);
# my $process = shift; is created above
# Calling this from another file
$process->($dir, $file, $config, $log);
}
$pm->finish; # Terminates the child process
}
Here 5 process is created and subroutine is called from another file which opens a log file write.
$process->($dir, $file, $config, $log);
When I print $pid always returns as 0.
How can I synchronize all the forked process to write to same log file or create a different log file for each process.
or
How can I create a threading process as the replacement for forking process