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

void MyAlarm();
void setAlarm(long milleseconds);

main() {

  /* Catch ALARM signal.  If not caught, process terminates. */
  signal(SIGALRM, MyAlarm);
  setAlarm(500);

  while (1) {

    pause();			/* hangout until signal happens */

    printf("We got a signal!\n");

  }

}

/* set the alarm to go off in the specified number of msecs */
void setAlarm(long milleseconds) {
  struct itimerval length;
  
  length.it_interval.tv_sec = 0;
  length.it_interval.tv_usec = 0;
  length.it_value.tv_sec = milleseconds/1000;
  length.it_value.tv_usec = (milleseconds % 1000) * 1000;
  
  if (setitimer(ITIMER_REAL,&length,NULL) < 0)
    perror("setitimer");
}

void MyAlarm()
{
   printf("Alarm went off!\n");
   setAlarm(500);
}

