forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.16.2.c
45 lines (35 loc) · 958 Bytes
/
10.16.2.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
#include "apue.h"
volatile sig_atomic_t quitflag;
static void sig_int(int signo) {
if (signo == SIGINT) {
printf("\ninterrupt\n");
} else if (signo == SIGQUIT) {
quitflag = 1;
}
}
int main() {
sigset_t newmask, oldmask, zeromask;
if (signal(SIGINT, sig_int) == SIG_ERR) {
err_sys("signal (SIGINT) error");
}
if (signal(SIGQUIT, sig_int) == SIG_ERR) {
err_sys("signal (SIGQUIT) error");
}
sigemptyset(&zeromask);
sigemptyset(&newmask);
sigaddset(&newmask, SIGQUIT);
// 设置 SIGQUIT 屏蔽字
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
err_sys("SIG_BLOCK error");
}
while (quitflag == 0) {
// 修改屏蔽字并挂起进程
// 有哪里怪怪的……
sigsuspend(&zeromask);
}
quitflag = 0;
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
err_sys("SIG_SETMASK error");
}
exit(0);
}