信号传递的时机&信号执行优先级

  •    信号传递的时机

  1.进程被再度调度时。

     意味着获得执行时间片。

  2.系统调用完成时。

   信号的传递可能引起阻塞的系统调用过早完

  • 解除信号阻塞时,信号的传递顺序

   linux 内核时按照信号编号的升序来传递信号的。

    测试一下

      

 1 #include <signal.h>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 static void sig_int(int signo) {
 7     if (signo == SIGINT)
 8         printf("SIGINT interrupt ,%d\n", SIGINT);
 9     else if ( signo == SIGQUIT)
10         printf("SIGQUIT interrupt %d\n", SIGQUIT);
11 
12 }
13 
14 int main(int argc, char const *argv[])
15 {
16     
17     sigset_t pendmask, oldmask, zeromask;
18     signal(SIGINT, sig_int);
19     signal(SIGQUIT, sig_int);
20      sigfillset(&pendmask);
21      printf("block all \n");
22      sigprocmask(SIG_BLOCK, &pendmask, &oldmask);
23      raise(SIGQUIT);
24      raise(SIGINT);
25      sleep(2);
26     printf("unblock   \n");
27      sigprocmask(SIG_SETMASK, &oldmask, &oldmask);
28      printf("exit\n");
29 
30     return 0;
31 }

 

执行结果 
block all
unblock
SIGQUIT interrupt 3
SIGINT interrupt ,2
exit
为什么 SIGQUIT(3)先被执行,难道有问题?

 看看下面的测试程序

  

 1 #include <signal.h>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 static void sig_int(int signo) {
 7     if (signo == SIGINT)
 8         printf("SIGINT interrupt ,%d\n", SIGINT);
 9     else if ( signo == SIGQUIT)
10         printf("SIGQUIT interrupt %d\n", SIGQUIT);
11     else if ( signo == SIGTERM)
12         printf("SIGTERM interrupt %d\n", SIGTERM);
13 }
14 
15 int main(int argc, char const *argv[])
16 {
17     
18     sigset_t pendmask, oldmask, zeromask;
19     struct  sigaction sa; 
20      sigfillset(&pendmask);
21      sa.sa_flags = 0;
22     sa.sa_handler = sig_int;
23     sa.sa_mask = pendmask;
24     sigaction(SIGINT, &sa, NULL);
25     sigaction(SIGQUIT, &sa, NULL);
26     sigaction(SIGTERM, &sa, NULL);
27     sigaction(SIGUSR1, &sa, NULL);
28      printf("block all \n");
29      sigprocmask(SIG_BLOCK, &pendmask, &oldmask);
30      raise(SIGUSR1);
31      raise(SIGQUIT);
32      raise(SIGINT);
33      raise(SIGTERM);
34      sleep(2);
35     printf("unblock   \n");
36      sigprocmask(SIG_SETMASK, &oldmask, &oldmask);
37      printf("exit\n");
38 
39     return 0;
40 }
View Code

   执行结果

block all
unblock
SIGINT interrupt ,2
SIGQUIT interrupt 3
SIGTERM interrupt 15
exit


原来是信号处理程序时没有屏蔽信号,导致的。


 

    

转载于:https://www.cnblogs.com/snail88/p/8413623.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值