Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found