信号槽

本文通过两个实例详细介绍了如何在Linux环境下使用信号处理函数,包括捕捉并处理特定信号的方法,以及如何利用信号集进行更复杂的信号操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

信号:signal(), sigaction(), sigaddset(), sigemptyset(), sigismember(), sigprocmask()

分类: Linux设备驱动入门   302人阅读  评论(0)  收藏  举报

实例一:如何使用信号函数捕捉相应的信号,并做相应的处理。

[cpp]  view plain copy
  1. /* 
  2.  * signal.c 
  3.  * 
  4.  *  Created on: 2012-7-19 
  5.  *      Author: liwei.cai 
  6.  */  
  7.   
  8. #include <signal.h>  
  9. #include <stdio.h>  
  10. #include <stdlib.h>  
  11.   
  12. //自定义信号处理函数  
  13. void my_func(int sign_no)  
  14. {  
  15.     if (sign_no == SIGINT)  
  16.     {  
  17.         printf("I have get SIGINT\n"); //Ctrl + C  
  18.     }  
  19.     else if (sign_no == SIGQUIT)  
  20.     {  
  21.         printf("I hava get SIGQUIT\n");//Ctrl + \  
  22.     }  
  23. }  
  24.   
  25. //int main()  
  26. //{  
  27. //  printf("Waiting for signal SIGINT or SIGQUIT..\n");  
  28. //  //发出相应的信号,并跳到信号处理函数处  
  29. //  signal(SIGINT,my_func);  
  30. //  signal(SIGQUIT,my_func);  
  31. //  pause();  
  32. //  exit(0);  
  33. //}  
  34.   
  35. int main()  
  36. {  
  37.     struct sigaction action;  
  38.     printf("Waiting for signal SIGINT or SIGQUIT>>>\n");  
  39.     //sigaction结构初始化  
  40.     action.sa_handler = my_func;  
  41.     sigemptyset(&action.sa_mask);  
  42.     action.sa_flags = 0;  
  43.   
  44.     //发出相应的信号,并跳转到信号处理函数处  
  45.     sigaction(SIGINT, &action,0);  
  46.     sigaction(SIGQUIT, &action, 0);  
  47.     pause();  
  48.     exit(0);  
  49. }  
实例二:首先把SIGQUIT、SIGINT两个信号加入信号集,然后将该信号集合设为阻塞状态,并进入用户输入状态。用户只需按任意键,就可以立刻将信号集合设置为非阻塞状态,再对这个信号分别操作,其中SIGQUIT执行默认操作,而SIGINT执行用户自定函数的操作。

[cpp]  view plain copy
  1. /* 
  2.  * sigset.c 
  3.  * 
  4.  *  Created on: 2012-7-20 
  5.  *      Author: liwei.cai 
  6.  */  
  7. #include <sys/types.h>  
  8. #include <unistd.h>  
  9. #include <signal.h>  
  10. #include <stdio.h>  
  11. #include <stdlib.h>  
  12.   
  13. //自定义的信号处理函数  
  14. void my_func(int signum)  
  15. {  
  16.     printf("IF you want to quit, please try SIGUIT\n");  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     sigset_t set, pendset;  
  22.     struct sigaction action1, action2;  
  23.   
  24.     //初始化信号集为空  
  25.     if (sigemptyset(&set) < 0)  
  26.     {  
  27.         perror("sigemptyset");  
  28.         exit(1);  
  29.     }  
  30.     //将相应的信号加入到信号集  
  31.     if(sigaddset(&set, SIGQUIT) < 0)  
  32.     {  
  33.         perror("sigaddset");  
  34.         exit(1);  
  35.     }  
  36.     if(sigaddset(&set, SIGINT) < 0)  
  37.     {  
  38.         perror("sigaddset");  
  39.         exit(1);  
  40.     }  
  41.   
  42.     if(sigismember(&set, SIGINT))  
  43.     {  
  44.         sigemptyset(&action1.sa_mask);  
  45.         action1.sa_handler = my_func;  
  46.         action1.sa_flags = 0;  
  47.         sigaction(SIGINT, &action1, NULL);  
  48.     }  
  49.     if(sigismember(&set, SIGQUIT))  
  50.     {  
  51.         sigemptyset(&action2.sa_mask);  
  52.         action2.sa_handler = my_func;  
  53.         action2.sa_flags = 0;  
  54.         sigaction(SIGINT, &action2, NULL);  
  55.     }  
  56.     //设置信号集屏蔽字,此时set中的信号不会被传递给进程,暂时进入待处理状态  
  57.     if (sigprocmask(SIG_BLOCK, &set, NULL) < 0)  
  58.     {  
  59.         perror("sigprocmask");  
  60.         exit(1);  
  61.     }  
  62.     else  
  63.     {  
  64.         printf("Signal set was blocked, Press any key!");  
  65.         getchar();  
  66.     }  
  67.     //在信号屏蔽字中删除set中的信号  
  68.     if (sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)  
  69.     {  
  70.         perror("sigprocmask");  
  71.         exit(1);  
  72.     }  
  73.     else  
  74.     {  
  75.         printf("Signal set is in unblock state!");  
  76.         getchar();  
  77.     }  
  78.     while(1);  
  79.     exit(0);  
  80. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值