为什么 SIGINT 没有在这里被捕获?
这是怎么回事?我以为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。
Whats going on here? I thought SIGINT would be sent to the foreground process group.
(I think, maybe, that system() is running a shell which is creating a new process group for the child process? Can anyone confirm this?)
% perl
local $SIG{INT} = sub { print "caught signal\n"; };
system('sleep', '10');
Then hit ctrl+d then ctrl+c immediately and notice that "caught signal" is never printed.
I feel like this is a simple thing... anyway to work around this? The problem is that when running a bunch of commands via system results in holding ctrl+c until all iterations are completed (because perl never gets the SIGINT) and is rather annoying...
How can this be worked around? (I already tested using fork() directly and understand that this works... this is not an acceptable solution at this time)
UPDATE: please note, this has nothing to do with "sleeping", only the fact that the command takes some arbitrary long amount of time to run which is considerably more than that of the perl around it. So much so that pressing ctrl+c gets sent to the command (as its in the foreground process group?) and somehow manages to never be sent to perl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 perldoc 系统:
from perldoc system:
我不太明白你想要在这里实现什么......但是你是否尝试过简单地比较:
你能解释一下你想要达到什么效果,以及为什么要调用 shell 吗?是否可以直接调用外部程序而不涉及shell?
I don't quite get what you're trying to achieve here... but have you tried simply comparing to:
Can you explain what effect you're trying to go for, and why you are invoking the shell? Can you simply call into the external program directly without involving the shell?