如何修复信号处理程序分配

时间:2019-03-06 23:56:24

标签: c linux signals handler sigaction

对于Linux C编程,我有以下处理程序和主要方法:

// Inferred return type: Something | StricterEmptyObject
const getSomething = (t: Test) => t['key'] || {} as StricterEmptyObject;


declare const test: Test;

// inferred: string | undefined
const a = getSomething(test).a;

如果我仅使用1个参数void handler(int number, signinfo_t, void *ignore){ printf("Signaling %d\n", si->si_pid); } int main(){ struct sigaction sig; sig.sa_flags = SA_SIGINFO; sigemptyset(&sig.sa_mask); sig.sa_handler = handler // This line has error 来创建处理程序 它工作正常;但是,我将无法使用void handler(int num)。我得到的警告是:

si->si_pid

我改为 warning: assignment to __sighandler_t from an incompatible pointer type -Wincompatible-pointer-types sig.sa_handler = handler; 吗?我要修正警告

1 个答案:

答案 0 :(得分:1)

您正在将处理函数分配给sig的错误成员。 struct sigaction的声明为:

struct sigaction {
    void     (*sa_handler)(int);
    void     (*sa_sigaction)(int, siginfo_t *, void *);
    sigset_t   sa_mask;
    int        sa_flags;
    void     (*sa_restorer)(void);
};

sig.sa_handler是一个只有一个参数(信号编号)的函数。使用SA_SIGINFO标志时,需要将3参数函数分配给sig.sa_sigaction

int main(){
    struct sigaction sig;
    sig.sa_flags = SA_SIGINFO;
    sigemptyset(&sig.sa_mask);
    sig.sa_sigaction = handler;