- // 程序退出时的函数操作
- void test(int n,struct siginfo *siginfo,void *myact)
- {
- printf("signal number:%d\n",n);/** 打印出信号值 **/
- printf("siginfo signo:%d\n",siginfo->si_signo); /** siginfo结构里保存的信号值 **/
- printf("siginfo errno:%d\n",siginfo->si_errno); /** 打印出错误代码 **/
- printf("siginfo code:%d\n",siginfo->si_code); /** 打印出出错原因 **/
- exit(0);
- }
- int main(void)
- {
- /** install signal use sigaction **/
- struct sigaction act;
- sigemptyset(&act.sa_mask); /** 清空阻塞信号 **/
- act.sa_flags=SA_SIGINFO; /** 设置SA_SIGINFO 表示传递附加信息到触发函数 **/
- act.sa_sigaction=test;
- if(sigaction(SIGINT,&act,NULL) < 0)
- {
- printf("install signal error\n");
- }
- while(1)
- {
- sleep(1);
- printf("wait for the signal\n");
- }
- }
///////////////////////////////////////////////////
if (atexit(&cleanup)) //atexit现在不鼓励用了,常用来做程序退出的后处理工作 { fprintf(stderr, "cannot set exit function/n"); return EXIT_FAILURE; } signal (SIGINT, &catch_int); signal (SIGTERM, &catch_term); #ifdef __WIN32__ signal (SIGBREAK, &catch_break); #else signal (SIGHUP, &catch_hup); signal (SIGQUIT, &catch_quit); #endif