2

An application (running on linux 2.6.31) is paced using usleep calls. However, it looks like it is not resilient to a date change (either using date from a shell or using clock_settime from another process). What is the right thing to do to avoid the application to be locked ?

edit1

Say the application wants to do something every 100ms. When the job is done, it calls gettimeofday, and deduce the remaining ms to sleep until the next 100ms slice, then it calls usleep.

Laurent G
  • 2,994
  • 1
  • 18
  • 10
  • How is usleep() typically called, i.e. how often is the application awake and running? Try explaining more what the problem is, what goes wrong when the clock is changed? – unwind Mar 29 '11 at 13:01
  • @unwind. Well, I edited the question. Hoping it is more clear. – Laurent G Mar 29 '11 at 13:08

1 Answers1

2

If you have to be sure about time interval, not regarding the current date/time, you have to use this call :

struct timespec tim;
uint64_t microsec;

clock_gettime( CLOCK_MONOTONIC, &tim );
microsec= 1000000ULL * tim.tv_sec + tim.tv_nsec / 1000;

It is your system uptime, real full "hardware" time ( cannot be modified ).

About your periodic routine, you can set the periodic handler with alarm() and setitimer().

Massimo
  • 3,171
  • 3
  • 28
  • 41