Where did you find the code example you posted?
I edited first example (in SYNOPSIS) here.
I should have expected this since system probably starts it's own child process.
Exactly!
or if the actual offset is subject to system timing?
yep.
having said all that I am really not sure how to properly work around this.
Why do you need exactly the PID which do the work?
in general this problem is hard to solve..
You can use something like this:
use Parallel::ForkManager;
$pm = new Parallel::ForkManager(100);
my @all_data = qw/1/;
foreach $data (@all_data) {
# Forks and returns the pid for the child:
my $pid = $pm->start;
print "PID $pid\n";
$pid and next;
print "$data", "\n";
my $pid2 = fork();
unless ($pid2) {
exec $^X, '-e', 'while(){}';
}
print "Real PID $pid2\n";
waitpid $pid2, 0;
$pm->finish; # Terminates the child process
}
$pm->wait_all_children;
i.e. use "exec", "fork" and "wait/waitpid" calls.
Note that I don't recommend putting it into production until you fully understand how those functions works.
Also this example won't work if you replace
exec $^X, '-e', 'while(){}';
with
exec "perl -e 'while(){}'";
those calls are equivalent, but former calls PERL, and later one calls SHELL, which will call PERL.
so with later one you'll get wrong pid again (PID of shell)
Also, you really cannot control if scripts, that you call are calling other scripts or other processes or no
Thus this task is hardly solvable.
I propose explain you business requirements (exactly why you need that PID who do works) and someone might suggest something. |