#include #include #include #include #include static volatile sig_atomic_t g_run = 1; static void stop(int s){ (void)s; g_run = 0; } static double mono(void){ struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (double)t.tv_sec + (double)t.tv_nsec/1e9; } int main(void){ signal(SIGINT, stop); signal(SIGTERM, stop); struct timespec base; clock_gettime(CLOCK_REALTIME, &base); base.tv_nsec = 0; base.tv_sec += 1; double t0 = mono(); uint64_t tick = 0; while (g_run){ struct timespec dl = base; dl.tv_sec += (time_t)tick; int rc = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &dl, NULL); if (rc != 0 && rc != EINTR) break; double now = mono(); double drift = (now - t0 - (double)tick) * 1e3; struct timespec r; clock_gettime(CLOCK_REALTIME, &r); struct tm mv; localtime_r(&r.tv_sec, &mv); printf("[%05" PRIu64 "] t+%09.6fs drift %+7.2fms %02d:%02d:%02d\n", tick, now - t0, drift, mv.tm_hour, mv.tm_min, mv.tm_sec); fflush(stdout); tick++; } return 0; }