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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: spawning windows children (revisted) by BrowserUk
in thread spawning windows children (revisted) by Random_Walk

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.