Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat pselect01.c
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <time.h>
- #include <unistd.h>
- #include <errno.h>
- struct tcase {
- struct timespec tv;
- unsigned int iterations;
- };
- static unsigned int monotonic_resolution;
- static struct tcase tcases[] = {
- {{0, 1000000}, 500},
- {{0, 2000000}, 500},
- {{0, 10000000}, 300},
- {{0, 100000000}, 1},
- {{1, 0}, 1},
- };
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
- static struct timespec start_time, stop_time;
- static inline struct timespec tst_timespec_diff(struct timespec t1,
- struct timespec t2)
- {
- struct timespec res;
- res.tv_sec = t1.tv_sec - t2.tv_sec;
- if (t1.tv_nsec < t2.tv_nsec) {
- res.tv_sec--;
- res.tv_nsec = 1000000000 - (t2.tv_nsec - t1.tv_nsec);
- } else {
- res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
- }
- return res;
- }
- static inline long long tst_timespec_to_us(struct timespec t)
- {
- return t.tv_sec * 1000000 + (t.tv_nsec + 500) / 1000;
- }
- static void verify_pselect(int i)
- {
- fd_set readfds;
- struct timespec tv;
- long long requested_us, slept_us = 0;
- int threshold;
- struct tcase *t = &tcases[i];
- FD_ZERO(&readfds);
- FD_SET(0, &readfds);
- tv = t->tv;
- clock_gettime(CLOCK_MONOTONIC, &start_time);
- pselect(0, &readfds, NULL, NULL, &tv, NULL);
- clock_gettime(CLOCK_MONOTONIC, &stop_time);
- printf("expected %lu us, got %llu us\n", tv.tv_nsec/1000, tst_timespec_to_us(tst_timespec_diff(stop_time, start_time)));
- }
- void main(void)
- {
- unsigned int i;
- for (i = 0; i < 4; i++)
- verify_pselect(i);
- }
Add Comment
Please, Sign In to add comment