linux cプログラミング信号処理のいくつかの例signal sigation
57211 ワード
ソース: http://www.nenew.net/linux-c-signal-sigaction-examples.html
まず教科書の上の二つの小さい実例を載せて、メモしてください。
1.signalの使用
キャプチャctrl+cが送信するSIGINTとctrl+\が送信するSIGQUITT信号
同様にctrl+cをキャプチャして送信するSIGINTとctrl+\が送信するSIGQUITT信号です。
デフォルトでは信号の状態がブロックされています。この時は入力は一切行われません。ctrl+cとctrl+\は実行されませんが、任意の文字を入力して車に戻ります。元の信号は直ちに実行されます。任意の文字を入力してSIGを解除することができます。BLOCK状態で、SIG_を実行します。INTとSIG_QUITT,SIG_INTは私たち自身の関数myを呼び出します。funcですので、プログラムはずっと終了しませんが、SIG_QUITTは相変わらずシステムの関数ですので、正常に表現できます。
まず教科書の上の二つの小さい実例を載せて、メモしてください。
1.signalの使用
キャプチャctrl+cが送信するSIGINTとctrl+\が送信するSIGQUITT信号
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
-
- void my_func(int sign_no) {
- if (sign_no == SIGINT) {
- printf("I have get SIGINT
");
- } else if (sign_no == SIGQUIT) {
- printf("I have get SIGQUIT
");
- }
- }
-
- int main() {
- printf("waiting for signal SIGINT or SIGQUIT ...
");
- signal(SIGINT, my_func);
- signal(SIGQUIT, my_func);
- pause();
- exit(0);
- }
2.sigactionの使用<—感覚はsignalの一種の変種です。同様にctrl+cをキャプチャして送信するSIGINTとctrl+\が送信するSIGQUITT信号です。
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
-
- void my_func(int sign_no) {
- if (sign_no == SIGINT) {
- printf("I have get SIGINT
");
- } else if (sign_no == SIGQUIT) {
- printf("I have get SIGQUIT
");
- }
- }
-
- int main() {
- struct sigaction action;
- printf("waiting for signal SIGINT or SIGQUIT ...
");
- action.sa_handler = my_func;
- sigemptyset(&action.sa_mask);
- action.sa_flags = 0;
- sigaction(SIGINT, &action, 0);
- sigaction(SIGQUIT, &action, 0);
- pause();
- exit(0);
- }
3.信号セットデフォルトでは信号の状態がブロックされています。この時は入力は一切行われません。ctrl+cとctrl+\は実行されませんが、任意の文字を入力して車に戻ります。元の信号は直ちに実行されます。任意の文字を入力してSIGを解除することができます。BLOCK状態で、SIG_を実行します。INTとSIG_QUITT,SIG_INTは私たち自身の関数myを呼び出します。funcですので、プログラムはずっと終了しませんが、SIG_QUITTは相変わらずシステムの関数ですので、正常に表現できます。
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <signal.h>
-
- void my_func(int signum) {
- printf("If you want to quit ,please try SIGQUIT
");
- }
-
- int main() {
- sigset_t set, pendset;
- struct sigaction action1, action2;
-
- if (sigemptyset(&set) < 0) {
- perror("sigemptyset");
- exit(1);
- }
-
- if (sigaddset(&set, SIGQUIT) < 0) {
- perror("sigaddset");
- exit(1);
- }
-
- if (sigaddset(&set, SIGINT) < 0) {
- perror("sigaddset");
- exit(1);
- }
-
- if (sigismember(&set, SIGINT)) {
- sigemptyset(&action1.sa_mask);
- action1.sa_handler = my_func;
- action1.sa_flags = 0;
- sigaction(SIGINT, &action1, 0);
- }
-
- if (sigismember(&set, SIGQUIT)) {
- sigemptyset(&action2.sa_mask);
- action2.sa_handler = SIG_DFL;
- action2.sa_flags = 0;
- sigaction(SIGQUIT, &action2, 0);
- }
-
- if (sigprocmask(SIG_BLOCK, &set, NULL) < 0) {
- perror("sigprocmask");
- exit(1);
- } else {
- printf("Signal set was blocked,Press any key!");
- getchar();
- }
- if (sigprocmask(SIG_UNBLOCK, &set, NULL) < 0) {
- perror("sigprocmask");
- exit(1);
- } else {
- printf("Signal set is unblock state
");
- }
- while (1)
- ;
- exit(0);
- }
-