forked from wenjun1055/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.14.c
85 lines (67 loc) · 1.64 KB
/
10.14.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <time.h>
#include <errno.h>
static void sig_usr1(int), sig_alrm(int), pr_mask(const char *str);
static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjmp;
int main(void)
{
if (signal(SIGUSR1, sig_usr1) == SIG_ERR) {
printf("signal(SIGUSR1) error\n");
exit(-1);
}
if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
printf("signal(SIGALRM) error\n");
exit(-1);
}
pr_mask("starting main: ");
if (sigsetjmp(jmpbuf, 1)) {
pr_mask("ending main: ");
return 0;
}
canjmp = 1;
for (; ;)
pause();
}
static void sig_usr1(int signo)
{
time_t starttime;
if (0 == canjmp) {
return ;
}
pr_mask("starting sig_usr1: ");
alarm(3);
starttime = time(NULL);
for ( ; ; ) {
if (time(NULL) > starttime + 5) {
break;
}
}
pr_mask("finishing sig_usr1: ");
canjmp = 0;
siglongjmp(jmpbuf, 1);
}
static void sig_alrm(int signo)
{
pr_mask("in sig_alarm: ");
}
static void pr_mask(const char *str)
{
sigset_t sigset;
int errno_save;
errno_save = errno;
if (sigprocmask(0, NULL, &sigset) < 0) {
printf("sigprocmask error\n");
exit(-1);
}
printf("%s", str);
if (sigismember(&sigset, SIGINT)) printf("SIGINT ");
if (sigismember(&sigset, SIGQUIT)) printf("SIGQUIT ");
if (sigismember(&sigset, SIGUSR1)) printf("SIGUSR1 ");
if (sigismember(&sigset, SIGALRM)) printf("SIGALRM ");
printf("\n");
errno = errno_save;
}