zoukankan      html  css  js  c++  java
  • signal 和 sigaction

    signal,此函数相对简单一些,给定一个信号,给出信号处理函数则可,当然,函数简单,其功能也相对简单许多,简单给出个函数例子如下:

    1.  1 #include <signal.h>  
    2.  2 #include <stdio.h>  
    3.  3 #include <unistd.h>  
    4.  4   
    5.  5 void ouch(int sig)  
    6.  6 {  
    7.  7     printf("I got signal %d ", sig);  
    8.  8     // (void) signal(SIGINT, SIG_DFL);  
    9.  9     //(void) signal(SIGINT, ouch);  
    10. 10   
    11. 11 }  
    12. 12   
    13. 13   
    14. 14   
    15. 15 int main()  
    16. 16 {  
    17. 17     (void) signal(SIGINT, ouch);  
    18. 18   
    19. 19     while(1)  
    20. 20     {  
    21. 21         printf("hello world... ");  
    22. 22         sleep(1);  
    23. 23     }  
    24. 24 }  

    当然,实际运用中,需要对不同到signal设定不同的到信号处理函数,SIG_IGN忽略 /SIG_DFL默认,这俩宏也可以作为信号处理函数。同时SIGSTOP/SIGKILL这俩信号无法捕获和忽略。注意,经过实验发现,signal函 数也会堵塞当前正在处理的signal,但是没有办法阻塞其它signal,比如正在处理SIG_INT,再来一个SIG_INT则会堵塞,但是来 SIG_QUIT则会被其中断,如果SIG_QUIT有处理,则需要等待SIG_QUIT处理完了,SIG_INT才会接着刚才处理。


    sigaction,这个相对麻烦一些,函数原型如下:

    int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);

    函数到关键就在于struct sigaction

    1. stuct sigaction  
    2. {  
    3.       void (*)(int) sa_handle;  
    4.       sigset_t sa_mask;  
    5.       int sa_flags;  
    6. }  


    1. 1 #include <signal.h>  
    2.   2 #include <stdio.h>  
    3.   3 #include <unistd.h>  
    4.   4   
    5.   5   
    6.   6 void ouch(int sig)  
    7.   7 {  
    8.   8     printf("oh, got a signal %d ", sig);  
    9.   9   
    10.  10     int i = 0;  
    11.  11     for (i = 0; i < 5; i++)  
    12.  12     {  
    13.  13         printf("signal func %d ", i);  
    14.  14         sleep(1);  
    15.  15     }  
    16.  16 }  
    17.  17   
    18.  18   
    19.  19 int main()  
    20.  20 {  
    21.  21     struct sigaction act;  
    22.  22     act.sa_handler = ouch;  
    23.  23     sigemptyset(&act.sa_mask);  
    24.  24     sigaddset(&act.sa_mask, SIGQUIT);  
    25.  25     // act.sa_flags = SA_RESETHAND;  
    26.  26     // act.sa_flags = SA_NODEFER;  
    27.  27     act.sa_flags = 0;  
    28.  28   
    29.  29     sigaction(SIGINT, &act, 0);  
    30.  30   
    31.  31   
    32.  32     struct sigaction act_2;  
    33.  33     act_2.sa_handler = ouch;  
    34.  34     sigemptyset(&act_2.sa_mask);  
    35.  35     act.sa_flags = 0;  
    36.  36     sigaction(SIGQUIT, &act_2, 0);  
    37.  37   
    38.         while(1)  
    39.         {  
    40.              sleep(1);  
    41.         }  
    42.  38     return;  
    43.   
    44.     }  


    1. 阻塞,sigaction函数有阻塞的功能,比如SIGINT信号来了,进入信号处理函数,默认情况下,在信号处理函数未完成之前,如果又来了一个 SIGINT信号,其将被阻塞,只有信号处理函数处理完毕,才会对后来的SIGINT再进行处理,同时后续无论来多少个SIGINT,仅处理一个 SIGINT,sigaction会对后续SIGINT进行排队合并处理。

    2. sa_mask,信号屏蔽集,可以通过函数sigemptyset/sigaddset等来清空和增加需要屏蔽的信号,上面代码中,对信号SIGINT处 理时,如果来信号SIGQUIT,其将被屏蔽,但是如果在处理SIGQUIT,来了SIGINT,则首先处理SIGINT,然后接着处理SIGQUIT。

    3.sa_flags如果取值为0,则表示默认行为。还可以取如下俩值,但是我没觉得这俩值有啥用。

    SA_NODEFER,如果设置来该标志,则不进行当前处理信号到阻塞

    SA_RESETHAND,如果设置来该标志,则处理完当前信号后,将信号处理函数设置为SIG_DFL行为


    下面单独来讨论一下信号屏蔽,记住是屏蔽,不是消除,就是来了信号,如果当前是block,则先不传递给当前进程,但是一旦unblock,则信号会重新到达。

    1. #include <signal.h>  
    2. #include <stdio.h>  
    3. #include <unistd.h>  
    4.   
    5.   
    6.   
    7. static void sig_quit(int);  
    8.   
    9. int main (void) {  
    10.     sigset_t new, old, pend;  
    11.       
    12.     signal(SIGQUIT, sig_quit);  
    13.   
    14.     sigemptyset(&new);  
    15.     sigaddset(&new, SIGQUIT);  
    16.     sigprocmask(SIG_BLOCK, &new, &old);  
    17.   
    18.     sleep(5);  
    19.   
    20.     printf("SIGQUIT unblocked ");  
    21.     sigprocmask(SIG_SETMASK, &old, NULL);  
    22.   
    23.     sleep(50);  
    24.     return 1;  
    25. }  
    26.   
    27. static void sig_quit(int signo) {  
    28.     printf("catch SIGQUIT ");  
    29.     signal(SIGQUIT, SIG_DFL);  
    30. }  


    gcc -g -o mask mask.c 

    ./mask

    ========这个地方按多次ctrl+

    SIGQUIT unblocked

    catch SIGQUIT
    Quit (core dumped)

    ======================

    注意观察运行结果,在sleep的时候,按多次ctrl+,由于sleep之前 block了SIG_QUIT,所以无法获得SIG_QUIT,但是一旦运行sigprocmask(SIG_SETMASK, &old, NULL);则unblock了SIG_QUIT,则之前发送的SIG_QUIT随之而来。

    由于信号处理函数中设置了DFL,所以再发送SIG_QUIT,则直接coredump。

  • 相关阅读:
    linux shell script
    API Gateway : Kong
    rabbitmq management advance lesson
    Python Base HTTP Server
    linux 笔试题
    dll return a string
    friend class
    GCC 编译使用动态链接库 LD
    设计模式学习(四)——单例模式
    简单聊聊TestNG中的并发
  • 原文地址:https://www.cnblogs.com/jkred369/p/4983219.html
Copyright © 2011-2022 走看看