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

OS X sigaction错误地设置了sa_mask

发布时间:2020-12-16 09:55:18 所属栏目:百科 来源:网络整理
导读:在macbook(OSX 10.9.5(13F34))上有以下简单程序: #include stdio.h#include signal.hstatic void nop(int unused) { }intmain(void) { struct sigaction sa,osa; sigset_t mask; sigemptyset(sa.sa_mask); printf("Errno after sigempty sa_mask: %dn",err
在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: %dn",errno);
    sigemptyset(&osa.sa_mask);
    printf("Errno after sigempty oldsa_mask: %dn",errno);
    sa.sa_flags = 0;
    sa.sa_handler = nop;

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

    sigaction(SIGALRM,&sa,&osa);
    printf("Errno after sigaction sa osa: %dn",sigismember(&osa.sa_mask,SIGALRM));
    printf("%dn",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

我希望osa的sa_mask成员匹配sigprocmask给出的掩码.

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匹配掩码.你的面具是当前的信号掩码.你的osa.sa_mask是一个额外的信号掩码,在调用osa.sa_handler来处理SIGALRM时应用.

在您的测试用例中,osa.sa_handler是SIG_DFL,因此osa.sa_mask的内容无关紧要.据我所知(在简短的搜索之后),POSIX没有说明osa.sa_mask应该是什么时候,就像你的测试案例一样,该过程自最近的执行官以来没有为信号设置动作.

此外,当系统调用已安装的SIGALRM处理程序时,它会自动在信号掩码中包含SIGALRM(除非您在安装处理程序时传递了SA_NODEFER或SA_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确实对信号的处理没有任何影响.

另请注意,对于sigprocmask的how参数传递0(而不是其中一个已定义的常量),即使set参数为null,也不清楚(对我而言)是合法的.但我发现将它改为SIG_BLOCK没有任何区别.

(编辑:李大同)

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

    推荐文章
      热点阅读