Skip to content

Instantly share code, notes, and snippets.

@travispaul
Created November 8, 2016 15:20
Show Gist options
  • Save travispaul/09d27ad91540b8b8d7d301b4207ef1b2 to your computer and use it in GitHub Desktop.
Save travispaul/09d27ad91540b8b8d7d301b4207ef1b2 to your computer and use it in GitHub Desktop.
Create timers (until you can't)
// Example:
// cc -lrt timertest.c
// ./a.out 128
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
int
main(int argc, char *argv[])
{
int timers = atoi(argv[1]);
timer_t *timerid;
struct sigevent *sev;
int i;
timerid = malloc((sizeof timerid) * timers);
sev = malloc((sizeof sev) * timers);
for (i = 0; i < timers; i++) {
sev[i].sigev_notify = SIGEV_SIGNAL;
sev[i].sigev_signo = SIGRTMIN;
sev[i].sigev_value.sival_ptr = &timerid[i];
printf("timer_create: %d\n", i);
if (timer_create(CLOCK_MONOTONIC, &sev[i], &timerid[i]) == -1) {
fprintf(stderr, "timer_create: %d failed\n", i);
exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment