Linux Kernel Development-Kernel timers

Source: Internet
Author: User

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

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.