Created
January 15, 2019 00:30
-
-
Save Forty-Bot/780f66b442750d6d21dc93d7c8a2dab8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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