当信号 sig
作为参数给出时,我不知道如何使用此赋值调用函数 func
的方法。
void set_sig_handler(int sig,void (*func)(int)){
谢谢。
最佳答案
你可以使用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;
}
}
关于c - 注册一个函数处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552642/