-
Notifications
You must be signed in to change notification settings - Fork 7
/
periodic.c
69 lines (51 loc) · 1.23 KB
/
periodic.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
struct periodic_info
{
sigset_t alarm_sig;
};
static int make_periodic (unsigned int period, struct periodic_info *info)
{
int ret;
struct itimerval value;
/* Block SIGALRM in this thread */
sigemptyset (&(info->alarm_sig));
sigaddset (&(info->alarm_sig), SIGALRM);
pthread_sigmask (SIG_BLOCK, &(info->alarm_sig), NULL);
/* Set the timer to go off after the first period and then
repetitively */
#if 0
value.it_value.tv_sec = period/1000000;
value.it_value.tv_usec = period%1000000;
value.it_interval.tv_sec = period/1000000;
value.it_interval.tv_usec = period%1000000;
#endif
value.it_value.tv_sec = 1;
value.it_value.tv_usec = 0;
value.it_interval.tv_sec = 1;
value.it_interval.tv_usec = 0;
ret = setitimer (ITIMER_REAL, &value, NULL);
if (ret != 0)
perror ("Failed to set timer");
return ret;
}
static void wait_period (struct periodic_info *info)
{
int sig;
/* Wait for the next SIGALRM */
sigwait (&(info->alarm_sig), &sig);
}
int main(int argc,char*argv[])
{
struct periodic_info info;
make_periodic (10000, &info);
while (1)
{
/* Do useful work */
printf("hello world!\n");
wait_period (&info);
}
}