Why does the following code:
#!/usr/bin/perl
use strict;
use warnings;
use Parallel::ForkManager;
my $pm = new Parallel::ForkManager(5);
my @all_data = ('a','b','c','d','e','f');
foreach my $data (@all_data) {
# Forks and returns the pid for the child:
my $pid = $pm->start and next;
print "Hello $pid\n";
$pm->finish; # Terminates the child process
}
$pm->wait_all_children;
print:
Hello 0
Hello 0
Hello 0
Hello 0
Hello 0
I am new to Perl and I am trying to catch up on multiprocessing in Perl
From the docs for the
start
method:As it happens, the
fork
function does the same, which is whystart
mirrors it.The parent may need the child PID to control the child – sending signals and stuff – but the child knows its own PID via the
$$
variable:Example output: