1

我不知道如何用这个赋值来做一个方法,func当信号sig作为参数给出时,它必须调用函数。

  void set_sig_handler(int sig,void (*func)(int)){

谢谢。

4

1 回答 1

1

您可以使用sigaction(),处理程序必须具有以下签名之一:

/* this one matches your function */
void (*sa_handler)(int);

/* use thhis one If SA_SIGINFO is specified */
void (*sa_sigaction)(int, siginfo_t *, void *);

例子:

#include <signal.h>
....
void set_sig_handler(int sig, void (*func)(int))
{
    struct sigaction act= {0};

    /* set signal handler */
    act.sa_handler = func;

    if (sigaction(sig, &act, NULL) < 0) {
        perror ("sigaction");
        return 1;
    }
}
于 2012-11-25T16:00:47.030 回答