Timers 101, Part 2
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.
» posted by ITworld staff
ITworld
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
jfruh
Apple syncing patent can't come soon enough
pasmith
New Twitter features borrow from 3rd party clients
Esther Schindler
Open Source Changes the Software Acquisition Process
mikelgan
How to set up continuous podcast play on the new iTunes
David Strom
Five important Windows 7 mobility features
sjvn
Guard your Wi-Fi for your own sake
Sandra Henry-Stocker
Grepping on Whole Words
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.












