Created
November 2, 2011 05:42
-
-
Save anonymous/1332978 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <inttypes.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
int diff(struct timespec* ts_prev, struct timespec* ts){ | |
return (ts->tv_sec - ts_prev->tv_sec) * 1000 + (ts->tv_nsec - ts_prev->tv_nsec) / 1000000; | |
} | |
int main(){ | |
int c = CLOCK_MONOTONIC; | |
struct timespec ts, ts_prev; | |
struct timespec req; | |
req.tv_sec = 0; | |
req.tv_nsec = 200*1000000; | |
clock_gettime(c, &ts_prev); | |
clock_nanosleep(c, 0, &req, NULL); | |
for(;;){ | |
clock_gettime(c, &ts); | |
int d = diff(&ts_prev, &ts); | |
printf("%d.%d (%d)\n", ts.tv_sec, ts.tv_nsec / 1000000, d); | |
if(d > 220 || d < 190)break; | |
ts_prev = ts; | |
clock_nanosleep(c, 0, &req, NULL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment