#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

void sigprof(int i) { _exit(0); }

/**
 * Usage: ./<filename> [time in seconds]
 */
int main(int argc, char ** argv)
{
  static struct itimerval itv, ito;
  volatile double secs;

  if (argc == 2)
    secs = atof(argv[ 1 ]);

  if (argc != 2 || secs <= 0) {
    fprintf(stderr, "Usage: %s [time in seconds]\n", argv[0]);
    return -1;
  }

  itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
  itv.it_value.tv_sec = (long)floor(secs);
  itv.it_value.tv_usec = (long)(1000000.0 * (secs - itv.it_value.tv_sec));

  signal(SIGPROF, sigprof);

  setitimer(ITIMER_PROF, &itv, &ito);

  while (1) {}
  
  return 0;
}
