BamaMonk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How do you get the PID of an external process?
by BrowserUk (Patriarch) on Sep 25, 2013 at 02:23 UTC | |
by McA (Priest) on Sep 25, 2013 at 05:50 UTC | |
by BrowserUk (Patriarch) on Sep 25, 2013 at 07:25 UTC | |
by McA (Priest) on Sep 25, 2013 at 11:18 UTC | |
by syphilis (Archbishop) on Sep 25, 2013 at 12:42 UTC | |
by soonix (Canon) on Sep 25, 2013 at 07:31 UTC | |
Re: How do you get the PID of an external process?
by syphilis (Archbishop) on Sep 24, 2013 at 23:37 UTC | |
Re: How do you get the PID of an external process?
by McA (Priest) on Sep 24, 2013 at 23:21 UTC | |
by salva (Canon) on Sep 25, 2013 at 06:41 UTC |