Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

spawning windows children (revisted)

by Random_Walk (Prior)
on Jun 18, 2007 at 12:01 UTC ( #621768=perlquestion: print w/replies, xml ) Need Help??

Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

Honourable monks of Perlish persuasion,

In response to detached process in windows I got what I thought was the perfect, simple solution to spawning detached children, suggested by BrowserUK. Unfortunately now I'm finding when the parent runs for a while it starts failing with Resource temporarily unavailable

Here is a simple demonstration (Windows 2000 'Pro', Perl 5.8.8 +50 patches, Active State)

C:\> perl -le "for (1..100) {system 1,'echo>null';print \"$_\t\",$?==-1 ? $! : 'OK' };" 1 OK 2 OK . . . 63 OK 64 OK 65 Resource temporarily unavailable 66 Resource temporarily unavailable . . . 99 Resource temporarily unavailable 100 Resource temporarily unavailable

Is this a known windows 'feature' and can it be fixed easily or am I really best just accepting a bigger implementation variance between my Linux/Windows code and throwing in some if $^O eq 'MSWin32' require Win32::Process; as suggested in the same thread by bart

Thanks again for all your help,

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: spawning windows children (revisted)
by BrowserUk (Patriarch) on Jun 18, 2007 at 13:58 UTC

    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.
Re: spawning windows children (revisted)
by Jenda (Abbot) on Jun 18, 2007 at 13:37 UTC

    You should wait() for the kids from time to time. You can only spawn 64 processes at a time, then you have to wait() and reap some of the children that already exited.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://621768]
Approved by almut
Front-paged by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2023-03-31 15:58 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (76 votes). Check out past polls.