In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The Linux kernel timer is a mechanism used by the kernel to control the scheduling and execution of a function at some point in the future (based on jiffies), and its implementation is located in the and kernel/timer.c file.
Development platform Xinlingsi SinlinxA33 development board Taobao shop: https://sinlinx.taobao.com/
Embedded linux development board communicates the data structure of QQ:641395230 kernel timer struct timer_list {struct list_head entry; / / bi-directional linked list element list: used to connect multiple timers into a two-way circular queue. The unsigned long expires; / / expires field represents the jiffies value that the timer is expected to execute, and when the jiffies value is reached, the function function is called, passing data as an argument. Void (* function) (unsigned long); / / points to an executable function. When the timer expires, the kernel executes the function specified by function. The data field is used by the kernel as the calling parameter of the function function. Unsigned long data; struct tvec_base * base; / *... * /}; time comparison operation
In timer applications, it is often necessary to compare two time values to determine whether timer times out, so the Linux kernel defines four time comparison macros in the timer.h header file. Here we say that time an is after time b, which means that time is worth a ≥ b. Linux strongly recommends that users use the following four time comparison action macros (include/linux/timer.h) defined by it:
# define time_after (a) (long) (b)-(long) (a)
< 0) #define time_before(a,b) time_after(b,a) #define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0) # define time_before_eq (aheline b) time_after_eq (b) A) Linux kernel time-dependent conversion function 1. Unsigned long usecs_to_jiffies (const unsigned int u) function: convert microseconds into clock beats parameters: U time microseconds return: corresponding clock beats 2. Unsigned long msecs_to_jiffies (const unsigned int m) function: convert milliseconds into clock beats parameters: u time milliseconds return: corresponding clock beats example: to be timed from now on Execute a function in 3 milliseconds expires set to jiffies+ msecs_to_jiffies (3) Linux kernel timer operation related API statically define structure variables and initialize (macro)
DEFINE_TIMER (_ name, _ function, _ expires, _ data)
Function: define a variable of the struct timer_list structure named _ name and initialize its function, expires, data members
two。 Timer initialization (macro)
Init_timer (timer)
Function: only perform some basic initialization operations on struct timer_list structure members, function, expires, data members also need to be self-made
It's been filled.
3. Set timer (macro)
Setup_timer (timer, fn, data)
Function: set the function, data and some basic members in the timer. Expires is not initialized, so you need to initialize it yourself.
4. Register timer to kernel
Void add_timer (struct timer_list * timer)
Function: register a timer with the kernel, and the timing will start immediately after registration.
5. Log out the timer from the kernel
Int del_timer (struct timer_list * timer)
Function: delete the specified timer from the kernel timing list, and the bound function will not be executed after deletion
6. Modify the timer timing value and re-register
Int mod_timer (struct timer_list * timer, unsigned long expire0.s)
Function: modify the timer timing value and re-register, regardless of whether the timing timeout function has been executed or not. It will start as soon as the execution is completed.
Timing of movement.
Programming steps of kernel timer
Step1 defines timer_list structure variables
Step2 defines the timeout function
Step3 initializes timer_list structure variables
Step4 registration timer, start timing
Step5 logout timer
Experimental platform: core Lingsi SinlinxA33 development board
Driver code:
# include # include # include / / Step1 timer_list structure variable struct timer_list timer;//Step2 timeout function void timer_fun (long data) {printk ("% s is call! Data:%d\ r\ n ", _ _ FUNCTION__,data); / / _ _ FUNCTION__ gets the current function name mod_timer (& timer, jiffies + HZ*1); / / modifies the timer timeout again to 1 second after the current time} static int _ init timer_init (void) {/ / Step3 initiates init_timer (& timer) on the timer_list structure variable Setup_timer (& timer, timer_fun, 666); timer.expires = jiffies + HZ*2; / / Step4 registration timer, start timing add_timer (& timer); printk ("Timer start!\ r\ n"); return 0 } static void _ exit timer_exit (void) / / Module exit function specified by module_exit () {/ / Step5 logout timer del_timer_sync (& timer); printk ("Timer over!\ r\ n");} module_init (timer_init); module_exit (timer_exit); MODULE_LICENSE ("GPL")
Makefile Code:
KERN_DIR = / work/lichee/linux-3.4all: make-C $(KERN_DIR) M = `pwd` modules clean: make-C $(KERN_DIR) M = `pwd` modules clean rm-rf modules.orderobj-m + = timer_drv.o
Embedded linux development board communicates QQ:641395230
Finally, using the dmseg command, you can see that it is printed every 1 second.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.