#include #include #include #include #include #include #define wait_next_activation pause static void sighand(int s) { } int start_periodic_timer(uint64_t offs, int period) { struct itimerval t; t.it_value.tv_sec = offs / 1000000; t.it_value.tv_usec = offs % 1000000; t.it_interval.tv_sec = period / 1000000; t.it_interval.tv_usec = period % 1000000; signal(SIGALRM, sighand); return setitimer(ITIMER_REAL, &t, NULL); } static void job_body(void) { static int cnt; static uint64_t start; uint64_t t; struct timeval tv; if (start == 0) { gettimeofday(&tv, NULL); start = tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL; } gettimeofday(&tv, NULL); t = tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL; if (cnt && (cnt % 100) == 0) { printf("Avg time: %f\n", (double)(t - start) / (double)cnt); } cnt++; } int main(int argc, char *argv[]) { int res; res = start_periodic_timer(2000000, 5000); if (res < 0) { perror("Start Periodic Timer"); return -1; } while(1) { wait_next_activation(); job_body(); } return 0; }