1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
/*
** NOTE NOTE NOTE NOTE
**
** you will need to link: (use LDFLAGS)
**
** -lrt -lpthread
*/
#define SZ 46
long int overruns[SZ];
void callback(union sigval arg) {
timer_t * p = (timer_t *) arg.sival_ptr;;
int delay = timer_getoverrun(*p);
printf("delay = %d\n", delay);
// count overruns, which are timer intervals skipped
if (delay<SZ) {
overruns[delay]++;
} else {
timer_delete(*p);
return ;
}
return;
}
timer_t timerid;
int main (int argc, char ** argv) {
struct sigevent se;
struct itimerspec ts;
long int nsec;
long int msec;
long int sec;
if(argc<2) {
fputs("time milliseconds?\n",stderr);
exit(1);
}
msec = atoi(argv[1]);
fprintf(stderr, "got %ld\n", msec);
sec = (msec / 1000);
msec %= 1000;
nsec = msec * 1e06;;
se.sigev_notify = SIGEV_THREAD;
se.sigev_value.sival_ptr = &timerid;
se.sigev_notify_function = callback;
se.sigev_notify_attributes = NULL;
if ( -1 == timer_create(CLOCK_REALTIME, &se, &timerid)) {
perror("timer_create:");
return(1);
}
ts.it_value.tv_sec = sec;
ts.it_value.tv_nsec = nsec;
ts.it_interval.tv_sec = sec;
ts.it_interval.tv_nsec = nsec ;
printf("nanosex = %ld\n", ts.it_interval.tv_nsec);
printf("seconds = %ld\n", ts.it_interval.tv_sec);
if (-1 == timer_settime(timerid, 0, &ts, NULL)) {
perror("timer_settime:");
}
getchar(); // press a key to stop
int t;
for(t=0; t<SZ; t++) {
printf("overruns[%02d] = %ld\n", t, overruns[t]);
}
return 0;
}
|