Skip to content

Instantly share code, notes, and snippets.

@kuenishi
Forked from anonymous/gist:1332978
Created November 2, 2011 06:07
Show Gist options
  • Save kuenishi/1333008 to your computer and use it in GitHub Desktop.
Save kuenishi/1333008 to your computer and use it in GitHub Desktop.
#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