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

void handler(int signo)
{
  static int beeps = 0; /* static e' importante perche' ..*/

  printf("BEEP\n");
  if (++beeps < 5)
    alarm(1);
  else
    {
      printf("BOOM!\n");
      exit(0);
    }
}


int main()
{
  struct timespec ts;

  signal(SIGALRM, handler);
  alarm(1); 

  ts.tv_sec = 20;
  ts.tv_nsec = 0;

  while(1)
    nanosleep (&ts, NULL);

  exit(0);
}

// RICORDARE CHE PER INTERROMPERE IL PRG SI DEVE USARE Ctrl^z (sospensione) 
// e poi da Shell usare kill -9 <PID del prg> se no prg resta sospeso in eterno!
