Measurement of the time differenceThe system's timer hardware generates a clock interrupt at a fixed frequency, producing a constant interrupt interval determined by Hz constants, usually between 50~1200, x86 by default 1000,hz can be configured according to different cores.
Linux uses jiffies (unsigned long) to count clock interrupts, and the value of Jiffies is 1 when a clock interrupt occurs, so Jiffies records the total number of clock interrupts since the system was powered on. Clock interrupts are often used during driver development to calculate time intervals for different events.
Deferred executionFor imprecise time delay requirements, a while loop can be used to calculate the delay time.
10 seconds delay
unsigned long j = jiffies + ten * Hz;while (Jiffies < J) {//do something.}
Kernel timersThe kernel timers are used to control the execution of each function at some point in the future. The kernel timer can only be executed once after it has been registered, and the target function cannot be executed if the timer has not yet reached the point at which the target function was executed.
The data structure used by the kernel timer (different kernel timing events are connected in the form of a doubly linked list):
struct Timer_list {struct list_head entry;//list header unsigned long expires;//delay time struct tvec_base *base;void (*function) (uns igned long); The target function is called unsigned Long data when the timing time arrives; The data that the target function carries ...};
- Init_timer (struct timer_list *timer)//Initialize Timer event
- void Add_timer (struct timer_list *timer)//Add timed Event
- int Del_timer (struct timer_list *timer)//Delete timed Event
Example:
#include <linux/module.h> #include <linux/init.h>module_license ("GPL"); Module_author ("Jack Chen"); Module_description ("Hello World"); Module_alias ("A simple Module"); Module_version ("V1.0"), struct timer_list timer;static void _function (int data) {PRINTK ("<3> time is up data:%d\n", data);} static int Timer_init () {Init_timer (&timer); timer.expires = jiffies + 5*hz;timer.function = _function;timer.data = 10 ; Add_timer (&timer); return 0;} static void Timer_exit () {Del_timer (&timer);} Module_init (Timer_init); Module_exit (Timer_exit);
Linux Kernel Development-Kernel timers