Skip to content

Instantly share code, notes, and snippets.

@Forty-Bot
Created January 15, 2019 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Forty-Bot/780f66b442750d6d21dc93d7c8a2dab8 to your computer and use it in GitHub Desktop.
Save Forty-Bot/780f66b442750d6d21dc93d7c8a2dab8 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <error.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
/*
* lhs < rhs: return <0
* lhs == rhs: return 0
* lhs > rhs: return >0
*/
static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
{
if (lhs->tv_sec < rhs->tv_sec)
return -1;
if (lhs->tv_sec > rhs->tv_sec)
return 1;
return lhs->tv_nsec - rhs->tv_nsec;
}
#define test_time(clock) do { \
struct timespec new, old = { 0 }; \
while (1) { \
int err = clock_gettime(clock, &new); \
if (err) { \
error(0, errno, #clock); \
} else if (timespec_compare(&new, &old) < 0) { \
printf(#clock ": new %lu %lu < old %lu %lu\n", \
new.tv_sec, new.tv_nsec, old.tv_sec, old.tv_nsec); \
} \
old = new; \
} \
} while (0)
int main()
{
if (fork()) {
if (fork())
test_time(CLOCK_MONOTONIC_RAW);
else
test_time(CLOCK_MONOTONIC);
} else {
test_time(CLOCK_REALTIME);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment