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, 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

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.