Created
October 12, 2017 20:13
-
-
Save aeppert/6dfae189bffeb573c53893035a715e74 to your computer and use it in GitHub Desktop.
ITIMER with sigaction example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Thanks for posting this, I was having a hard time struggling with understanding how setitimer worked and this helped a lot!!!