a clock interrupt is generated by the system's timing hardware at periodic intervals (1/hz seconds), this interval (frequency) is determined by Hz, which is a hardware independent constant, configurable (50-1200), on the X86 platform, the default is 1000
Whenever a clock interrupt occurs, the global variable jiffies + 1, so Jiffies records the number of clock interrupts since the Linux boot, and the driver often uses jiffies to calculate the time interval for different events.
Application: Delay execution
If the accuracy of the delay is not high, the simplest implementation requirements are as follows:--busy waiting
long j = jiffies +jit_delay*HZ; while (Jiffies < j) { //do nothing;}
Second, the core timer
Timers are used to control a function (timer handler function) in the future of an event execution, the kernel timer registered functions are executed only once, not repeatedly executed.
In general, the timer executes the timeout function immediately after the timeout, but it can also be deferred to the next clock beat, so no hard real-time tasks can be implemented with timers.
structstruct// kernel use, kernel timer is organized into two-wire linked list long// Set timeout for jiffies value voidlong/ / Timeout handler long// Timeout handler function parameter struct tvec_base *base// kernel use }
Third, the operation
Initialize timer init_timer (struct timer_list *timer);
Start timer void Add_timer (struct timer_list *timer);
Delete the timer (it will be automatically deleted before it takes effect) int Del_timer (struct timer_list * timer);
#include"linux/module.h"#include"Linux/timer.h"#include"Linux/jiffies.h"structtimer_list Demo_timer;Static voidTime_func (unsignedLongdata) {PRINTK ("%s, secs =%ld!\n",(Char*) data,jiffies/HZ); Mod_timer (&demo_timer,jiffies +5*HZ);}Static int__init Mytimer_init (void) {PRINTK ("mytimer_init!\n"); Setup_timer (&demo_timer,time_func, (unsignedLong)"demo_timer!"); Demo_timer.expires= Jiffies +1*HZ; Add_timer (&Demo_timer); return 0;}Static void__exit Mytimer_exit (void) {PRINTK ("mytimer_exit!\n"); Del_timer (&demo_timer);} Module_init (Mytimer_init); Module_exit (Mytimer_exit); Module_license ("Dual BSD/GPL");
linux--Core Timer