Linux Kernel Timer

Source: Internet
Author: User

A timer, also known as a dynamic timer or kernel timer, is the basis for managing Kernel Time.
The kernel often needs to postpone the execution of some code, for example, the lower half mechanism is to push the work back and execute
Clock interruption is generated by the system's scheduled hardware at periodic intervals. This interval (that is, the frequency) is determined by the kernel according to Hz.
When a clock interruption occurs, the global variable jiffies (unsigned long) is added with 1. Therefore, jiffies records the number of clock interruptions since LINUX started.
The kernel timer is used to control the execution of a function (timer processing function) at a specific time in the future.
The handler function registered by the kernel timer is executed only once-not cyclically. (The timer does not run cyclically. It is destroyed automatically after timeout.
The timer is constantly created and destroyed, and its running times are not limited)

To use a timer, you only need to perform initialization. Set a time-out period, specify the function to be executed after the time-out occurs, and then activate the timer.

1. timer structure timer_list
Include/Linux/Timer. h
Struct timer_list {

Struct list_head entry; // the entry of the timer linked list
Unsigned long expires; // timer expiration time
Struct tvec_base * base;

Void (* function) (unsigned long); // timer processing function
Unsigned long data; // The long integer parameter passed to the timer processing function.

Int slack;

...

};

2. Timer usage
1) define a timer structure
Struct timer_list timer;

2) initialize the timer
There are many interface functions that can initialize the timer
Init_timer (struct timer_list * timer );
Timer_initializer (_ function, _ expires, _ data );
Define_timer (_ name, _ function, _ expires, _ data );
Normally, you can initialize the timer and assign values to func and data.
Setup_timer (& timer, gpio_keys_timer, (unsigned long) data );

Timer processing function
Static void gpio_keys_timer (unsigned long data)
{
Schedule_work (& work );
}

3) Add a timer and activate the timer.
Void add_timer (struct timer_list * timer );
Register the kernel timer and add the timer to the kernel dynamic timer linked list.

4) Delete the timer
Int del_timer (struct timer_list * timer );
Del_timer_sync (struct timer_list * timer );
Del_timer_sync () is the synchronous version of del_timer (). When deleting a timer, you must wait until it is processed (it cannot be used in the interrupt context)

5) modify the expire of the timer and start
Int mod_timer (struct timer_list * timer, unsigned long expires );
Mod_timer (& timer, jiffies + msecs_to_jiffies (50); // jiffies overflow is not considered
Msecs_to_jiffies () is used to convert ms to jiffies

/*************************************** * ***************/Kernel timer example # include <Linux/init. h> # include <Linux/module. h> # include <Linux/Timer. h> # include <Linux/Fs. h> # define timer_major 234 # define device_name "timer_test"/** 1. define the timer structure */struct timer_list timer; static void func_timer (unsigned long data) {/** 4. modify the timer timeout parameter and restart */mod_timer (& timer, jiffies + Hz); printk ("Current jiffies is % LD \ n", jiffies );} struct file_operations timer_ops = {. owner = this_module,}; static int _ init timer_init (void) {register_chrdev (timer_major, device_name, & timer_ops);/** 2. initialize the timer */setup_timer (& timer, func_timer, 0); # If 0init_timer (& timer); timer. data = 0; timer. expires = jiffies + Hz; timer. function = func_timer; # endif/** 3. add the activation timer */add_timer (& timer); printk ("timer_init \ n"); Return 0;} static void _ exit timer_exit (void) {/** 4. delete timer */del_timer (& timer); unregister_chrdev (timer_major, device_name);} module_init (timer_init); module_exit (timer_exit); module_license ("GPL "); /*************************************** * ***************/# insmod timer_test.ko timer_inittimer_initcurrent jiffies is 220614 current jiffies is 220614 current jiffies is 220714

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.