2003-05-12 16:36:16

by Eric Piel

[permalink] [raw]
Subject: [BUG] (clock_) nanosleep() too short

/* should gives effective times of a nanosleep() */

#include <stdio.h>
#include <sys/time.h>
#include <time.h>

#define USEC_PER_SEC 1000000
#define NSEC_PER_SEC 1000000000L

#define timerdiff(a,b) ((double)((a)->tv_sec - (b)->tv_sec) + \
(double)((a)->tv_usec - (b)->tv_usec)/USEC_PER_SEC)

main()
{
struct timeval pre_time, post_time;
struct timespec req;
double diff;
int i;

req.tv_sec = 0;
req.tv_nsec = 10;
for (i=0; i<15; i++){
req.tv_nsec *= 10;
if (req.tv_nsec >= NSEC_PER_SEC) {
req.tv_nsec = 100;
req.tv_sec++;
}
gettimeofday(&pre_time, NULL);
nanosleep(&req, NULL);
gettimeofday(&post_time, NULL);

diff = timerdiff(&post_time, &pre_time);
printf("%12.9f\t %12.9f\n", (double)req.tv_sec + ((double)req.tv_nsec/NSEC_PER_SEC), diff);
}


}


Attachments:
clock_nanosleep.c (860.00 B)
nanosleep.c (807.00 B)
Download all attachments