forked from wenjun1055/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.16.c
51 lines (40 loc) · 978 Bytes
/
10.16.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
volatile sig_atomic_t quitflag;
static void sig_int(int signo)
{
if (SIGINT == signo) {
printf("\ninterrupt\n");
} else if (SIGQUIT == signo) {
quitflag = 1;
}
}
int main(void)
{
sigset_t newmask, oldmask, zeromask;
if (signal(SIGINT, sig_int) == SIG_ERR) {
printf("signal(SIGINT) error\n");
exit(-1);
}
if (signal(SIGQUIT, sig_int) == SIG_ERR) {
printf("signal(SIGQUIT) error\n");
exit(-1);
}
sigemptyset(&zeromask);
sigemptyset(&newmask);
sigaddset(&newmask, SIGQUIT);
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
printf("SIG_BLOCK error\n");
exit(-1);
}
while(0 == quitflag) {
sigsuspend(&zeromask);
}
quitflag = 0;
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
printf("SIG_SETMASK error\n");
exit(-1);
}
return 0;
}