linux--Core Timer

Source: Internet
Author: User
Tags time interval

a clock interrupt is generated by the system's timing hardware at periodic intervals (1/hz seconds), this interval (frequency) is determined by Hz, which is a hardware independent constant, configurable (50-1200), on the X86 platform, the default is 1000

Whenever a clock interrupt occurs, the global variable jiffies + 1, so Jiffies records the number of clock interrupts since the Linux boot, and the driver often uses jiffies to calculate the time interval for different events.

Application: Delay execution

If the accuracy of the delay is not high, the simplest implementation requirements are as follows:--busy waiting

long j = jiffies +jit_delay*HZ;  while (Jiffies < j) {    //do nothing;}

Second, the core timer

Timers are used to control a function (timer handler function) in the future of an event execution, the kernel timer registered functions are executed only once, not repeatedly executed.

In general, the timer executes the timeout function immediately after the timeout, but it can also be deferred to the next clock beat, so no hard real-time tasks can be implemented with timers.

structstruct// kernel use, kernel timer is organized into two-wire linked list long//  Set timeout for jiffies value voidlong/ / Timeout handler long//  Timeout handler function parameter struct tvec_base *base// kernel use }

Third, the operation

Initialize timer init_timer (struct timer_list *timer);

Start timer void Add_timer (struct timer_list *timer);

Delete the timer (it will be automatically deleted before it takes effect) int Del_timer (struct timer_list * timer);

#include"linux/module.h"#include"Linux/timer.h"#include"Linux/jiffies.h"structtimer_list Demo_timer;Static voidTime_func (unsignedLongdata) {PRINTK ("%s, secs =%ld!\n",(Char*) data,jiffies/HZ); Mod_timer (&demo_timer,jiffies +5*HZ);}Static int__init Mytimer_init (void) {PRINTK ("mytimer_init!\n"); Setup_timer (&demo_timer,time_func, (unsignedLong)"demo_timer!"); Demo_timer.expires= Jiffies +1*HZ; Add_timer (&Demo_timer); return 0;}Static void__exit Mytimer_exit (void) {PRINTK ("mytimer_exit!\n"); Del_timer (&demo_timer);} Module_init (Mytimer_init); Module_exit (Mytimer_exit); Module_license ("Dual BSD/GPL");

linux--Core Timer

Related Article

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.