-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleepTimer.c
104 lines (80 loc) · 1.96 KB
/
sleepTimer.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <time.h>
#include <unistd.h>
static void my_sleep(struct timespec *exp, unsigned int *timeAdjust)
{
struct timespec delay;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
*timeAdjust = 0;
if ((exp->tv_sec > now.tv_sec) || ((exp->tv_sec == now.tv_sec) && (exp->tv_nsec > now.tv_nsec)))
{
delay.tv_sec = 0;
if (exp->tv_nsec < now.tv_nsec)
delay.tv_nsec = exp->tv_nsec + 1000000000 - now.tv_nsec;
else
delay.tv_nsec = exp->tv_nsec - now.tv_nsec;
if ((exp->tv_sec - now.tv_sec) <= 1)
{
if (delay.tv_nsec >= 10000000)
nanosleep(&delay, 0);
else
{
exp->tv_sec = now.tv_sec;
exp->tv_nsec = now.tv_nsec;
*timeAdjust = delay.tv_nsec;
}
}
else
{
delay.tv_nsec = 10000000;
nanosleep(&delay, 0);
clock_gettime(CLOCK_REALTIME, exp);
printf("[P]Time changed - Last: %ld.%09ld ,Now:%ld.%09ld\n", now.tv_sec, now.tv_nsec, exp->tv_sec, exp->tv_nsec);
}
}
else
{
unsigned int diff = now.tv_sec - exp->tv_sec;
if (diff > 1)
{
delay.tv_sec = 0;
delay.tv_nsec = 10000000;
nanosleep(&delay, 0);
clock_gettime(CLOCK_REALTIME, exp);
printf("[P]Time changed - Last: %ld.%09ld ,Now:%ld.%09ld\n", now.tv_sec, now.tv_nsec, exp->tv_sec, exp->tv_nsec);
}
else
{
if (((1000000000*diff) + now.tv_nsec - exp->tv_nsec)/1000000 >= 500)
{
delay.tv_sec = 0;
delay.tv_nsec = 10000000;
nanosleep(&delay, 0);
clock_gettime(CLOCK_REALTIME, exp);
}
}
}
}
int main()
{
int i, ret;
unsigned int timeAdjust = 0;
struct timespec exp, now;
clock_gettime(CLOCK_REALTIME, &exp);
while(1)
{
exp.tv_nsec = exp.tv_nsec + 1000000000 + timeAdjust;
if (exp.tv_nsec >= 1000000000)
{
exp.tv_nsec -= 1000000000;
exp.tv_sec++;
}
clock_gettime(CLOCK_REALTIME, &now);
printf("------------ time = %u, sec=%u, nsec=%u\n", time(0), now.tv_sec, now.tv_nsec);
for(i=0; i<3; i++)
usleep(i*50);
my_sleep(&exp, &timeAdjust);
}
return 0;
}