linux cプログラミング信号処理のいくつかの例signal sigation

57211 ワード

ソース: http://www.nenew.net/linux-c-signal-sigaction-examples.html
まず教科書の上の二つの小さい実例を載せて、メモしてください。
1.signalの使用
キャプチャctrl+cが送信するSIGINTとctrl+\が送信するSIGQUITT信号

  
  
  
  
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <signal.h> 
  4.  
  5. void my_func(int sign_no) { 
  6.     if (sign_no == SIGINT) { 
  7.         printf("I have get SIGINT
    "
    ); 
  8.     } else if (sign_no == SIGQUIT) { 
  9.         printf("I have get SIGQUIT
    "
    ); 
  10.     } 
  11.  
  12. int main() { 
  13.     printf("waiting for signal SIGINT or SIGQUIT ... 
    "
    ); 
  14.     signal(SIGINT, my_func); 
  15.     signal(SIGQUIT, my_func); 
  16.     pause(); 
  17.     exit(0); 
2.sigactionの使用<—感覚はsignalの一種の変種です。
同様にctrl+cをキャプチャして送信するSIGINTとctrl+\が送信するSIGQUITT信号です。

  
  
  
  
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <signal.h> 
  4.  
  5. void my_func(int sign_no) { 
  6.     if (sign_no == SIGINT) { 
  7.         printf("I have get SIGINT
    "
    ); 
  8.     } else if (sign_no == SIGQUIT) { 
  9.         printf("I have get SIGQUIT
    "
    ); 
  10.     } 
  11.  
  12. int main() { 
  13.     struct sigaction action; 
  14.     printf("waiting for signal SIGINT or SIGQUIT ... 
    "
    ); 
  15.     action.sa_handler = my_func; 
  16.     sigemptyset(&action.sa_mask); 
  17.     action.sa_flags = 0; 
  18.     sigaction(SIGINT, &action, 0); 
  19.     sigaction(SIGQUIT, &action, 0); 
  20.     pause(); 
  21.     exit(0); 
3.信号セット
デフォルトでは信号の状態がブロックされています。この時は入力は一切行われません。ctrl+cとctrl+\は実行されませんが、任意の文字を入力して車に戻ります。元の信号は直ちに実行されます。任意の文字を入力してSIGを解除することができます。BLOCK状態で、SIG_を実行します。INTとSIG_QUITT,SIG_INTは私たち自身の関数myを呼び出します。funcですので、プログラムはずっと終了しませんが、SIG_QUITTは相変わらずシステムの関数ですので、正常に表現できます。

  
  
  
  
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <sys/types.h> 
  4. #include <unistd.h> 
  5. #include <signal.h> 
  6.  
  7. void my_func(int signum) { 
  8.     printf("If you want to quit ,please try SIGQUIT
    "
    ); 
  9.  
  10. int main() { 
  11.     sigset_t set, pendset; 
  12.     struct sigaction action1, action2; 
  13.  
  14.     if (sigemptyset(&set) < 0) { 
  15.         perror("sigemptyset"); 
  16.         exit(1); 
  17.     } 
  18.  
  19.     if (sigaddset(&set, SIGQUIT) < 0) { 
  20.         perror("sigaddset"); 
  21.         exit(1); 
  22.     } 
  23.  
  24.     if (sigaddset(&set, SIGINT) < 0) { 
  25.         perror("sigaddset"); 
  26.         exit(1); 
  27.     } 
  28.  
  29.     if (sigismember(&set, SIGINT)) { 
  30.         sigemptyset(&action1.sa_mask); 
  31.         action1.sa_handler = my_func; 
  32.         action1.sa_flags = 0; 
  33.         sigaction(SIGINT, &action1, 0); 
  34.     } 
  35.  
  36.     if (sigismember(&set, SIGQUIT)) { 
  37.         sigemptyset(&action2.sa_mask); 
  38.         action2.sa_handler = SIG_DFL; 
  39.         action2.sa_flags = 0; 
  40.         sigaction(SIGQUIT, &action2, 0); 
  41.     } 
  42.  
  43.     if (sigprocmask(SIG_BLOCK, &set, NULL) < 0) { 
  44.         perror("sigprocmask"); 
  45.         exit(1); 
  46.     } else { 
  47.         printf("Signal set was blocked,Press any key!"); 
  48.         getchar(); 
  49.     } 
  50.     if (sigprocmask(SIG_UNBLOCK, &set, NULL) < 0) { 
  51.         perror("sigprocmask"); 
  52.         exit(1); 
  53.     } else { 
  54.         printf("Signal set is unblock state
    "
    ); 
  55.     } 
  56.     while (1) 
  57.         ; 
  58.     exit(0);