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
| #include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
static void sig_handler(int sig, siginfo_t *si, void *uc)
{
printf("Caught signal %d\n", sig);
}
int main(int argc, char *argv[])
{
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
sigset_t newMask, oldMask;
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = sig_handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, NULL) == -1)
{
errExit("sigaction");
}
sigemptyset(&newMask);
sigaddset(&newMask, SIGINT);
if (sigprocmask(SIG_BLOCK, &newMask, &oldMask) == -1)
{
errExit("sigprocmask");
}
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGINT;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1)
{
errExit("timer_create");
}
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (timer_settime(timerid, 0, &its, NULL) == -1)
{
errExit("timer_settime");
}
if (sigprocmask(SIG_SETMASK, &oldMask, NULL) == -1)
{
errExit("sigprocmask");
}
while(1)
{
sleep(10);
}
return 0;
}
|