May 10, 2002, 12:00 AM — Interval Timers
An interval timer delivers signals to a process on a regular basis.
Linux assigns three interval timers to a process, each of which delivers
a distinct signal. The following symbolic constants identify these
timers:
* ITIMER_REAL -- This is the so-called "real-time timer", or a timer
that functions like a clock on the wall. It delivers a SIGALRM
signal to a process on a periodical basis, regardless of whether
the process is executing or not.
* ITIMER_VIRTUAL -- This timer counts only the time during which the
process is executing, excluding the time spent in syscalls. It
delivers a SIGVTALRM signal.
* ITIMER_PROF -- Counts the time during which the process is
executing, including syscalls' execution time. It excludes any
time spent executing interrupts. This timer delivers a SIGPROF
signal.
The combination of ITIMER_VIRTUAL and ITIMER_PROF is often used in code
profilers. You shouldn't use the alarm() and sleep() syscalls in a
program that uses an ITIMER_REAL timer because it conflicts with the
signals delivered by these syscalls.
Signal Generation
Each of these timers will issue its distinct signal within one system
clock tick (a hundredth of second on most hardware architectures) once
the timer has expired. If the process is currently executing, the
SIGALRM and SIGPROF signals will be delivered immediately. Otherwise,
they will be delivered as soon as possible. Since SIGVTALRM is generated
only when the process is running, it will always be delivered
immediately.
Setting and Examining Timers
The struct itimerval is used for setting and querying interval timers:
struct itimerval
{
struct timeval it_interval;
struct timeval it_value;
};
Recall that struct timeval is declared as follows:
struct timeval
{
int tv_sec; /*seconds*/
int tv_usec; /*microseconds*/
};
it_interval contains the amount of time between signals. it_value is the
amount of time left until the next signal is issued. To query a timer,
use the getitimer function:
int getitimer(int timertype, struct itimerval * tv);
The first argument is one of the three interval timers listed above. The
function fills tv with the current state of the timertype.
int setitimer(int timertype,
struct itimerval * new,
struct itimerval * old);
This function sets the timer timertype to newtimer. If old isn't NULL,
it's filled with the previous setting. To disable a timer immediately,
set its it_value to zero. To disable after the next signal, set
it_interval to zero.













