linux c编程信号处理的一些实例signal sigaction

来源: http://www.nenew.net/linux-c-signal-sigaction-examples.html

先把课本上的两个小实例放上来,记录下:

1.signal()的使用

捕捉ctrl+c发送的SIGINT与ctrl+\发送的SIGQUIT信号

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <signal.h> 
  4.  
  5. void my_func(int sign_no) { 
  6.     if (sign_no == SIGINT) { 
  7.         printf("I have get SIGINT\n"); 
  8.     } else if (sign_no == SIGQUIT) { 
  9.         printf("I have get SIGQUIT\n"); 
  10.     } 
  11.  
  12. int main() { 
  13.     printf("waiting for signal SIGINT or SIGQUIT ... \n"); 
  14.     signal(SIGINT, my_func); 
  15.     signal(SIGQUIT, my_func); 
  16.     pause(); 
  17.     exit(0); 

2.sigaction()的使用<—感觉是signal的一个变种

同样是捕捉ctrl+c发送的SIGINT与ctrl+\发送的SIGQUIT信号

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <signal.h> 
  4.  
  5. void my_func(int sign_no) { 
  6.     if (sign_no == SIGINT) { 
  7.         printf("I have get SIGINT\n"); 
  8.     } else if (sign_no == SIGQUIT) { 
  9.         printf("I have get SIGQUIT\n"); 
  10.     } 
  11.  
  12. int main() { 
  13.     struct sigaction action; 
  14.     printf("waiting for signal SIGINT or SIGQUIT ... \n"); 
  15.     action.sa_handler = my_func; 
  16.     sigemptyset(&action.sa_mask); 
  17.     action.sa_flags = 0; 
  18.     sigaction(SIGINT, &action, 0); 
  19.     sigaction(SIGQUIT, &action, 0); 
  20.     pause(); 
  21.     exit(0); 

3.信号集

默认对信号进行状态阻塞,此时输入任何信号都不执行,ctrl+c 与ctrl+\都不会被执行,但是当输入任意字符并回车后,原来的信号就会被立即执行.可以先输入任意字符解除SIG_BLOCK状态,然后执行SIG_INT与SIG_QUIT,SIG_INT调用我们自己的函数my_func,所以程序会一直不退出,但是SIG_QUIT仍旧是系统的函数,因此可以正常表达

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <sys/types.h> 
  4. #include <unistd.h> 
  5. #include <signal.h> 
  6.  
  7. void my_func(int signum) { 
  8.     printf("If you want to quit ,please try SIGQUIT\n"); 
  9.  
  10. int main() { 
  11.     sigset_t set, pendset; 
  12.     struct sigaction action1, action2; 
  13.  
  14.     if (sigemptyset(&set) < 0) { 
  15.         perror("sigemptyset"); 
  16.         exit(1); 
  17.     } 
  18.  
  19.     if (sigaddset(&set, SIGQUIT) < 0) { 
  20.         perror("sigaddset"); 
  21.         exit(1); 
  22.     } 
  23.  
  24.     if (sigaddset(&set, SIGINT) < 0) { 
  25.         perror("sigaddset"); 
  26.         exit(1); 
  27.     } 
  28.  
  29.     if (sigismember(&set, SIGINT)) { 
  30.         sigemptyset(&action1.sa_mask); 
  31.         action1.sa_handler = my_func; 
  32.         action1.sa_flags = 0; 
  33.         sigaction(SIGINT, &action1, 0); 
  34.     } 
  35.  
  36.     if (sigismember(&set, SIGQUIT)) { 
  37.         sigemptyset(&action2.sa_mask); 
  38.         action2.sa_handler = SIG_DFL; 
  39.         action2.sa_flags = 0; 
  40.         sigaction(SIGQUIT, &action2, 0); 
  41.     } 
  42.  
  43.     if (sigprocmask(SIG_BLOCK, &set, NULL) < 0) { 
  44.         perror("sigprocmask"); 
  45.         exit(1); 
  46.     } else { 
  47.         printf("Signal set was blocked,Press any key!"); 
  48.         getchar(); 
  49.     } 
  50.     if (sigprocmask(SIG_UNBLOCK, &set, NULL) < 0) { 
  51.         perror("sigprocmask"); 
  52.         exit(1); 
  53.     } else { 
  54.         printf("Signal set is unblock state\n"); 
  55.     } 
  56.     while (1) 
  57.         ; 
  58.     exit(0); 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值