|
|
#include <sys/select.h> #include <sys/itimer.h>getitimer(which, value) int which; struct itimerval *value);
setitimer(which, value, ovalue) int which; const struct itimerval *value; struct itimerval *ovalue;
Each process has three interval timers or ``itimers'' associated with it. At the expiration of a programmed amount of time, an interval timer signals the process and (optionally) automatically reloads itself to re-interrupt the process at programmed intervals. Each itimer counts a different type of time, and generates a different signal upon expiration:
Each itimer counts by decrementing the programmed amount towards zero.
The getitimer function returns the current value for the itimer specified by the which parameter in the structure pointed to by the value parameter.
The setitimer function programs the which itimer to the specified value. If the ovalue pointer is not NULL, the previous setting is returned in the itimerval structure pointed to by ovalue.
An itimer is defined by the itimerval structure defined in <sys/itimer.h> and the timeval structure defined in <sys/select.h>:
struct itimerval { struct timeval it_interval; /* timer interval */ struct timeval it_value; /* current value */ }struct timeval { long tv_sec; /* seconds */ long tv_usec; /* and microseconds */ }
If the it_value
member is nonzero,
it indicates the time to the next itimer expiration.
This member is decremented as time advances.
If the it_interval
member is nonzero,
it specifies a value to be used in reloading
it_value
when the itimer expires.
Setting it_value
to zero disables an itimer.
Setting it_interval
to zero causes an itimer to be disabled after its next expiration
(assuming it_value
is nonzero).
The child process does not inherit its parent's itimers after a fork(S-osr5); that is, all of the child's itimers are disabled. Interval timer settings are not changed by exec(S-osr5).
Both the it_value
and it_interval
members
have microsecond precision.
The tv_sec
member
indicates the number of seconds,
and tv_usec
the millionths of a second.
A timeval
structure is zero only when both
tv_sec
and tv_usec
are zero.
The accuracy of an itimer depends on the system's load;
the busier the system,
the greater the delay between the generation of the signal and
its delivery to the process.
An itimer never expires before
it is programmed to expire,
but on sufficiently busy systems
(or with very short itimer intervals),
a signal may not be delivered
before the subsequent one is generated.
Signals do not't queue,
so only one signal will be delivered.
The system compensates and does not allow the itimer to drift;
each successive signal is generated
when it is programmed to occur
and is not dependent on whether or when
the prior signal was delivered.
Four macros are defined in <sys/itimer.h> for manipulating timeval structures:
timercopy( const struct timeval source, struct timeval destination )
The timercopy macro copies the structure pointed to by source to that pointed to by destination .
timerisset( const struct timeval timvalptr )
The timerisset macro is nonzero (true) if the structure pointed to by timvalptr is nonzero.
timerclear( struct timeval timvalptr )
The timerclear macro zeros the structure pointed to by timvalptr .
timercmp( const struct timeval leftvptr, const struct timeval rightvptr, operator )
The timercmp
macro compares the two intervals defined by the structures pointed to by
lefttvptr and righttvptr
according to the specified operator
(<,
==,
!=,
or
>):
(lefttvptr's interval) operator (righttvptr's interval)The <= and >= operators do not work with the timercmp macro.
The getitimer( ) system call fails if one or more of the following is true:
The setitimer( ) system call fails if one of more of the following is true, and the current itimer setting is not changed:
Other vendors' systems may use the <sys/time.h> header file instead of <sys/itimer.h>.
Any call to alarm( ) disables the ITIMER_REAL. Any (successful) call to setitimer( ) disables any alarm. getitimer( ) returns information about whichever of alarm( ) and ITIMER_REAL. is currently enabled.
Older versions of sleep(S-osr5) may inadvertently convert a ITIMER_REAL into an alarm, which has only seconds precision and is not reloaded when it expires.
The ITIMER_PROF interval timer is traditionally used by interpreters to statistically profile interpreted code. The generated SIGPROF may interrupt in-progress system calls. There is no facility to restart such interrupted system calls, so this itimer may not be very useful for such profiling.