Skip to content

Instantly share code, notes, and snippets.

@szaydel
Last active December 18, 2020 14:16
Show Gist options
  • Select an option

  • Save szaydel/1b7db9437caab3a2252fdee04a85005b to your computer and use it in GitHub Desktop.

Select an option

Save szaydel/1b7db9437caab3a2252fdee04a85005b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static void
sigalrmHandler(int sig) {
printf("Signal: %d\n", sig);
}
int main(void) {
long long freq_nanosecs = 100000000;
struct itimerval itv = {0};
struct itimerval oitv = {0};
//itv.it_interval.tv_sec = freq_nanosecs / 1000000000;
//itv.it_value.tv_usec = freq_nanosecs % 1000000;
itv.it_value.tv_sec = 5;
itv.it_value.tv_usec = 0;
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500000;
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sigalrmHandler;
if (sigaction(SIGALRM, &sa, NULL) == -1)
errExit("sigaction");
setitimer(ITIMER_REAL, &itv, &oitv);
while(1) {
struct timespec ts = {0};
clock_gettime(CLOCK_REALTIME, &ts);
printf("%ld\n", ts.tv_sec % 2);
setitimer(ITIMER_REAL, &itv, &oitv);
printf("%ld\n", oitv.it_value.tv_sec);
sleep(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment