When the p5p guys added the system 1, ... asynchronous spawn, they wanted it to operate as much like the *nix equivalent as possible. Under *nix, a spawned process will hang around after it completes, as a zombie, until someone calls wait or waitpid to retrieve its exit status and clean it up.
Win32 didn't provide a direct equivalent of this facility, so they chose to emulate it within the Perl process. To do this, they store the process handles and pids in an internal data structure, so that the can emulate wait and waitpid.
## From win32.c
if (mode == P_NOWAIT) {
/* asynchronous spawn -- store handle, return PID */
ret = (int)ProcessInformation.dwProcessId;
if (IsWin95() && ret < 0)
ret = -ret;
w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
w32_child_pids[w32_num_children] = (DWORD)ret;
++w32_num_children;
}
However, the mechanism they chose to use for the wait & waitpid emulation has a limit of 64 elements, so when you try to spawn a 65th process, without having called wait or waitpid in the interim, they reject the attempt because the fixed sized tables (internal to the Perl process) have temporarially run out of resources.
case P_NOWAIT: /* asynch + remember result */
if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
errno = EAGAIN;
ret = -1;
goto RETVAL;
}
So, if you need to start more than 64 processes this way, you will need to clean up your zombies using wait as you go.
However, there is another alternative built-in. usage: Win32::Spawn($cmdName, $args, $PID).
C:\test>perl -le "for (1..100) {Win32::Spawn( $ENV{ComSpec},'/c echo>n
+ull', $pid );print qq[$_\tpid:$pid\t],$?==-1 ? $! : 'OK' };"
1 pid:5492 OK
2 pid:5256 OK
3 pid:1860 OK
...
93 pid:2236 OK
94 pid:2308 OK
95 pid:4012 OK
96 pid:4796 OK
97 pid:4636 OK
98 pid:4780 OK
99 pid:244 OK
100 pid:4800 OK
This doesn't attempt to emulate *nix mechanisms, and so doesn't have any limitations that arise from doing so. It also returns the real pid directly which can be useful.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|