当信号()呼叫重置为sig_dfl时,为什么用户定义的信号处理程序一次又一次地执行?
显然,signal()
将重置为sig_dfl(默认值),这就是为什么在第一次导致默认行为发生后立即到达同一类型的另一个信号的原因。我的问题是:为什么只有在第二个信号立即发生时才会发生这件事?不管第二个信号是否立即考虑到信号已重置,是否应该发生?我很困惑。请澄清。
Apparently, signal()
resets to SIG_DFL (default) which is why another another signal of the same type arriving immediately after the first causes default behaviour to occur. My question is: why does this thing happen only if the second signal occurs immediately? Shouldn't it happen regardless of whether the second signal is immediate or not considering that the signal has been reset? I am mighty confused. Please clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数在C标准中仔细措辞以允许多种行为,主要是因为创建标准时,现有的
Signal> Signal()
的实现提供了多种行为。nofollow noreferrer“> sigard> signal> signal> )遵循C标准,但对信号处理程序中的动作的限制更少。
C标准说:
信号处理程序的经典结构假定系统将信号处理重置为
sig_dfl
,并小心地将信号处理程序作为处理程序中的第一个操作:但是,有一个很小的时间窗口。信号处理是默认值,因此,如果在此期间(简短)间隔中到达第二个信号,则该程序可能会停止。这就是为什么“在第一次导致默认行为发生后立即到达的同一类型的另一个信号”。并且由于如图所示写处理程序,因此只有一个小间隔在此默认信号处理生效。如果处理程序未重置信号处理,那么第二个信号到达时也没关系。
如果您想控制信号处理,请使用POSIX
sigaction(sigaction()
。它具有支持
signal()
的所有行为的选项,并提供更好的控制。默认情况下,它不会将信号处理重置为sig_dfl
;它避免了脆弱性的窗口。The specification of §7.14.1.1 The
signal
function in the C standard is carefully worded to allow quite a variety of behaviours, mainly because when the standard was created, existing implementations ofsignal()
provided a wide variety of behaviours.The POSIX specification for
signal()
follows the C standard, but places fewer restrictions on the actions in the signal handler.The C standard says:
The classic structure for a signal handler assumed that the system would reset the signal handling to
SIG_DFL
and carefully restored the signal handler as the first action in the handler:However, there is a small window of time during which the signal handling is the default, so if a second signal arrives during that (short) interval, the program may be stopped. That's why "another another signal of the same type arriving immediately after the first causes default behaviour to occur". And because the handler is written as shown, there is only a small interval during which the default signal handling is in effect. If the handler does not reset the signal handling, then it doesn't matter when the second signal arrives.
If you want control over the signal handling, use POSIX
sigaction()
. It has options to support all the behaviours ofsignal()
and provides much better control. By default, it does not reset the signal handling toSIG_DFL
; it avoids that window of vulnerability.