gpt4 book ai didi

c - OS X sigaction 错误地设置 sa_mask

转载 作者:太空狗 更新时间:2023-10-29 16:01:33 25 4
gpt4 key购买 nike

在 macbook (OSX 10.9.5 (13F34)) 上执行以下简单程序:

#include <stdio.h>
#include <signal.h>

static void nop(int unused) { }

int
main(void) {
struct sigaction sa, osa;
sigset_t mask;

sigemptyset(&sa.sa_mask);
printf("Errno after sigempty sa_mask: %d\n", errno);
sigemptyset(&osa.sa_mask);
printf("Errno after sigempty oldsa_mask: %d\n", errno);
sa.sa_flags = 0;
sa.sa_handler = nop;

sigprocmask(0, NULL, &mask);
printf("Errno after sigprocmask mask: %d\n", errno);
printf("%d\n", sigismember(&mask, SIGALRM));

sigaction(SIGALRM, &sa, &osa);
printf("Errno after sigaction sa osa: %d\n", errno);
printf("%d\n", sigismember(&osa.sa_mask, SIGALRM));
printf("%d\n", sigismember(&sa.sa_mask, SIGALRM));

return 0;
}

神秘地打印:

Errno after sigempty sa_mask: 0
Errno after sigempty oldsa_mask: 0
Errno after sigprocmask mask: 0
0
Errno after sigaction sa osa: 0
1
0

我希望 osasa_mask 成员匹配 sigprocmask 给定的 mask

POSIX 是否对此字段指定任何要求?联机帮助页中唯一提到的是关于不可阻塞的信号,如 SIGKILL,其中未指定该值。

在 linux 上,这个程序打印:

Errno after sigempty sa_mask: 0
Errno after sigempty oldsa_mask: 0
Errno after sigprocmask mask: 0
0
Errno after sigaction sa osa: 0
0
0

正如预期的那样。

gcc 版本是:

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darw

二进制文件链接到:

/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

最佳答案

I would expect that the sa_mask member of osa to match mask as given by sigprocmask.

您的期望不正确。

这是指向 the current (as of this writing) official POSIX documentation 的链接.收藏它!

Here is what that documentation says about sa_mask:

Additional set of signals to be blocked during execution of signal-catching function.

没有理由期望 osa.sa_mask 匹配 mask。您的掩码当前 信号掩码。您的 osa.sa_mask 是一个附加 信号掩码,在调用 osa.sa_handler 期间应用它来处理 SIGALRM.

在您的测试用例中,osa.sa_handlerSIG_DFL,因此osa.sa_mask 的内容无关紧要。据我所知(经过简短搜索),POSIX 没有说明 osa.sa_mask 应该是什么,就像在您的测试用例中一样,该过程自从最近的 exec

此外,当系统调用已安装的 SIGALRM 处理程序时,它会自动在信号掩码中包含 SIGALRM(除非您通过 SA_NODEFERSA_RESETHAND 当你安装处理程序时)。引用上面链接的文档:

When a signal is caught by a signal-catching function installed by sigaction(), a new signal mask is calculated and installed for the duration of the signal-catching function (or until a call to either sigprocmask() or sigsuspend() is made). This mask is formed by taking the union of the current signal mask and the value of the sa_mask for the signal being delivered, and unless SA_NODEFER or SA_RESETHAND is set, then including the signal being delivered.

因此,如果 sa_flags 为 0,则 sa_mask 是否包含 SIGALRM 是无关紧要的。事实上,Linux 不包含它而 OS X 包含它对信号的处理没有区别。

还请注意,(对我而言)不清楚为 sigprocmaskhow 参数传递 0(而不是定义的常量之一)是否合法,即使当 set 参数为 null 时。但我发现将其更改为 SIG_BLOCK 没有任何区别。

关于c - OS X sigaction 错误地设置 sa_mask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159255/

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