2012-11-25 111 views
1

我不知道如何使用此作业来执行方法,当信号sig作为参数给出时,必须调用函数func注册函数处理程序

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

谢谢。

回答

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; 
    } 
} 
相关问题