Linux可靠/不可靠信号编程实践

综合案例

    1) 创建子进程与父进程;

    2) 注册SIGINT非实时信号与SIGRTMIN实时信号,并将这两种信号添加到进程屏蔽信号组中;

    3) 注册用户自定义信号;

    4) 子进程发送5次非实时信号,发5次实时信号;

    5) 然后子进程发送SIGUSR1解除进程对SIGINT,SIGTRMIN信号的阻塞

    6) 观察实时信号与非实时信号的区别


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //程序示例  
  2. void onSigAction(int signalNumber, siginfo_t *sigInfoStruct, void *)  
  3. {  
  4.     //获取接收到的数据  
  5.     int receiveNumber = sigInfoStruct->si_int;  
  6.   
  7.     //如果收到的是SIGUSR1信号,则解除对SIGINT,SIGRTMIN的屏蔽  
  8.     if (signalNumber == SIGUSR1)  
  9.     {  
  10.         sigset_t unblockSet;  
  11.         sigemptyset(&unblockSet);  
  12.         sigaddset(&unblockSet,SIGINT);  
  13.         sigaddset(&unblockSet,SIGRTMIN);;  
  14.         sigprocmask(SIG_UNBLOCK,&unblockSet,NULL);  
  15.   
  16.         //Value值会是乱码!  
  17.         //cout << "Receive SIGUSR1, Value = " << receiveNumber << endl;  
  18.         cout << "Unblock, Receive SIGUSR1" << endl;  
  19.     }  
  20.     else if (signalNumber == SIGINT)  
  21.     {  
  22.         cout << "Receive SIGINT, Value = " << receiveNumber << endl;  
  23.     }  
  24.     else if (signalNumber == SIGRTMIN)  
  25.     {  
  26.         cout << "Receive SIGRTMIN, Value = " << receiveNumber << endl;  
  27.     }  
  28. }  
  29.   
  30. //错误退出函数  
  31. inline void err_exit(string str);  
  32.   
  33. int main()  
  34. {  
  35.     struct sigaction act;  
  36.     //如果需要使得信号处理程序接收额外数据,  
  37.     //则必须将sa_flags位置为SA_SIGINFO  
  38.     act.sa_flags = SA_SIGINFO;  
  39.     sigemptyset(&act.sa_mask);  
  40.     act.sa_sigaction = onSigAction;  
  41.   
  42.     //注册信号处理函数  
  43.     if (sigaction(SIGINT,&act,NULL) < 0)  
  44.         err_exit("sigaction SIGINT");  
  45.     if (sigaction(SIGRTMIN,&act,NULL) < 0)  
  46.         err_exit("sigaction SIGRTMIN");  
  47.     if (sigaction(SIGUSR1,&act,NULL) < 0)  
  48.         err_exit("sigaction SIGUSR1");  
  49.   
  50.     //将信号SIGINT,SIGRTMIN信号阻塞  
  51.     sigset_t blockSet;  
  52.     sigemptyset(&blockSet);  
  53.     sigaddset(&blockSet,SIGINT);  
  54.     sigaddset(&blockSet,SIGRTMIN);  
  55.     if (sigprocmask(SIG_BLOCK,&blockSet,NULL) < 0)  
  56.         err_exit("sigprocmask error");  
  57.   
  58.     pid_t pid = fork();  
  59.     if (pid == -1)  
  60.         err_exit("fork error");  
  61.     else if (pid == 0)  
  62.     {  
  63.         union sigval Value;  
  64.         Value.sival_int = 200;  
  65.   
  66.         //给父进程发送五次带额外数据的非可靠信号(其实最终只接受到了一次)  
  67.         for (int i = 0; i < 5; ++i)  
  68.         {  
  69.             ++ Value.sival_int;  
  70.             if (sigqueue(getppid(),SIGINT,Value) != 0)  
  71.                 err_exit("sigqueue error");  
  72.         }  
  73.   
  74.         //给父进程发送五次带额外数据的可靠信号(最终接收到了五次!!!)  
  75.         Value.sival_int = 0;  
  76.         for (int i = 0; i < 5; ++i)  
  77.         {  
  78.             ++ Value.sival_int;  
  79.             if (sigqueue(getppid(),SIGRTMIN,Value) != 0)  
  80.                 err_exit("sigqueue error");  
  81.         }  
  82.   
  83.         //给父进程发送SIGUSR1信号,解除对SIGINT,SIGRTMIN信号的阻塞  
  84.         kill(getppid(),SIGUSR1);  
  85.     }  
  86.   
  87.     while (true)  
  88.     {  
  89.         pause();  
  90.     }  
  91. }  
  92.   
  93. void err_exit(string str)  
  94. {  
  95.     perror(str.c_str());  
  96.     exit(EXIT_FAILURE);  
  97. }  

运行结果:

 

 

其实只是收到了一份非可靠信号SIGINT!

 

附-查看系统限制命令:ulimit

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. xiaofang@xiaofang:~$ ulimit -a  
  2. core file size          (blocks, -c) unlimited  
  3. data seg size           (kbytes, -d) unlimited  
  4. scheduling priority             (-e) 0  
  5. file size               (blocks, -f) unlimited  
  6. pending signals                 (-i) 47131  
  7. max locked memory       (kbytes, -l) 64  
  8. max memory size         (kbytes, -m) unlimited  
  9. open files                      (-n) 1024  
  10. pipe size            (512 bytes, -p) 8  
  11. POSIX message queues     (bytes, -q) 819200  
  12. real-time priority              (-r) 0  
  13. stack size              (kbytes, -s) 8192  
  14. cpu time               (seconds, -t) unlimited  
  15. max user processes              (-u) 47131  
  16. virtual memory          (kbytes, -v) unlimited  
  17. file locks                      (-x) unlimited  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值