保证在 sigsuspend returns 时将调用信号处理程序
Guarantee that signal handler will be called when sigsuspend returns
假设我为 SIGALRM
设置了以下信号处理程序:
volatile sigatomic_t alarm_flag;
void alarm_handler(int signum)
{
(void)signum;
alarm_flag = 1;
}
然后假设我调用了 alarm
然后输入了一段代码,其中 SIGALRM
被阻止但我想与之同步。也就是我想用sigsuspend
等待闹钟过期:
sigfillset(&set);
sigdelset(&set, SIGALRM);
sigsuspend(&set);
some_other_command();
是否有任何 POSIX 保证 alarm_handler
将在 some_other_command
执行时调用,或者我是否需要执行以下操作?
sigsuspend(&set);
while ( !alarm_flag );
some_other_command();
来自 sigsuspend()
的 POSIX 文档:
If the action is to execute a signal-catching function, then sigsuspend()
shall return after the signal-catching function returns, with the signal mask restored to the set that existed prior to the sigsuspend()
call.
所以是的,有保证。如果您没有在 sigsuspend()
中阻止 SIGALARM
,至少(当然,假设在调用警报之前未处理警报)。
假设我为 SIGALRM
设置了以下信号处理程序:
volatile sigatomic_t alarm_flag;
void alarm_handler(int signum)
{
(void)signum;
alarm_flag = 1;
}
然后假设我调用了 alarm
然后输入了一段代码,其中 SIGALRM
被阻止但我想与之同步。也就是我想用sigsuspend
等待闹钟过期:
sigfillset(&set);
sigdelset(&set, SIGALRM);
sigsuspend(&set);
some_other_command();
是否有任何 POSIX 保证 alarm_handler
将在 some_other_command
执行时调用,或者我是否需要执行以下操作?
sigsuspend(&set);
while ( !alarm_flag );
some_other_command();
来自 sigsuspend()
的 POSIX 文档:
If the action is to execute a signal-catching function, then
sigsuspend()
shall return after the signal-catching function returns, with the signal mask restored to the set that existed prior to thesigsuspend()
call.
所以是的,有保证。如果您没有在 sigsuspend()
中阻止 SIGALARM
,至少(当然,假设在调用警报之前未处理警报)。