PDA

View Full Version : C calling a function at exact every second?


newbie14
8th January 2012, 01:21 PM
I need to call a function every second exactly as I want to store the data based on the every second so I cannot miss the second? What is best method in C?

Below is a skeleton of the timer_create method is this reliable enough?

#include <stdio.h>
#include <time.h>
#include <signal.h>

timer_t gTimerid;

void start_timer(void)
{


struct itimerspec value;

value.it_value.tv_sec = 1;
value.it_value.tv_nsec = 0;

value.it_interval.tv_sec = 1;
value.it_interval.tv_nsec = 0;

timer_create (CLOCK_REALTIME, NULL, &gTimerid);

timer_settime (gTimerid, 0, &value, NULL);

}

void stop_timer(void)
{


struct itimerspec value;

value.it_value.tv_sec = 0;
value.it_value.tv_nsec = 0;

value.it_interval.tv_sec = 0;
value.it_interval.tv_nsec = 0;

timer_settime (gTimerid, 0, &value, NULL);


}


void timer_callback(int sig) {

printf(" Catched timer signal: %d ... !!\n", sig);

}

int main(int ac, char **av)
{
(void) signal(SIGALRM, timer_callback);
start_timer();
while(1);
}