Measurement of the time differenceThe system's timer hardware generates a clock interrupt at a fixed frequency, with the interval of always interrupting being determined by the Hz constant, usually between 50~1200 and x86 by default of 1000. Hz can be configured according to different cores.
Linux uses jiffies (unsigned long) to count clock interrupts. Whenever a clock interrupt occurs, the value of jiffies will be + 1, 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.
Delayed Operation for imprecise time delay requirements, you can use a while loop 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 each function at some point in the future. The kernel timer is only able to run once after it has been registered, assuming that the timer has not been reached at the point in time when the target function was run. Then the target function will not be able to be run.
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)//Join 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