linux 訊號signal和sigaction理解

來源:互聯網
上載者:User

標籤:

今天看到unp時發現之前對signal到理解實在淺顯,今天拿來單獨學習討論下。

 

signal,此函數相對簡單一些,給定一個訊號,給出訊號處理函數則可,當然,函數簡單,其功能也相對簡單許多,簡單給出個函數例子如下:

 

[cpp] view plaincopy 
  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\n", 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...\n");  
  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

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

 

[cpp] view plaincopy 
  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\n", sig);  
  9.   9   
  10.  10     int i = 0;  
  11.  11     for (i = 0; i < 5; i++)  
  12.  12     {  
  13.  13         printf("signal func %d\n", 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,則訊號會重新到達。

 

[cpp] view plaincopy 
  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\n");  
  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\n");  
  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 訊號signal和sigaction理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.