gpt4 book ai didi

linux - 为什么 SIGINT 没有在这里被捕获?

转载 作者:IT王子 更新时间:2023-10-29 00:34:16 25 4
gpt4 key购买 nike

这是怎么回事?我以为 SIGINT 会被发送到前台进程组。

(我想,也许 system() 正在运行一个 shell,它正在为子进程创建一个新的进程组?有人能证实这一点吗?)

% perl
local $SIG{INT} = sub { print "caught signal\n"; };
system('sleep', '10');

然后按 ctrl+d,然后立即按 ctrl+c,注意永远不会打印“捕获信号”。

我觉得这是一件简单的事情...无论如何要解决这个问题?问题是,当通过系统运行一堆命令时,会导致按住 ctrl+c 直到所有迭代完成(因为 perl 永远不会获得 SIGINT)并且相当烦人...

如何解决这个问题? (我已经直接使用 fork() 进行了测试,我知道这是可行的……目前这不是一个可接受的解决方案)

更新:请注意,这与“休眠”无关,只是该命令需要任意长的时间才能运行,这相当可观比它周围的 perl 还要多。如此之多以至于按 ctrl+c 被发送到命令(因为它在前台进程组中?)并且以某种方式设法永远不会被发送到 perl。

最佳答案

来自 perldoc system :

Since SIGINT and SIGQUIT are ignored during the execution of system, if you expect your program to terminate on receipt of these signals you will need to arrange to do so yourself based on the return value.

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"

If you'd like to manually inspect system's failure, you can check all possible failure modes by inspecting $? like this:

if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}

Alternatively, you may inspect the value of ${^CHILD_ERROR_NATIVE} with the W*() calls from the POSIX module

关于linux - 为什么 SIGINT 没有在这里被捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com