#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include "signals.h" #include "timing.h" #define SIG_CATCH 10 int signum; void handler (int signo) { fprintf (stdout, "\nSIGALRM received"); fflush (stdout); return; } int main (int argc, char *argv[]) { long seconds, useconds, ttwait; unsigned long int ellapsed; /* ===================================================== */ /* Check command line arguments */ /* ===================================================== */ if (argc != 4) { fprintf (stderr, "\nUsage: one_shot_timer.test <timer seconds> <timer micro seconds> <time to wait>\n"); return 1; } seconds = atoi (argv[1]); useconds = atoi (argv[2]); ttwait = atoi (argv[3]); /* ===================================================== */ /* Set signal handler for SIGALRM */ /* ===================================================== */ set_signal (SIGALRM); if (set_signal_handler(handler) == 1) { fprintf (stderr, "\nset_signal_handler() failed\n"); return 1; } /* ===================================================== */ /* Create a one shot timer */ /* ===================================================== */ if (set_one_shot_timer(seconds, useconds) == SET_ONE_SHOT_TIMER_ERROR) { fprintf (stderr, "\nset_one_shot_timer() failed!\n\n"); return 1; } /* ===================================================== */ /* Wait until SIGALRM is raised */ /* ===================================================== */ fprintf (stdout, "\nWait %d seconds", (int)ttwait); fflush(stdout); if (sleep (ttwait) != 0) { fprintf (stdout, "\nTimer hit me!\n"); } else { fprintf (stdout, "\nTimer did not hit me!\n"); } /* ===================================================== */ /* Cancel the one shot timer */ /* ===================================================== */ if (cancel_one_shot_timer() == CANCEL_ONE_SHOT_TIMER_ERROR) { fprintf (stderr, "\ncancel_one_shot_timer() failed!\n\n"); return 1; } /* ===================================================== */ /* Restart the timer with a delay of 3 seconds */ /* ===================================================== */ fprintf (stdout, "\nRestart timer for 4 seconds ...\n"); fflush(stdout); if (set_one_shot_timer(4, 0) == SET_ONE_SHOT_TIMER_ERROR) { fprintf (stderr, "\nset_one_shot_timer() failed!\n\n"); return 1; } if (start_chrono() == -1) { fprintf (stderr, "\nstart_chrono() failed!\n\n"); return 1; } fprintf (stdout, "\nSleep 1 sec"); fflush(stdout); sleep(1); fprintf (stdout, "\nPause the timer"); fflush(stdout); if (pause_one_shot_timer() == PAUSE_ONE_SHOT_TIMER_ERROR) { fprintf (stderr, "\npause_one_shot_timer() failed!\n\n"); return 1; } fprintf (stdout, "\nSleep 1 sec"); fflush(stdout); sleep(1); fprintf (stdout, "\nRestart the timer"); fflush(stdout); if (restart_one_shot_timer() == RESTART_ONE_SHOT_TIMER_ERROR) { fprintf (stderr, "\nrestart_one_shot_timer() failed!\n\n"); return 1; } fprintf (stdout, "\nWait 5 seconds"); if (sleep(5) != 0) { fprintf (stdout, "\nTimer hit me!\n"); } else { fprintf (stdout, "\nTimer did not hit me!\n"); } if (stop_chrono() == -1) { fprintf (stderr, "\nstop_chrono() failed!\n\n"); return 1; } ellapsed = get_ms(); fprintf (stdout, "\nellapsed time: %lu (should be around 5000 ms)", ellapsed); fprintf (stdout, "\nReturn"); fflush (stdout); return 0; }