Skip to content

Instantly share code, notes, and snippets.

@aeppert
Created October 12, 2017 20:13
Show Gist options
  • Select an option

  • Save aeppert/6dfae189bffeb573c53893035a715e74 to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/6dfae189bffeb573c53893035a715e74 to your computer and use it in GitHub Desktop.
ITIMER with sigaction example
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
static volatile sig_atomic_t g_called;
void g(int sig)
{
g_called += 1;
printf("g called - %d times\n", g_called);
}
int main()
{
struct sigaction act, oact;
g_called = 0;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = g;
sigaction(SIGALRM, &act, &oact);
struct itimerval tv;
tv.it_value.tv_sec = 2; //time of first timer
tv.it_value.tv_usec = 0; //time of first timer
tv.it_interval.tv_sec = 2; //time of all timers but the first one
tv.it_interval.tv_usec = 0; //time of all timers but the first one
setitimer(ITIMER_REAL, &tv, NULL);
sigset_t mask;
sigprocmask(0, NULL, &mask);
sigdelset(&mask, SIGALRM);
while(1) {
sigsuspend(&mask);
}
return 0;
}
@sarahheartley

Copy link
Copy Markdown

Hey! Thanks for posting this, I was having a hard time struggling with understanding how setitimer worked and this helped a lot!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment