【发布时间】:2023-03-20 10:59:01
【问题描述】:
我在 Windows 上使用 Strawberry perl。我有一些运行 script.pl 的 GUI.pl 应用程序,它运行 some.exe。 perl 脚本充当 GUI 应用程序和 some.exe 之间的 STDIN/OUT/ERR 的代理。 问题是我无法杀死链 GUI.pl -> script.pl -> some.exe 中的 some.exe 进程。
GUI.pl 将 TERM 发送到 script.pl
# GUI.pl
my $pid = open my $cmd, '-|', 'script.pl';
sleep 1;
kill 'TERM', $pid;
script.pl 捕获 'TERM' 并试图杀死 some.exe
# script.pl
$SIG{TERM} = \&handler;
my $pid = open my $cmd, '-|', 'some.exe';
sub handler {
kill 'TERM', $pid;
}
采用这种方案,some.exe的进程继续执行。我已经了解了很多关于信号的知识,但仍然不明白如何解决这个问题。
提前致谢。
它使用的解决方案之一是threads:
# script.pl
use threads;
use threads::shared;
$SIG{BREAK} = \&handler;
my $pid :shared;
async {
$pid = open my $cmd, '-|', 'some.exe'
}->detach;
# 1 second for blocking opcode. After sleep handler will be applied
sleep 1;
sub handler {
kill 'TERM', $pid;
}
【问题讨论】: