Timers 101, Part 2

May 9, 2002, 11:00 PM —  ITworld — 

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

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Free books

Essential JavaFX
Get started building rich Web apps quickly with an introduction to the power of JavaFX key features -- scene node graphs, nodes as components, the coordinate system, layout options, colors and gradients, custom classes with inheritance, animation, binding, and event handlers.Enter now!

The Nomadic Developer
Consulting can be hugely rewarding, but it's easy to fail if you are unprepared. To succeed, you need a mentor who knows the lay of the land. Aaron Erickson is your mentor, and this is your guidebook. Enter now!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace