-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path20_1.c
65 lines (50 loc) · 1.69 KB
/
20_1.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
#define _GNU_SOURCE
#include <signal.h>
#include "../tlpi-dist/signals/signal_functions.h" /* Declaration of printSigset() */
#include "tlpi_hdr.h"
static int sigCnt[NSIG]; /* Counts deliveries of each signal */
static volatile sig_atomic_t gotSigint = 0;
/* Set nonzero if SIGINT is delivered */
static void
handler(int sig)
{
if (sig == SIGINT)
gotSigint = 1;
else
sigCnt[sig]++;
}
int main(int argc, char *argv[])
{
int n, numSecs;
sigset_t pendingMask, blockingMask, emptyMask;
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = handler;
act.sa_flags = 0;
printf("%s: PID is %ld\n", argv[0], (long)getpid());
for (n = 1; n < NSIG; n++)
sigaction(n, &act, NULL);
if (argc > 1)
{
numSecs = getInt(argv[1], GN_GT_0, NULL);
sigfillset(&blockingMask);
if (sigprocmask(SIG_SETMASK, &blockingMask, NULL) == -1)
errExit("sigprocmask");
printf("%s: sleeping for %d seconds\n", argv[0], numSecs);
sleep(numSecs);
if (sigpending(&pendingMask) == -1)
errExit("sigpending");
printf("%s: pending signals are: \n", argv[0]);
printSigset(stdout, "\t\t", &pendingMask);
sigemptyset(&emptyMask); /* Unblock all signals */
if (sigprocmask(SIG_SETMASK, &emptyMask, NULL) == -1)
errExit("sigprocmask");
}
while (!gotSigint) /* Loop until SIGINT caught */
continue;
for (n = 1; n < NSIG; n++) /* Display number of signals received */
if (sigCnt[n] != 0)
printf("%s: signal %d caught %d time%s\n", argv[0], n,
sigCnt[n], (sigCnt[n] == 1) ? "" : "s");
exit(EXIT_SUCCESS);
}