My code uses fork() and waitpid(-1,&WNOHANG) to spawn multiple children. Each of these children uses $pid = system(1,"start $executable $args") to start a program. This program sometimes hangs, and I would like to be able to kill it after some length of time. The $pid value returned by my system command is not the one I need to kill the process (From my research I believe this has to do with a shell?) and I'm stuck as to how to get around this. I've experimented with a piped open and had no luck, but I'm not sure if I'm doing it correctly. Any suggestions as to how I can get a PID so that I can kill the process later if needed? Sorry if this has already been answered, but I'm fairly new to coding and Perl, and I've looked at several similar questions and can't figure out how to apply them to get a workable solution. Also, my OS is Windows 7 64 bit and I can't use any modules that aren't standard. Thanks in advance for any advice!

Update: Thanks for all the replies! I found a not so elegant workaround that completely avoids the problem of getting the PID. For reasons other than the PID problem, I have to copy the executable to a new directory before I run it, and I now give the duplicate a unique name each time I copy it. Then, if it is still going after a certain amount of time, I kill it with start("Taskkill /IM $unique_executable /F"). A little bit clunky, but it works for my purposes.


In reply to How do you get the PID of an external process? by BamaMonk

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.