1, Timer
Two types of time related kernel structures have been mentioned before.
1, delay: Through busy waiting or sleep mechanism to achieve delay.
2, Tasklet and work queues, through some mechanism to delay the implementation of work, but do not know the specific time of execution.
The timer to be introduced next will enable the work to be performed at a specified point in time, and there is no need to use a delay method such as busy waiting. By defining a timer, it is OK to tell the kernel at what time it needs to perform, and when the time is up, the kernel executes the specified function.
2. Use timer
The use of the timer is very simple, only need three parts:
1, the definition timer structure body timer_list.
2. Set timeout time, define timer processing function and pass parameter.
3. Activate timer
Code
#include <linux/module.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/
timer.h> #if 0//definition and initialization of the timer structure body timer_list.
/*include/linux/timer.h*/struct Timer_list {struct List_head entry; unsigned long expires; Set the time Void (*function) (unsigned long) for the execution of the timer handler function; The timer processing function unsigned long data;
The parameters of the processing function struct tvec_base *base;
#ifdef config_timer_stats void *start_site;
Char start_comm[16];
int start_pid;
#endif}; #endif struct timer_list my_timer; 1. Define the timer structure body timer_list void Timer_func (unsigned long data)//2. Defines the timer handler function {PRINTK ("Time out![ %d] [%s]\n, (int) data, current->comm); Print current process} static int __init test_init (void)//module initialization function {Init_timer (&my_timer)//1. Initialize timer_list structure My_timer.exp Ires = jiffies + 5*hz; 2. Set Timer processing function trigger time is 5 seconds my_timer.function = Timer_func; 2. Assign timer processing function to struct My_timer.data = (unsigned long) 99; 2. Set the timer processing function of the Add_timer (&my_timer); 3. Activate timer PRINTK ("Hello timer,current->comm[%s]\n ", Current->comm);
return 0; } static void __exit test_exit (void)//module unload function {PRINTK ("Good Bye Timer\n");}
Third, the timer deletion and modification
It said that the activation of the timer can only be performed once, if you want to implement the interval specified time and repeated execution, it is necessary to modify the code.
Add two code to the timer handler function:
My_timer.expires = jiffies + 2*hz; Reset time, two seconds before execution
Add_timer (&my_timer); Activate timer again
In this case, the timer handler function is executed again every 2 seconds.
These two codes are also equivalent to the functions of the one:
Mod_timer (&my_timer, jiffies + 2*hz);
/*kernel/timer.c*/
int Mod_timer (struct timer_list *timer, unsigned long expires)
This is a function that changes the timer timeout, and the timeout is updated to the new timeout (expires) if the timer is called before timing out. If called after the timer has timed out, it is tantamount to specifying the timeout and activating the timer again.
If you want to cancel the timer before the timer is timed out, you can call the following function:
/*kernel/timer.c*/
int Del_timer (struct timer_list *timer)
This function is used to remove timers that have not timed out.