The Linux Programming Interface

(nextflipdebug5) #1

482 Chapter 23


Each time the timer expires, the SIGALRM handler is invoked, and it sets a global flag,
gotAlarm w. Whenever this flag is set, the loop in the main program calls displayTimes()
in order to show when the handler was called and the state of the timer y. (We
designed the signal handler in this manner to avoid calling non-async-signal-functions
from within the handler, for the reasons described in Section 21.1.2.) If the timer
has a zero interval, then the program exits on delivery of the first signal; otherwise,
it catches up to three signals before terminating u.
When we run the program in Listing 23-1, we see the following:

$ ./real_timer 1 800000 1 0 Initial value 1.8 seconds, interval 1 second
Elapsed Value Interval
START: 0.00
Main: 0.50 1.30 1.00 Timer counts down until expiration
Main: 1.00 0.80 1.00
Main: 1.50 0.30 1.00
ALARM: 1.80 1.00 1.00 On expiration, timer is reloaded from interval
Main: 2.00 0.80 1.00
Main: 2.50 0.30 1.00
ALARM: 2.80 1.00 1.00
Main: 3.00 0.80 1.00
Main: 3.50 0.30 1.00
ALARM: 3.80 1.00 1.00
That's all folks

Listing 23-1: Using a real-time timer
–––––––––––––––––––––––––––––––––––––––––––––––––––––– timers/real_timer.c
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include "tlpi_hdr.h"

static volatile sig_atomic_t gotAlarm = 0;
/* Set nonzero on receipt of SIGALRM */

/* Retrieve and display the real time, and (if 'includeTimer' is
TRUE) the current value and interval for the ITIMER_REAL timer */

static void
qdisplayTimes(const char *msg, Boolean includeTimer)
{
struct itimerval itv;
static struct timeval start;
struct timeval curr;
static int callNum = 0; /* Number of calls to this function */

if (callNum == 0) /* Initialize elapsed time meter */
if (gettimeofday(&start, NULL) == -1)
errExit("gettimeofday");

if (callNum % 20 == 0) /* Print header every 20 lines */
printf(" Elapsed Value Interval\n");
Free download pdf