-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23-4-use_sigwaitinfo.c
69 lines (53 loc) · 2.07 KB
/
23-4-use_sigwaitinfo.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
#define _POSIX_C_SOURCE 199309
#include <signal.h>
#include <time.h>
#include "curr_time.h" /* Declares currTime() */
#include "itimerspec_from_str.h" /* Declares itimerspecFromStr() */
#include "tlpi_hdr.h"
#define TIMER_SIG SIGRTMAX /* Our timer notification signal */
int
main(int argc, char *argv[])
{
struct itimerspec ts;
struct sigaction sa;
struct sigevent sev;
timer_t *tidlist;
int j;
if (argc < 2)
usageErr("%s secs[/nsecs][:int-secs[/int-nsecs]]...\n", argv[0]);
tidlist = calloc(argc - 1, sizeof(timer_t));
if (tidlist == NULL)
errExit("malloc");
/* Establish handler for notification signal */
/* Create and start one timer for each command-line argument */
sev.sigev_notify = SIGEV_SIGNAL; /* Notify via signal */
sev.sigev_signo = TIMER_SIG; /* Notify using this signal */
for (j = 0; j < argc - 1; j++) {
itimerspecFromStr(argv[j + 1], &ts);
sev.sigev_value.sival_ptr = &tidlist[j];
/* Allows handler to get ID of this timer */
if (timer_create(CLOCK_REALTIME, &sev, &tidlist[j]) == -1)
errExit("timer_create");
printf("Timer ID: %ld (%s)\n", (long) tidlist[j], argv[j + 1]);
if (timer_settime(tidlist[j], 0, &ts, NULL) == -1)
errExit("timer_settime");
}
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, TIMER_SIG);
if (sigprocmask(SIG_BLOCK, mask, NULL) == -1)
errExit("sigpromask");
for (;;) /* Wait for incoming timer signals */
{
siginfo_t info;
if (sigwaitinfo(mask, &info) == -1)
errExit("sigwaitinfo");
timer_t *tidptr;
tidptr = si->si_value.sival_ptr;
/* UNSAFE: This handler uses non-async-signal-safe functions
(printf(); see Section 21.1.2) */
printf("[%s] Got signal %d\n", currTime("%T"), sig);
printf(" *sival_ptr = %ld\n", (long) *tidptr);
printf(" timer_getoverrun() = %d\n", timer_getoverrun(*tidptr));
}
}