title: Calling a function at exact every second using a timer?
body: <p>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? </p>
  
  <p>Below is a skeleton of the timer_create method is this reliable enough?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;signal.h&gt;
  
  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, &amp;gTimerid);    
    timer_settime (gTimerid, 0, &amp;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, &amp;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);
  }
  </code></pre>