加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如果在取消点处调用信号处理程序,会发生什么?

发布时间:2020-12-16 05:39:22 所属栏目:百科 来源:网络整理
导读:假设应用在取消点被阻塞,例如读取,并且接收到信号并且调用信号处理程序. Glibc / NPTL通过在系统调用的持续时间内启用异步取消来实现取消点,因此据我所知,异步取消将在信号处理程序的整个持续时间内保持有效.这当然是非常错误的,因为有很多功能不是异步取消
假设应用在取消点被阻塞,例如读取,并且接收到信号并且调用信号处理程序. Glibc / NPTL通过在系统调用的持续时间内启用异步取消来实现取消点,因此据我所知,异步取消将在信号处理程序的整个持续时间内保持有效.这当然是非常错误的,因为有很多功能不是异步取消安全的,但是需要安全地从信号处理程序调用.

这让我有两个问题:

>我错了,还是glibc / NPTL行为真的这个危险的打破?如果是这样,这样的危险行为是否符合?
>根据POSIX,如果在进程正在执行一个取消点的函数时调用一个信号处理程序,那应该是什么?

编辑:我几乎相信,任何线程是pthread_cancel的潜在目标,必须确保在线程上下文中不能从信号处理程序中调用取消点的函数:

一方面,可以在线程中调用的任何可能被取消的信号处理程序,并且使用任何异步取消不安全功能的任何信号处理程序必须在调用任何作为取消点的函数之前禁用取消.这是因为从信号中断的代码的角度来看,任何这样的取消将等同于异步取消.另一方面,信号处理程序无法禁止取消,除非调用信号处理程序时将运行的代码仅使用异步信号安全功能,因为pthread_setcancelstate不是异步信号安全的.

解决方法

回答我自己问题的前半部分:glibc确实表现出我预测的行为.在异步取消运行的情况下,在取消点被阻止时运行的信号处理程序.要看到这个效果,只需创建一个线程,调用一个将永久阻止(或长时间)的取消点,等待一会儿,发送一个信号,再等一下,然后取消并加入.信号处理程序应该以某种方式解决一些易变的变量,这样可以清楚地表明它在异步终止之前运行了不可预测的时间量.

至于POSIX是否允许这种行为,我还没有百分百的肯定. POSIX声明:

Whenever a thread has cancelability enabled and a cancellation request has been made with that thread as the target,and the thread then calls any function that is a cancellation point (such as pthread_testcancel() or read()),the cancellation request shall be acted upon before the function returns. If a thread has cancelability enabled and a cancellation request is made with the thread as a target while the thread is suspended at a cancellation point,the thread shall be awakened and the cancellation request shall be acted upon. It is unspecified whether the cancellation request is acted upon or whether the cancellation request remains pending and the thread resumes normal execution if:

  • The thread is suspended at a cancellation point and the event for which it is waiting occurs

  • A specified timeout expired

before the cancellation request is acted upon.

推测执行信号处理程序不是“暂停”的情况,所以我倾向于将glibc的行为解释为不符合要求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读