122 lines
2.6 KiB
C
122 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include <sys/time.h>
|
|
#include <string.h>
|
|
#include "lceb.h"
|
|
|
|
int sigint_received=0;
|
|
int displaytimer=0;
|
|
static struct timespec start;
|
|
|
|
static void stopall(signum)
|
|
int signum;
|
|
{
|
|
printf("SIGNAL %d RECEIVED: aborting eval\n", signum);
|
|
sigint_received=1;
|
|
}
|
|
|
|
int stopped()
|
|
{
|
|
return sigint_received;
|
|
}
|
|
|
|
void set_intr()
|
|
{
|
|
struct sigaction sig;
|
|
|
|
sig.sa_handler = stopall;
|
|
sigemptyset(&sig.sa_mask);
|
|
sig.sa_flags = 0;
|
|
sigaction(SIGINT, &sig, NULL);
|
|
//sigaction(SIGUSR1, &sig, NULL);
|
|
# ifdef DEBUG
|
|
printf("SIGINT armed.\n");
|
|
# endif
|
|
}
|
|
|
|
void set_alarm(ms)
|
|
int ms;
|
|
{
|
|
struct sigaction sig;
|
|
struct itimerval timer;
|
|
|
|
/* Install timer_handler as the signal handler for SIGVTALRM. */
|
|
//memset (&sa, 0, sizeof (sa));
|
|
sig.sa_handler = stopall;
|
|
sigemptyset(&sig.sa_mask);
|
|
sig.sa_flags = 0;
|
|
sigaction (SIGALRM, &sig, NULL);
|
|
|
|
timer.it_value.tv_sec = ms/1000;
|
|
timer.it_value.tv_usec = (ms%1000)*1000;
|
|
# ifdef DEBUG
|
|
printf("alarm clock set to %.2f secs.\n",
|
|
timer.it_value.tv_sec + timer.it_value.tv_usec / 1000000.);
|
|
# endif
|
|
/* ... and every 250 msec after that. */
|
|
timer.it_interval.tv_sec = 0;
|
|
timer.it_interval.tv_usec = 0;
|
|
/* Start a virtual timer. It counts down whenever this process is executing. */
|
|
setitimer (ITIMER_REAL, &timer, NULL);
|
|
|
|
}
|
|
|
|
void start_timer()
|
|
{
|
|
printf("timer started...\n");
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
# ifdef DEBUG_TIMER
|
|
printf("sec=%d nsec=%d\n", start.tv_sec, start.tv_nsec);
|
|
# endif
|
|
}
|
|
|
|
void set_timer(timer)
|
|
struct timespec *timer;
|
|
{
|
|
clock_gettime(CLOCK_MONOTONIC, timer);
|
|
# ifdef DEBUG_TIMER
|
|
printf("timer: sec=%d nsec=%d orig: sec=%ld nsec=%ld\n",
|
|
timer->tv_sec, timer->tv_nsec, start.tv_sec, start.tv_nsec);
|
|
# endif
|
|
}
|
|
|
|
double get_timer(timer)
|
|
struct timespec timer;
|
|
{
|
|
double diff;
|
|
diff=(timer.tv_sec - start.tv_sec) + (timer.tv_nsec - start.tv_nsec) / NANOSEC;
|
|
# ifdef DEBUG_TIMER
|
|
printf("timer sec: %.5f\n", diff);
|
|
# endif
|
|
return diff;
|
|
}
|
|
|
|
#ifdef STANDALONE
|
|
int main(ac, av)
|
|
int ac;
|
|
char **av;
|
|
{
|
|
int delay;
|
|
struct timespec end;
|
|
|
|
if (ac != 2) {
|
|
fprintf(stderr, "usage: %s msecs\n", *av);
|
|
exit (1);
|
|
}
|
|
delay=atoi(*(av+1))*10;
|
|
start_timer();
|
|
set_intr();
|
|
set_alarm(delay);
|
|
//tsdelay.tv_sec=delay/MILLISEC;
|
|
//tsdelay.tv_nsec=delay%MILLISEC;
|
|
//printf("delay %d=%ld:%ld\n", delay, tsdelay.tv_sec, tsdelay.tv_nsec);
|
|
sleep(5);
|
|
set_timer(&end);
|
|
printf("Time elapsed: %.5f seconds\n", get_timer(end));
|
|
|
|
return 0;
|
|
}
|
|
#endif
|