开发者

How do I get the PID of the process I start with Perl's system()?

开发者 https://www.devze.com 2022-12-15 07:29 出处:网络
I\'m writing a Perl script that runs 4 simultaneous, identical processes with different input parameters (see backgroundhere - the rest of my question will make much more sense after reading that).

I'm writing a Perl script that runs 4 simultaneous, identical processes with different input parameters (see background here - the rest of my question will make much more sense after reading that).

I am making a system() call to a program that generates data (XFOIL, again see above link). My single-core version of this program looks like this:

eval{
    local $SIG{ALRM} = sub{die "TIMEOUT"};
    alarm 250;
    system("xfoil <command_list >xfoil_output");
    alarm 0;
};

if ($@){
    # read the output log and run timeout stuff...
    system('killall xfoil') # Kill the hung XFOIL. now it's a zombie.
}

Essentially, XFOIL should take only about 100 seconds to run - so after 250 seconds the program is hanging (presumably waiting for user input that it's never going to get).

The problem now is, if I do a killall in the multi-core version of my program, I'm going to kill 3 other instances of XFOIL, and those processes are generating data. So I need to kill only the hung instance, and this requires getting a PID.

I don't know very much about forks and such. From what I can tell so far, I would run an exec('xfoil') inside the child process that I fork. But the PID of the exec() will be different than the PID of the child process (or is it? It's a separate process so I'd assume it is, 开发者_如何学运维but again I've no experience with this..), so this still doesn't help when I want to forcefully kill the process since I won't have the PID anyway. How do I go about doing this?

Thanks a ton for your help!


If you want the PID, fork the process yourself instead of using system. The system command is mostly designed as a "fire and forget" tool. If you want to interact with the process, use something else. See, for instance, the perlipc documentation.

I think you've already looked at Parallel::ForkManager based on answers to your question How can I make my Perl script use multiple cores for child processes?

0

精彩评论

暂无评论...
验证码 换一张
取 消