#include <unistd.h> // write
#include <sys/time.h> // ITIMER_REAL, struct itimerval, setitimer
#include <time.h> // struct timespec, nanosleep
#include <signal.h> // SIGALRM, struct sigaction, sigemptyset, sigaction
#include <stdio.h> // printf
#include <stdlib.h> // EXIT_SUCCESS
void tmr_hndlr(int);
int main(void)
{
struct itimerval int_time = { .it_interval = { .tv_sec = 3, .tv_usec = 0 },
.it_value = { .tv_sec = 3, .tv_usec = 0 } };
struct timespec sleep_time = { .tv_sec = 2, .tv_nsec = 0 };
struct sigaction sa_timer = { .sa_handler = &tmr_hndlr };
sigemptyset(&sa_timer.sa_mask);
sigaction(SIGALRM, &sa_timer, NULL);
setitimer(ITIMER_REAL, &int_time, NULL);
while (1)
{
nanosleep(&sleep_time, NULL);
printf("Endlosschleife!\n");
}
return EXIT_SUCCESS;
}
/* function to be called on timer interrupt */
void tmr_hndlr(int i)
{
(void)i;
const char str[] = "Timer\n";
write(1, str, sizeof (str));
}