Kernel Timers

The ultimate resources for time-keeping in the kernel are the timers. Timers are used to dispatch execution of a function (a timer handler) at a particular time in the future. This is different from task queues, in that you can specify when in the future your function must be called, whereas you can’t tell exactly when a queued task will be executed. On the other hand, kernel timers are similar to task queues in that a function registered in a kernel timer is executed only once--timers aren’t cyclic.

There are times when you need to execute operations detached from any process’s context, like turning off the floppy motor or terminating another lengthy shutdown operation. In that case, delaying the return from close wouldn’t be fair to the application program. Using a task queue is also overkill, because a queued task must continually re-register itself while making its time calculations.

A timer is much easier to use. You register your function once and the kernel calls it once when the timer expires. Such a functionality is used often within the kernel proper, but it is sometimes needed by the drivers as well, as in the example of the floppy motor.

Linux uses two kinds of timers, so-called ``old timers'' and new timers. I’ll quickly mention the old timers before showing you how to use the better new timers. The new timers, indeed, are not that new; they were introduced before Linux 1.0.

The old timers consist of 32 static timers. They survive only for compatibility ...

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.