<分区>
我是 Unix 中信号的新手,这个练习的目的是禁用 SIGINT 来停止程序并使用 SIGQUIT 代替,当我按下 ctrl-c 时它成功地忽略了 SIGINT 但 printf 直到我按下Ctrl-/用于 SIGQUIT,所有消息立即出现。
示例:如果我按 ctrl-c 4 次,然后按 ctrl-/: 仅在按下 ctrl-\后,我收到 4 条消息说我按下了 Ctrl-c,然后退出时您发送了 SIGQUIT 信号。
如果我只按 ctrl-c,我什么也得不到,但至少它被忽略了
struct sigaction action;
void hand(int sig)
{
switch(sig){
case SIGINT:
printf("You have presed ctrl-c");
break;
case SIGQUIT:
printf("Quittin you sent a SIGQUIT signal");
exit(SIGQUIT);
break;
default:
printf("WRONG SIGNAL");
exit(-1);
break;
}
}
int main()
{
action.sa_handler=hand;
printf("Ctrl-C disabled, press Ctrl-/ to quit...\n");
sigaction(SIGINT,&action,NULL);
sigaction(SIGQUIT,&action,NULL);
while(1){
sleep(1);
}
}