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,

-fp

In reply to Broken getppid() on Darwin/OSX by fuzzyping

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.