使用sigaction处理SIGINT时阻塞SIGQUIT
# include <stdio. h> # include <signal.h> # define INPUTLEN 100 void inthandler(); int main()
{ struct sigaction newhandler; sigset_t blocked; char x[INPUTLEN]; newhandler.sa_handler=inthandler; newhandler.sa_flags=SA_RESETHAND|SA_RESTART; sigemptyset(&blocked); sigaddset(&blocked,SIGQUIT); newhandler.sa_mask=blocked; if( sigaction( SIG1NT, &newhandler, NULL) = = - 1 ) perror("sigaction"); else whileC 1 ) { fgets(x,INLUTLEN,stdin); printf("input:%s",x); }
} void inthandler( int s) { printf( "Called with signal 屯 d\n飞的 J sleep(s); printf("done handling signal %d\n",s); }
函数原型:
<signal.h>
int sigaction(int signum, const struct sigaction* action, struct sigaction* prevaction)
signum:要处理的信号 action:指向描述如何响应信号的结构体 prevation:被替换的结构体,可以为NULL
成功返回0,失败返回-1
struct sigaction
{
void(*sa_handler)();
void(*sa_sigaction)(int,siginfo_t*,void*);
sigset_t sa_mask;//被阻塞的信号集
int sa_flags;//SA_RESETHAND,SA_NODEFER,SA_RESTART,SA_SIGINFO
}
SA_RESETHAND:当处理函数被调用时需要重置才再次有效
SA_NODEFER:允许递归调用信号处理函数
SA_RESTART:当输入被中断后需要重新输入
SA_SIGINFO:处理函数使用sa_sigaction。

浙公网安备 33010602011771号