linux c信号使用
#include #include #include #include void handler(int s){ printf("----------------\n"); if (s == SIGBUS) printf(" now got a bus error signal\n"); if (s == SIGSEGV) printf(" now got a segmentation violation signal\n"); if (s == SIGILL) printf(" now got an illegal instruction signal\n"); if (s == SIGINT) puts("ctrl + c signal"); if (s == SIGKILL) puts("kill -9 signal"); if (s == SIGSTOP) puts("ctrl + z signal");// exit(1);}void do_sig(int num){ printf("I am do_sig\n"); printf("num = %d\n", num);}int main() { signal(SIGBUS, handler); signal(SIGSEGV, handler); signal(SIGILL, handler); signal(SIGINT, handler);//ctrl + c signal(SIGKILL, handler); signal(SIGSTOP, handler);//ctrl + z// int *a = NULL;// *a = 5;/* struct sigaction act; act.sa_handle = do_sig; //act.sa_handle = SIG_DEF;//默认动作 //act.sa_handle = SIG_IGN;//忽略 sigemptyset(&act.sa_mask);//设置BLOCK阻塞信号集为0,为未阻塞 //sigaddset(&act.sa_mask, SIGQUIT);//屏蔽其他信号屏蔽字,置为1 act.sa_flags =0; sigaction(SIGINT, &act, NULL); */ while(1); return 0;}
#include #include #include #include void docatch(int signo) //用户处理函数{ printf("the %dth signal is catched\n", signo); sleep(10); printf("-------finish------\n");}int main(void){ int ret; struct sigaction act; act.sa_handler = docatch; sigemptyset(&act.sa_mask); sigaddset(&act.sa_mask, SIGQUIT); //sa_mask屏蔽字中,3号信号置1 act.sa_flags = 0; //默认属性 信号捕捉函数执行期间,自动屏蔽本信号 // ret = sigaction(SIGINT, &act, NULL); //注册2号信号 ret = sigaction(SIGKILL,&act,NULL); if (ret == -1) { perror("sigaction error"); exit(1); } while (1); return 0;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~