1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <signal.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
void handle_error(char* file, char* line)
{
printf("Error in the file %s, at the line %s : %s\n", file, line, strerror(errno));
exit(-1);
}
void increment_and_print(int sig, siginfo_t* si, void*)
{
(*((int*) si->si_value.sival_ptr))++;
printf("Compteur = %d\n", *((int*) si->si_value.sival_ptr));
}
/*
unsigned incr(unsigned int nLoops, double* pCounter, bool* pStop)
{
int counter = si->si_value;
counter++;
printf("Compteur = %d\n", counter);
}*/
int main()
{
struct sigaction signal_action;
signal_action.sa_flags = SA_SIGINFO;
signal_action.sa_sigaction = increment_and_print;
sigemptyset(&signal_action.sa_mask);
sigaction(SIGRTMIN, &signal_action, NULL);
sigevent_t event;
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_signo = SIGRTMIN;
int counter = 0;
event.sigev_value.sival_ptr = (void*) &counter;
timer_t timer_id;
timer_create(CLOCK_REALTIME, &event, &timer_id);
struct itimerspec times;
times.it_value.tv_sec = 0;
times.it_value.tv_nsec = 0;
times.it_interval.tv_nsec = 500000000u;
times.it_interval.tv_sec = 0;
timer_settime(timer_id, 0, ×, NULL);
//sleep(3);
// timer_destroy();
return 0;
} |
Partager