Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Programming of Linux Kernel timer on Xinlingsi Sinlinx A64 Development Board

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Development platform Lingsi Sinlinx A64

Memory: 1GB storage: 4GB

Detailed parameters of the development board https://m.tb.cn/h.3wMaSKm

Development board communication group 641395230

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.

Data structure of kernel timer

Struct timer_list {struct list_head entry; / / two-way 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 (a) b) time_after_eq (b)

Linux kernel time-dependent conversion function

Unsigned long usecs_to_jiffies (const unsigned int u)

Function: convert microseconds into clock beats

Parameter: U time microsecond

Return: the corresponding number of clock beats unsigned long msecs_to_jiffies (const unsigned int m)

Function: convert milliseconds into clock beats

Parameter: U time millisecond

Return: the corresponding number of clock beats

Example: to timing from now on, execute a function in 3 milliseconds

Expires is set to jiffies+ msecs_to_jiffies (3)

API related to Linux kernel timer operation

Statically define structure variables and initialize (macros)

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

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.

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

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

Driver code: # include # include # include void tasklet_fun (unsigned long data); / / Step1 defines and statically initializes the tasklet_struct structure variable DECLARE_TASKLET (mytasklet, tasklet_fun, 651); / / Step2 tasklet service function void tasklet_fun (unsigned long data) {static unsigned long count = 0; printk ("count:%lu,%s is call! Data:%lu\ r\ n ", count++,__FUNCTION__,data); tasklet_schedule (& mytasklet); / / reschedule yourself in the working function, which will call tasklet_fun} static int _ init mytasklet_init (void) {/ / Step3 to start scheduling mytasklet tasklet_schedule (& mytasklet); printk ("% s is call!\ r\ n ", _ _ FUNCTION__); return 0 } static void _ exit mytasklet_exit (void) / / Module exit function specified by module_exit () {/ / Step4 delete tasklet tasklet_kill (& mytasklet);} module_init (mytasklet_init); module_exit (mytasklet_exit); MODULE_LICENSE ("GPL")

Experimental phenomenon

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report