Chapter 6. Flow of Time

At this point, we know how to write a full-featured char module. We’ll deal with kernel resources available to the driver in the next few chapters. I’ll start by showing how timing issues are addressed from kernel code. This involves, in ascending order of complexity:

  • Knowing the current time

  • Delaying operation for a specified amount of time

  • Scheduling asynchronous functions to happen after a specified time lapse

Time Intervals in the Kernel

The first point we need to cover is the timer interrupt, which is the mechanism the kernel uses to keep track of time intervals. The timer interrupt is set to a default frequency of HZ, which is an architecture-dependent value defined in <linux/param.h>. Through at least version 2.0 and 2.1.43, Linux defines HZ to be 1024 for the Alpha and 100 for all other platforms.

When the timer interrupt occurs, the jiffies value is incremented. jiffies is thus the number of clock ticks since the operating system was booted; it is declared in <linux/sched.h> as unsigned long volatile, and it will overflow in one and a third years of continuous operation. If you’re planning on more than one and a third years of uptime, you’d better buy yourself an Alpha, which won’t overflow for half a billion years--it has 64-bit longs. I can’t tell you exactly what happens when jiffies overflows, and I haven’t got time to wait to find out.

If you change the value of HZ and try to recompile the kernel, you won’t notice any difference when ...

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.