/* 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);
}
}