信号处理程序中的取消点?
如果程序调用作为信号处理程序取消点的函数,会发生什么情况? POSIX 指定了许多函数作为异步信号安全点和取消点。如果信号处理程序调用这样的函数并执行取消操作,则结果与线程启用异步取消时发生的情况非常相似 - 实际上更糟糕,因为所有取消清理处理程序(可能不是异步信号)安全,将从信号处理程序上下文中调用。
在这种情况下,POSIX 实际上指定了什么,以及实现实际上做了什么?我在 POSIX 中找不到任何禁止对信号处理程序中的取消点进行操作的语言,在 glibc/nptl 源代码中也找不到任何此类保护。
What happens if a program calls a function which is a cancellation point from a signal handler? There are a number of functions which POSIX specifies as both async-signal-safe and cancellation points. If a signal handler calls such a function and cancellation is acted upon, the result is quite similar to what would happen if the thread had enabled asynchronous cancellation - actually much worse, because all the cancellation cleanup handlers, which are probably not async-signal-safe, would be called from a signal-handler context.
What does POSIX actually specify in this case, and what do implementations actually do? I can't find any language in POSIX that would forbid cancellation points in signal handlers from being acted upon, nor any such protection in the glibc/nptl source.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 POSIX 甚至敢于提及这个话题,但我还没有进行详尽的搜索。
对 gcc/nptl 系统的一些简短实验表明,正如我所怀疑的那样,我认为您也这样做了,NPTL 中没有这样的保护 - 取消处理程序确实从信号处理程序上下文中被调用。
下面的程序(对黑客行为等表示歉意)显示以下输出:
... 表明:
pthread_cancel()
程序如下:
I'm not aware that POSIX even dares to mention this topic, but I haven't done an exhaustive search.
Some brief experimentation with a gcc/nptl system reveals that, as I suspected and I think you did too, there is no such protection in NPTL - the cancellation handlers do indeed get called, from within the signal handler context.
The program below (apologies for the hackiness etc) displays the following output:
... indicating that:
pthread_cancel()
Here's the program: