gpt4 book ai didi

c - Linux:可以抢占信号处理程序的执行吗?

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

我遇到了以下存储 errno 变量的信号处理程序代码,因此它不会影响主线程的 errno 处理。

void myhandler(int signo)
{
int esaved;
esaved = errno;
write(STDOUT_FILENO, "Got a signal\n", 13);
errno = esaved;
}

但这真的有用吗?如果另一个线程在 write() 之后和恢复 errno 之前检查共享的 errno 变量,会发生什么情况?该线程是否会由于竞争条件而获得错误的 errno 值?

或者信号处理程序相对于线程/进程原子地执行,以便一旦信号处理程序执行,内核不会调度线程返回直到信号处理程序完成?

换句话说 - 一旦启动,信号处理程序就会执行而不会被以下中断:

 - 1) Scheduler (process/threads),  or  
- 2) Other signals, or
- 3) Hardware interrupt handlers ?

最佳答案

信号处理程序确实可以被另一个信号中断(假设它与首先调用处理程序的信号不同)。

your handler can still be interrupted by delivery of another kind of signal. To avoid this, you can use the sa_mask member of the action structure passed to sigaction to explicitly specify which signals should be blocked while the signal handler runs. These signals are in addition to the signal for which the handler was invoked, and any other signals that are normally blocked by the process. See Blocking for Handler.

When the handler returns, the set of blocked signals is restored to the value it had before the handler ran. So using sigprocmask inside the handler only affects what signals can arrive during the execution of the handler itself, not what signals can arrive once the handler returns.

http://www.gnu.org/software/libc/manual/html_node/Signals-in-Handler.html#Signals-in-Handler

关于c - Linux:可以抢占信号处理程序的执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651964/

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