480 Chapter 23
Using setitimer(), a process can establish three different types of timers, by specify-
ing which as one of the following:
ITIMER_REAL
Create a timer that counts down in real (i.e., wall clock) time. When the
timer expires, a SIGALRM signal is generated for the process.
ITIMER_VIRTUAL
Create a timer that counts down in process virtual time (i.e., user-mode
CPU time). When the timer expires, a SIGVTALRM signal is generated for the
process.
ITIMER_PROF
Create a profiling timer. A profiling timer counts in process time (i.e., the
sum of both user-mode and kernel-mode CPU time). When the timer
expires, a SIGPROF signal is generated for the process.
The default disposition of all of the timer signals is to terminate the process. Unless
this is the desired result, we must to establish a handler for the signal delivered by
the timer.
The new_value and old_value arguments are pointers to itimerval structures,
defined as follows:
struct itimerval {
struct timeval it_interval; /* Interval for periodic timer */
struct timeval it_value; /* Current value (time until
next expiration) */
};
Each of the fields in the itimerval structure is in turn a structure of type timeval, con-
taining seconds and microseconds fields:
struct timeval {
time_t tv_sec; /* Seconds */
suseconds_t tv_usec; /* Microseconds (long int) */
};
The it_value substructure of the new_value argument specifies the delay until the
timer is to expire. The it_interval substructure specifies whether this is to be a peri-
odic timer. If both fields of it_interval are set to 0, then the timer is expires just
once, at the time given by it_value. If one or both of the it_interval fields are non-
zero, then, after each expiration of the timer, the timer will be reset to expire again
at the specified interval.
A process has only one of each of the three types of timers. If we call setitimer()
a second time, it will change the characteristics of any existing timer corresponding
#include <sys/time.h>
int setitimer(int which, const struct itimerval *new_value,
struct itimerval *old_value);
Returns 0 on success, or –1 on error