The Linux Programming Interface

(nextflipdebug5) #1
Timers and Sleeping 503

if (cptr == NULL) {
tsp->it_interval.tv_sec = 0;
tsp->it_interval.tv_nsec = 0;
} else {
sptr = strchr(cptr + 1, '/');
if (sptr != NULL)
*sptr = '\0';
tsp->it_interval.tv_sec = atoi(cptr + 1);
tsp->it_interval.tv_nsec = (sptr != NULL)? atoi(sptr + 1) : 0;
}
}
––––––––––––––––––––––––––––––––––––––––––––––– timers/itimerspec_from_str.c
We demonstrate the use of the program in Listing 23-5 in the following shell ses-
sion, creating a single timer with an initial timer expiry of 2 seconds and an interval
of 5 seconds.

$ ./ptmr_sigev_signal 2:5
Timer ID: 134524952 (2:5)
[15:54:56] Got signal 64 SIGRTMAX is signal 64 on this system
*sival_ptr = 134524952 sival_ptr points to the variable tid
timer_getoverrun() = 0
[15:55:01] Got signal 64
*sival_ptr = 134524952
timer_getoverrun() = 0
Type Control-Z to suspend the process
[1]+ Stopped ./ptmr_sigev_signal 2:5

After suspending the program, we pause for a few seconds, allowing several timer
expirations to occur before we resume the program:

$ fg
./ptmr_sigev_signal 2:5
[15:55:34] Got signal 64
*sival_ptr = 134524952
timer_getoverrun() = 5
Type Control-C to kill the program

The last line of program output shows that five timer overruns occurred, meaning
that six timer expirations occurred since the previous signal delivery.

23.6.6 Timer Overruns.....................................................................................


Suppose that we have chosen to receive notification of timer expiration via delivery
of a signal (i.e., sigev_notify is SIGEV_SIGNAL). Suppose further that the timer expires
multiple times before the associated signal is caught or accepted. This could occur
as the result of a delay before the process is next scheduled. Alternatively, it could
occur because delivery of the associated signal was blocked, either explicitly via
sigprocmask(), or implicitly during the execution of the handler for the signal. How
do we know that such timer overruns have happened?
We might suppose that using a realtime signal would help solve this problem,
since multiple instances of a realtime signal are queued. However, this approach
turns out to be unworkable, because there are limits on the number of realtime signals
Free download pdf