【问题标题】:SIG_DFL throws warning at compiling, is ignored by OS when executingSIG_DFL 在编译时抛出警告,在执行时被操作系统忽略
【发布时间】:2014-05-17 11:02:48
【问题描述】:

我有一个看起来很奇怪的小问题。我写了这段代码来处理信号。

void sig_install(wlist *arg)
{
    struct sigaction sigstruct;
    //Doing stuff
    sigstruct.sa_handler = &handlerfunc;
    sigstruct.sa_flags = SA_RESTART;
    sigaction(sig, &sigstruct, NULL)
}

而且这个真的很好用。我必须定义

#define _POSIX_C_SOURCE 200809L

解决 SA_RESTART 标志(我的教授告诉我使用它)。 我的问题如下:我到处都读到我可以使用 SIG_DFL 和 sigaction 为我的信号安装默认处理程序,例如:

void sig_uninstall(wlist *arg)
{
    //Do stuff
    if (sigaction(sig, SIG_DFL, NULL) == 0)
        printf("Signalhandler deleted!\n");
    else
        printf("Signalhandler could not be deleted!\n");
}

但是当我编译它时,我收到一个关于 SIG_DFL 参数的警告,上面写着:

»const struct sigaction * __restrict__« expected, but Argument is type »void (*)(int)«

当我执行程序时,sigaction() 返回 0 表示成功,但是当我发送信号时,我的信号处理程序仍然不是默认值。

我的标题中的定义是:

#define SIG_DFL ((__sighandler_t) 0)        /* Default action.  */
# define SA_RESTART   0x10000000 /* Restart syscall on signal return.  */
/* Get and/or set the action for signal SIG.  */
extern int sigaction (int __sig, __const struct sigaction *__restrict __act,
          struct sigaction *__restrict __oact) __THROW;

我在 mandb 中读到 SA_RESTART 仅在 BSD 上使用。这可能是问题吗? linux有替代标志吗?(在大学我们必须使用BSD,但在家里我使用linux)。非常感谢您的宝贵时间。

【问题讨论】:

    标签: c linux signals bsd


    【解决方案1】:

    您需要将struct sigactionsa_handler(伪)成员设置为SIG_DFL,而不是struct sigaction * 本身。

    或者,直接使用signal()

    【讨论】:

    • 完成了这项工作。非常感谢!
    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2014-08-14
    • 2023-03-18
    • 2010-09-18
    相关资源
    最近更新 更多