In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to achieve Linux kernel timer and delay work". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Kernel timer
The timer on the software ultimately depends on the hardware clock. To put it simply, the kernel will detect whether each timer registered with the kernel expires after the clock interrupt occurs. If it expires, it will call back the corresponding registration function and execute it as the bottom half of the interrupt. In fact, the clock interrupt handler triggers the TIMER_SOFTIRQ soft interrupt, running all timers that expire on the current processor.
Device drivers can use kernel timers if they want to obtain time information and need timing services.
Jiffies
To talk about the kernel timer, we must first talk about an important concept of time in the kernel: the jiffies variable. As the basis of the kernel clock, jiffies increases by 1 at every other fixed time, which is called adding a beat. This fixed interval is realized by timer interrupts, and how many timer interrupts are generated per second is determined by the HZ macro defined in. In this way, a period of time can be obtained through jiffies. For example, jiffies/HZ represents the number of seconds since the system started. The next two seconds are (jiffies/HZ+2). Jiffies is used in the kernel to time, and seconds are converted into jiffies:seconds*HZ, so time is calculated in jiffiy and based on the current time: (jiffies/HZ+2) * HZ=jiffies+2*HZ if you want to get the current time, you can use do_gettimeofday (), which fills a struct timeval structure with close to subtle resolution.
/ / kernel/time/timekeeping.c / * * do_gettimeofday-Returns the timeofday in a timeval * @ tv: pointer to the timeval to be set * * NOTE: Users should be converted to using getnstimeofday () * / void do_gettimeofday (struct timeval * tv)
Drivers often need to delay the execution of specific code for a certain period of time in order to give the hardware enough time to complete some tasks. according to the length of delay, the concepts of long delay and short delay are used in kernel development. Long delay is defined as: delay time > multiple jiffies. Long delay can be achieved by querying jiffies:
Time_before (jiffies, new_jiffies); time_after (new_jiffiesmjiffies)
* * short delay is defined as: the delay event is close to or shorter than a jiffy, and the short delay can be called.
Udelay (); mdelay ()
Both functions are busy waiting functions that consume a lot of CPU time. The former uses software loops to delay a specified number of subtle numbers, while the latter uses the nesting of the former to achieve millisecond delay.
Timer
The driver can register a kernel timer to specify that a function will be executed at some point in the future. The timer starts from registration to the kernel and executes the registered function when the specified time is reached. That is, the timeout value is a jiffies value, and when the jiffies value is greater than timer- > expires, timer- > function will be executed. API is as follows
/ set a timer struct timer_list my_timer;// initialize timer void init_timer (struct timer_list * timer); mytimer.function = my_function; mytimer.expires = jiffies + HZ;// add timer void add_timer (struct timer_list * timer); / / delete timer int del_tiemr (struct timer_list * timer)
Example
Static struct timer_list tm; struct timeval oldtv;void callback (unsigned long arg) {struct timeval tv; char* strp = (char*) arg; do_gettimeofday (& tv); printk ("% s:% ld,% ld\ n", _ _ func__, tv.tv_sec-oldtv.tv_sec, tv.tv_usec- oldtv.tv_usec); oldtv = tv; tm.expires = jiffies+1*HZ; add_timer (& tm) } static int _ init demo_init (void) {init_timer (& tm); do_gettimeofday (& oldtv); tm.function= callback; tm.data = (unsigned long) "hello world"; tm.expires = jiffies+1*HZ; add_timer (& tm); return 0;}
Delay in work
In addition to using kernel timers to complete timing delay work, the Linux kernel also provides a set of encapsulated "shortcuts"-delayed_work, which, like kernel timers, is essentially implemented using work queues and timers.
/ / include/linux/workqueue.h struct work_struct {atomic_long_t data; struct list_head entry; work_func_t func; # ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map; # endif}; struct delayed_work {114struct work_struct work; struct timer_list timer / * target workqueue and CPU-> timer uses to queue-> work * / struct workqueue_struct * wq; int cpu;}
Functions that need to be delayed, typedef void (work_func_t) (struct work_struct work)
At this point, we can use a delayed_work object and the corresponding scheduling API to implement the delayed execution of the specified task.
/ / register a delayed execution of 591 static inline bool schedule_delayed_work (struct delayed_work * dwork,unsigned long delay) / / cancel a delayed execution of 2975 bool cancel_delayed_work (struct delayed_work * dwork)
Like the internal check timer, delayed execution is executed only once when the timeout occurs, and if you want to achieve a loop delay, you only need to register a delayed execution function again in the registered function.
Schedule_delayed_work (& work,msecs_to_jiffies (poll_interval)); "how to implement Linux kernel timers and deferred work" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.