I was testing out pid generation behavior on Mac OS X (10.3.4) when I noticed that the child process of a fork() was returning the parent process' parent id (1) instead of its own parent process id. This system is running Perl v.5.8.1RC3 for Darwin.
The behavior happens anywhere from 20-99% of the time, as you can see from the test output below. I was wondering if anyone else has experienced this behavior, I haven't been able to find anything on this via Google, the perldocs, etc.
The script:
#!/usr/bin/perl
use strict;
my $pid = fork();
die "Can't fork: $!" unless defined $pid;
if ($pid > 0) {
print "I'm the parent and my pid is $$. My child is $pid\n";
} else {
print "I'm the child and my pid is $$. My parent is " . getpp
+id() . "\n";
}
And the results:
$ while true; do perl fork.pl; sleep 1; done
I'm the parent and my pid is 2094. My child is 2095
I'm the child and my pid is 2095. My parent is 1
I'm the parent and my pid is 2097. My child is 2098
I'm the child and my pid is 2098. My parent is 1
I'm the parent and my pid is 2100. My child is 2101
I'm the child and my pid is 2101. My parent is 1
I'm the parent and my pid is 2103. My child is 2104
I'm the child and my pid is 2104. My parent is 2103
I'm the parent and my pid is 2106. My child is 2107
I'm the child and my pid is 2107. My parent is 1
^C
Thanks,