My purpose is to execute a while
loop for a defined time (e.g. 90 seconds for this example). It does not have to be exactly 90 s, but 1-2 second inaccuracy is acceptable. I trued to use clock()` function for this purpose:
int main(void){
clock_t start, end;
volatile double elapsed;
start = clock();
int terminate = 1;
while(terminate)
{
end = clock();
elapsed = ((double) (end-start)) / (double) CLOCKS_PER_SEC *1000;
printf("elapsed time:%f\n",elapsed);
if(elapsed >= 90.0)
terminate = 0;
usleep(50000);
}
printf("done..\n");
return 0;
}
when I run it on my laptop (x86, 3.13 kernel, gcc 4.8.2), my stopwatch measures 72 seconds for it to be completed. (1000 was necessary to have the elapsed
in seconds accuracy on my laptop)
When I run it on an ARM device (armv5tejl, 3.12 kernel, gcc 4.6.3) it takes 58 seconds to complete the code. (I needed to used 100
on elapsed
on the armv5).
I run the code on a room temperature, so the clock should be stable. I know that kernel sleeps the threads and has inaccuracy with time to wake them up, etc. Therefore, as I said previously I don't expect to get a perfect timing, but it should have some accuracy.
I had tried to use only usleep
(even nanosleep
) but the resolution was not good as well. At the end I come up with the the bottom code that fetches the system time (hour, minute, second) then calculate the elapsed time. And it works with a good accuracy.
I wonder if there is another solution that would be less costly to use?
typedef struct{
int hour;
int minute;
int second;
} timeInfo;
timeInfo getTimeInfo(void){
timeInfo value2return;
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
value2return.hour = timeinfo->tm_hour;
value2return.minute = timeinfo->tm_min;
value2return.second = timeinfo->tm_sec;
return value2return;
}
int checkElapsedTime(const timeInfo *Start, const timeInfo *Stop, const int Reference){
if(Stop->hour < Start->hour){
printf("1:%d\n", (Stop->hour +24) *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second));
if( ( (Stop->hour +24) *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second)) >= Reference )
return 0; //while(0): terminate the loop
else
return 1; //while(1)
}else{
printf("2:%d\n",Stop->hour *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second));
if( (Stop->hour *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second)) >= Reference )
return 0;
else
return 1;
}
}
int main(void){
timeInfo stop, start = getTimeInfo();
int terminate = 1;
while(terminate)
{
stop = getTimeInfo();
terminate = checkElapsedTime(&start, &stop, 90);
usleep(5000); //to decrease the CPU load
}
printf("terminated\n");
return 0;
}
Lastly, I need to run it inside a pthread
.