Possible duplicate:
Linux clock_gettime (CLOCK_MONOTONIC) strange non-monotonous behavior
After some Erlang crash problems, I wrote a program that calls clock_gettime (CLOCK_MONOTONIC, & ts) several times and checks to see if it ever goes back, and unfortunately it sometimes goes back.
This is the test program I am using:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
struct timespec ts_start;
struct timespec ts_end;
int i;
clock_gettime(CLOCK_MONOTONIC, &ts_start);
clock_gettime(CLOCK_MONOTONIC, &ts_end);
for(i = 0; i<10000000; i++) {
if(ts_end.tv_sec <= ts_start.tv_sec
&& ts_end.tv_nsec < ts_start.tv_nsec) {
printf("ERROR!\n");
return 1;
}
ts_start.tv_sec = ts_end.tv_sec;
ts_start.tv_nsec = ts_end.tv_nsec;
clock_gettime(CLOCK_MONOTONIC, &ts_end);
}
printf("OK\n");
return 0;
}
On my Hyper-V virtual machine (kernel 2.6.18-238.12.1.e15, others have tried) it sometimes displays ERROR, but on the physical computer it always displays OK.
Any idea why CLOCK_MONOTONIC will not be monotonous?
source
share