In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to understand the timing and delay of Linux device drivers. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Linux generates timer interrupts at regular intervals (measured by HZ) through system hardware timers, and each interrupt accumulates the value of a kernel counter jiffies, so the jiffies records the time elapsed when the system starts, and the kernel implements software timers and delays accordingly.
Demo for jiffies and HZ
# include unsigned long j, stamp_1, stamp_half, stamp_n; j = jiffies; / * read the current value * / stamp_1 = j + HZ; / * 1 second in the future * / stamp_half = j + HZ/2; / * half a second * / stamp_n = j + n * HZ/ 1000; / * n milliseconds * /
Kernel timer
The hardware clock interrupt handler invokes the TIMER_SOFTIRQ soft interrupt and runs all kernel timers that expire on the current processor.
Timer definition / initialization
In the Linux kernel, an instance of the timer_list structure corresponds to a timer:
/ * when the timer specified by expires expires, function (data) will be executed * /
/ * when the timer expiration time specified by expires expires, function (data) * / struct timer_list {unsigned long expires; / * timer expiration time * / void (* function) (unsigned long) will be executed; / * parameters of the timer handler function * / unsigned long data; / * function * /.} / * define * / struct timer_list my_timer; / * initialization function * / void init_timer (struct timer_list * timer); / * initialize macro * / TIMER_INITIALIZER (_ function, _ expires, _ data) / * define and initialize macro * / DEFINE_TIMER (_ name, _ function, _ expires, _ data)
Timer add / remove
/ * register the kernel timer and add the timer to the kernel dynamic timer list * / void add_timer (struct timer_list * timer); / * del_timer_sync () is the synchronous version of del_timer (), and you need to wait for it to be processed when deleting a timer, so the call to this function cannot occur in the interrupt context * / void del_timer (struct timer_list * timer). Void del_timer_sync (struct timer_list * timer)
Timing time modification
Int mod_timer (struct timer_list * timer, unsigned long expires)
Delay time
Short delay
Void ndelay (unsigned long nsecs); void udelay (unsigned long usecs); void mdelay (unsigned long msecs)
When the kernel starts, it runs a delay test program (delay loop calibration) and calculates lpj (loops per jiffy). These functions are implemented according to lpj, which is busy waiting.
Long delay
A very intuitive way is to compare the current jiffies with the target jiffies:
Int time_after (unsigned long a, unsigned long b); / * an after b, true * / int time_before (unsigned long a, unsigned long b); / * a before b * / int time_after_eq (unsigned long a, unsigned long b); / * an after or equal b * / int time_before_eq (unsigned long a, unsigned long b); / * a before or equal b * /
Delay in falling asleep
Void msleep (unsigned int millisecs); unsigned long msleep_interruptible (unsigned int millisecs); void ssleep (unsigned int seconds)
Tip: msleep (), ssleep () cannot be interrupted.
The above is how to understand the timing and delay of Linux device drivers. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.