本文共 5712 字,大约阅读时间需要 19 分钟。
[root@db-172-16-3-150 zzz]# cat a.c#include执行 :#include #include #include #include #include #define TIMEOUT 5void error(char * msg) { fprintf(stdout, "%s: %s\n", msg, strerror(errno)); exit(1);}void alrm_handler(int sig) { fprintf(stdout, "sig:%i, Sorry, TIME IS UP. Please enter your name within %i second.\n", sig, TIMEOUT); alarm(TIMEOUT); //exit(1);}int reg_handler(int sig, void (*handler)(int)) { struct sigaction action; action.sa_handler = handler; sigemptyset(&action.sa_mask); action.sa_flags = 0; return sigaction(sig, &action, NULL);}int main() { char fname[80]; char lname[80]; alarm(TIMEOUT); if( reg_handler(SIGALRM, alrm_handler) == -1 ) { error("reg_handler error"); } fprintf(stdout, "please enter your first name: "); fgets(fname, 80, stdin); // 第一次超时后, 从下面开始执行. fprintf(stdout, "please enter your last name: "); fgets(lname, 80, stdin); // 第二次超时后, 从下面开始执行. fprintf(stdout, "Hello, %s.%s\n", lname, fname); return 0;}
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./a.c -o a[root@db-172-16-3-150 zzz]# ./aplease enter your first name: sig:14, Sorry, TIME IS UP. Please enter your name within 5 second. please enter your last name: sig:14, Sorry, TIME IS UP. Please enter your name within 5 second.Hello, ?
[root@db-172-16-3-150 zzz]# cat a.c#include在程序中要忽略某些信号或者要还原信号处理函数怎么办呢?#include #include #include #include #include #define TIMEOUT 5void error(char * msg) { fprintf(stdout, "%s: %s\n", msg, strerror(errno)); exit(1);}void alrm_handler(int sig) { fprintf(stdout, "sig:%i, Sorry, TIME IS UP. Please enter your name within %i second.\n", sig, TIMEOUT); // 一般在信号处理函数中使用exit, 这么做的话程序就直接退出了. 当然你可以选择不用. 例如定时执行其他任务的, 就不需要exit. exit(1);}int reg_handler(int sig, void (*handler)(int)) { struct sigaction action; action.sa_handler = handler; sigemptyset(&action.sa_mask); action.sa_flags = 0; return sigaction(sig, &action, NULL);}int main() { char fname[80]; char lname[80]; if( reg_handler(SIGALRM, alrm_handler) == -1 ) { error("reg_handler error"); } alarm(TIMEOUT); fprintf(stdout, "please enter your first name: "); fscanf(stdin, "%80s", fname); // 时间在这里剩余2秒时, 重新调用alarm(TIMEOUT) 又会把计时器调整为5秒, 覆盖前面的剩余时间. alarm(TIMEOUT); fprintf(stdout, "please enter your last name: "); fscanf(stdin, "%80s", lname); fprintf(stdout, "Hello, %s.%s\n", lname, fname); return 0;}
sa_handler specifies the action to be associated with signum and may be SIG_DFL for the default action, SIG_IGN to ignore this signal, or a pointer to a signal handling function. This function receives the signal number as its only argument.
if( reg_handler(SIGINT, SIG_IGN) == -1 ) { error("reg_handler error"); }
NAME sigaction - examine and change a signal actionSYNOPSIS #includeint sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);DESCRIPTION The sigaction() system call is used to change the action taken by a process on receipt of a specific signal. signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP.
[root@db-172-16-3-150 zzz]# cat a.c#include其他 :#include #include #include #include #include #include #define TIMEOUT 5int score = 0;void end_game(int sig) { printf("\nsig:%i, Final score: %i\n", sig, score); exit(0);}int catch_signal (int sig, void (*handler)(int)) { struct sigaction action; action.sa_handler = handler; sigemptyset(&action.sa_mask); action.sa_flags = 0; return sigaction (sig, &action, NULL);}void times_up(int sig) { fprintf(stdout, "\nsig:%i, TIME'S UP!", sig); raise(SIGINT);}void error(char *msg) { fprintf(stderr, "%s: %s\n", msg, strerror(errno)); exit(1);}int main() { catch_signal(SIGALRM, times_up); catch_signal(SIGINT, end_game); srandom (time (0)); while(1) { int a = random() % 11; int b = random() % 11; char txt[4]; alarm(TIMEOUT); printf("\nWhat is %i times %i? ", a, b); fgets(txt, 4, stdin); int answer = atoi(txt); if (answer == a * b) score++; else printf("\nWrong! Score: %i\n", score); } return 0;}
Q: Are signals always received in the same order they are sent?A: Not if they are sent very close together. The operating system might choose to reorder the signals if it thinks one is more important than the others.Q: Is that always true?A: It depends on the platform. On most versions of Cygwin, for example, the signals will always be sent and received in the same order. But in general, you shouldn’t rely on it.Q: If I send the same signal twice, will it be received twice by the process?A: Again, it depends. On Linux and the Mac, if the same signal is repeated very quickly, the kernel might choose to only send the signal once to the process. On Cygwin, it will always send both signals. But again, you should not assume that just because you sent the same signal twice, it will be received twice
转载地址:http://zmbql.baihongyu.com/