clock_gettime doesn't always return an updated time
Created by: glegris
Reproduction: Inside a process, successive calls of clock_gettime gives the same time value, except if sleep is called between the calls. The code below allows to reproduce the 2 behaviors.
-
Compile the test code: i386-elf-redox-gcc -o time_test time_test.c
-
Test 1 currently fails (simple loop between two calls of clock_gettime) time_test
-
Test 2 (sleep between two clock_gettime calls) time_test sleep
Expected behavior:
timespec should be updated between successive calls to clock_gettime
I provide the source code below as attachment doesn't work for me (!)
include <stdlib.h>
include <stdio.h>
include <dirent.h>
include <sys/time.h>
include <time.h>
include <unistd.h>
int main(int argc, char ** argv){ printf("Test %d\n", argc); int i; for(i = 0; i < argc; i++){ printf("%d: %s\n", i, argv[i]); }
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
int start_time = (int)tp.tv_sec;
printf("clock_gettime start %d %d\n", (int)tp.tv_sec, (int)tp.tv_nsec);
int a = 64;
if (argc > 1) {
sleep(2);
} else {
for(i = 1; i < 100000000; i++){
a += a / i;
}
}
clock_gettime(CLOCK_REALTIME, &tp);
printf("clock_gettime end %d %d\n", (int)tp.tv_sec, (int)tp.tv_nsec);
if ((int)tp.tv_sec - start_time < 2) {
printf("Oops ! time was not updated !\n");
} else {
printf("OK\n");
}
printf("%i\n", a);
}