2012-11-25 109 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; 
    } 
} 
相關問題