1

So I understand that Windows does not support the Unix fork-exec model and instead spawns processes. However, Strawberry Perl's fork emulation produces children with negative PIDs. These PIDs appear to be consistent, but I don't understand why they are negative or, really, how Perl is emulating Unix fork.

use strict;
use warnings;

my $cpid = fork();

if ($cpid == 0) {
    printf "%s\n", "I'm the child, pid is $$";
} else {
    printf "%s\n", "I'm the parent, pid is $$, cpid is $cpid";
}

This produces something similar to:

I'm the parent, pid is 3428, cpid is -2600
I'm the child, pid is -2600
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65

2 Answers2

5

Most of the details are in perlfork, but to answer your specific questions, Pseudo-processes for Perl on Windows are actually implemented as threads. You should interpret a positive PID to be the actual PID of the original thread and negative PIDs are actually thread IDs (negated, of course).

tjd
  • 4,064
  • 1
  • 24
  • 34
  • "You should interpret a positive PID to be the actual PID of the original thread and negative PIDs are actually thread IDs" Is this documented anywhere? – ThisSuitIsBlackNot Nov 02 '15 at 22:09
  • I read perlfork and couldn't tell where the negative numbers are coming from, only that mysterious things happen with `wait` if you get a pseudo-pid of -1 – Greg Nisbet Nov 02 '15 at 22:36
3

If positive PIDs were used for the fake processes created by fork (which are actually threads), they could conflict with PIDs of actual processes.

ikegami
  • 367,544
  • 15
  • 269
  • 518