I create processes (on Windows) using something like $pid=system(1,'perl.exe "prog.pl" "x" "y" "z" 2>&1'); (i.e. the command always needs to invoke cmd.exe implicitly - I mention this because it might be relevant to the problem), and from time to time use waitpid($pid,WNOHANG) to poll this process. If waitpid returns -1, I assume that the process has been ended.

This works well so far. However, I now had once the following weird behaviour: waitpid returned -1, but the process terminated 8 seconds later (and actually did useful work during this 8 seconds). I found out because we do plausibility checks: The processes do some logging on their activities, and at the last step, the child process creates a certain file. The main process, after receiving -1 from waitpid, checks for the existence of this file, and issues an error message if this control file does not exist.

In this failure case, I see from the log file of the masterprocess, that at a certain time X waitpid had returned -1, but the control file did not exist, while the log file of the child process asserted that the control file had been created at time X+8 seconds.

From this I conclude that waitpid had signalled the end of the child process while this was still running. Now we know that in order to run the child process, a cmd.exe process must have been created (because the argument passed to system(1,...) uses shell quoting and redirection), and that cmd.exe then in turn creates a process for the command I had passed to system(). We also know that cmd.exe and its sub-process run in the same process group (at least this is how system(1,...) is supposed to work). The symptom I have experienced, looks as if cmd.exe had terminated earlier, before perl.exe "prog.pl".... was finished. This seems bizarre, and makes me a bit nervous. Any explanations for this?

-- 
Ronald Fischer <ynnor@mm.st>

In reply to waitpid returns -1 for still running child (Windows) by rovf

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.