On Thu, 3 Nov 2005, Scott Klement wrote:

How about setting up a 60-second interval timer by calling the setitimer() function. Will that solve the problem?

This is the perfect solution. It uses basically no resources at all and is very accurate. I've even coded up a simple example to demonstrate how this works:

<begin timer.c>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

/* The action that will happen when the timer expires */
void action (int arg);

/* The action that will happen when the user presses Control-C */
void interrupt (int arg);

main ()
  void (*alarmhandler) (int);
  void (*inthandler) (int);
  struct tm *timerstart;
  struct timeval starttime;
  struct timeval curtime;
  struct timeval interval;
  struct itimerval timing;

  /* set up the signal handler */
  alarmhandler = action;
  signal (SIGALRM, alarmhandler);
  inthandler = interrupt;
  signal (SIGINT, inthandler);

  /* set up the timer start time */
  if (gettimeofday (&curtime, NULL) < 0)
      printf ("error occurred getting the time of day!\n");
      return (-1);

  /* set the timer to go off at 13:45 today */
  timerstart = localtime (&curtime.tv_sec);
  timerstart->tm_sec = ;
  timerstart->tm_min = 45;
  timerstart->tm_hour = 13;
  starttime.tv_sec = mktime (timerstart);
  starttime.tv_usec = 0;
  printf ("The timer will go off at %04d-%02d-%02d %02d:%02d:%02d\n",
          timerstart->tm_year + 1900, timerstart->tm_mon + 1,
          timerstart->tm_mday, timerstart->tm_hour, timerstart->tm_min,
  curtime.tv_sec = difftime (starttime.tv_sec, curtime.tv_sec);
  curtime.tv_usec = 0;

  /* set up the timer interval to 1 second */
  interval.tv_sec = 1;
  interval.tv_usec = 0;

  /* create the timer */
  timing.it_interval = interval;
  timing.it_value = curtime;

  printf ("Waiting for the timer (press Control-C to abort)...\n");
  if (setitimer (ITIMER_REAL, &timing, NULL) < 0)
      printf ("error occurred in the timer!\n");
      return (-1);

  /* wait for the signal to occur */
  while (1)
      pause ();
  return (0);

/* This function is where you do whatever work you need to when
 * the timer goes off */
action (int arg)
  printf ("The timer went off!\n");

interrupt (int arg)
  printf ("Shutting down!\n");
  exit (0);
<end timer.c>

This has only been tested on linux, but should be cross platform (of course, on iSeries Control-C won't work, use the attention key instead).

James Rich

It's not the software that's free; it's you.
        - billyskank on Groklaw

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].