Problem with usleep's precision.

Ulrich Prakash uprakash@npd.hcltech.com
Tue Apr 15 11:37:00 GMT 2003


Hi All,

I want to sleep my program for around 20 milliseconds period.
But usleep doesn't work correctly...

man page tells that usleep wud sleep for the microseconds time,but
when i gave usleep(1),it actually sleeps for 12000-14000
microseconds..

When i gave usleep(20000),it sleeps for 27000-29000microseconds
instead of 20,000...

I also tried using nanosleep,but nanosleep for 1 nanoseconds also
sleep for 12000-14000 microseconds.

Attaching the sample code also here...
/***** testusleep.c*********/
#include <sys/timeb.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>

int main()
{

    struct timeval time1, time2;

    gettimeofday (&time1, 0x0);
    usleep (1);
    gettimeofday (&time2, 0x0);

    printf ("time difference in seconds is %ld\n", (time2.tv_sec -
time1.tv_sec));
    printf ("time difference in microseconds is %ld\n", (time2.tv_usec
- time1.tv_usec));

}

My pc detail is:
Pentium III(i686),701Mhz speed..
OS-Red Hat Linux7.2
gcc-version2.96

Please let me know if there is a way of achiveing a sleep for a
SPECIFIC precision value in milliseconds range...

Thanks,
Prakash.




More information about the Gcc-help mailing list