forked from superjamie/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer-skeleton.c
47 lines (41 loc) · 1.08 KB
/
timer-skeleton.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
/* timer_create skeleton
*
* this is THE ONLY WAY to get something to actually happen every
* second within the same millisecond, and not every second plus
* a few milliseconds here or there so you eventually skip a count
*
* don't forget to link with -lrt
*
*/
#include <stdio.h>
#include <time.h>
#include <signal.h>
timer_t gTimerid;
void start_timer(void)
{
struct itimerspec value;
value.it_value.tv_sec = 1; // initial wait
value.it_value.tv_nsec = 0;
value.it_interval.tv_sec = 1; // timer interval
value.it_interval.tv_nsec = 0;
timer_create (CLOCK_REALTIME, NULL, &gTimerid);
timer_settime (gTimerid, 0, &value, NULL);
}
void stop_timer(void)
{
struct itimerspec value;
value.it_value.tv_sec = 0;
value.it_value.tv_nsec = 0;
value.it_interval.tv_sec = 0;
value.it_interval.tv_nsec = 0;
timer_settime (gTimerid, 0, &value, NULL);
}
void timer_callback(int sig) {
printf("Caught timer signal: %d ... !!\n", sig);
}
int main(int ac, char **av)
{
(void) signal(SIGALRM, timer_callback);
start_timer();
while(1);
}