search
HomeSystem TutorialLINUXA closer look at Linux kernel timers: interrupt-based asynchronous mechanisms and non-process context principles

深入了解 Linux 内核定时器:基于中断的异步机制与非进程上下文原则

1. Kernel timer 1. Basic concepts

In certain scenarios, we need to perform certain actions after a specific time, and we don’t want to waste CPU by waiting. At this time, the timer is a very suitable mechanism. A timer is used to execute a function at a certain point in the future to complete a specific task.

Kernel timers tell the kernel to call a specific function with specific parameters at a specified point in time. The timer runs asynchronously on its registrant. When the timer is running, the task that registered the timer may be sleeping or running on other processors, or may even have exited long ago.

The kernel timer in Linux is implemented based on (soft) interrupts Linux application timer, that is, it is in the interrupt context rather than the process context. There are some principles to follow in non-process context:

Access to user space is not allowed

current is meaningless and therefore unavailable

Cannot sleep or schedule. Schedule or some kind of wait_event cannot be called, nor can any function that may cause sleep be called. Semaphores are also not available since semaphores may cause hibernation.

应用定时器2_linux 应用定时器_应用定时器2安卓版下载

The kernel code can determine whether it is currently in the interrupt context by calling the function in_interrupt(). As long as it returns non-0, it means it is in the interrupt context. The kernel can determine whether scheduling is currently allowed by calling in_atomic(). Situations where scheduling is not allowed include: being in an interrupt context and a context that owns a carrier lock.

Because the timer is executed asynchronously, the timer processing function must pay attention to mutual exclusion protection.

2.Timers supported by linux kernel

The Linux kernel supports two types of timers:

Classic timer: a timer whose accuracy depends on the frequency of computer clock interrupts. The timer accuracy is usually relatively low, with an accuracy of 1000/HZms. This timer is generated at a fixed frequency, that is, every 1000/HZms. If the dynamic clock feature is not enabled, no real timing disturbance may occur when the timer expires. For example, only the following timers are added to the system: 11ms, 52ms, and 78ms expiration timers, and the timers expire accurately. The typical timer will expire at multiples of 4 ms (4, 8, 12...), so there may not necessarily be a timing disturbance at the time when the timer expires.

High frame rate timer: The precision of the classic timer is relatively low. In some situations, a higher precision timer is required, such as multimedia applications in the Chinese Linux operating system, so this type of timer is introduced in the system. This timer can essentially occur at any time.

应用定时器2_linux 应用定时器_应用定时器2安卓版下载

Two concepts of exception are also needed here:

Dynamic clock: The periodic clock linux application timer is only activated when a task needs to be actually executed, otherwise the periodic clock technology is disabled. The approach is to disable the periodic clock if you need to schedule idle to run; then enable the periodic clock until the next timer expires or an interrupt occurs. One-shot clock is a prerequisite for realizing dynamic clock, because the key feature of dynamic clock is that the clock can be stopped or restarted as needed, and pure periodic clock is not suitable for these scenarios.

Periodic clock: A clock that forms clock time periodically.

In terms of application, timers have two main uses:

Timeout: Indicates a disturbance that will occur after a certain period of time. In fact, when using timeout, in most cases, the timeout is not expected to occur, and the timer is often canceled before timeout. In addition, although it is not canceled, the timeout incident is often not an accurate incident. For example, the various timeout timers used in the network often express the meaning that if there is no... before this point in time, it can be considered ..., the value of this time is often an empirical value or a calculated value, and is not a precise time requirement. In these situations the classic timer is sufficient.

Timer: used to implement timing. For example, when playing sound, data needs to be sent to the sound card regularly. In these situations, there are strict requirements on time. If data is not sent to the sound card at a certain point in time, sound distortion will occur. . At this time, a high-precision timer must be used.

应用定时器2安卓版下载_linux 应用定时器_应用定时器2

Linux can be prompted to work in the following mode through configuration:

High frame rate dynamic clock

High frame rate period clock

Low bitrate dynamic clock

Low code rate period clock

3. Low bitrate kernel timer

linux 应用定时器_应用定时器2安卓版下载_应用定时器2

The low-bitrate timer is the most common kernel timer. The kernel will use the processor's clock interrupt or any other appropriate periodic clock source as the time baseline of the timer. Clock interrupts occur periodically, HZ times per second. The timer processing function corresponding to this interrupt is usually timer_interrupt. In the processing of this function, it will eventually be adjusted to do_timer and update_process_timers. Among them, do_timer will be responsible for system-wide and global tasks: updating jiffies and processing process statistics; and the latter function will perform process statistics to form TIMER_SOFTIRQ to provide time awareness to the scheduler.

When the timer expires, the timer will be removed from the activation array before the timer processing function is called. Therefore, if you want to re-execute after a period of time after this execution, you need to re-add the timer. device. In the SMP system, the timer function will be executed by the CPU where it is registered.

The implementation of the kernel timer must meet the following requirements and assumptions:

Timer management must be as simple as possible.

The design must have good scalability when the activity timer is greatly reduced

Most timers expire within a few seconds or at most a few minutes, and timers with long delays are quite rare.

A timer should run on the same CPU as where it was registered.

The implementation of the low bit rate kernel timer is very clever. It is based on a per-CPU data structure. The base array of timer_list contains pointers pointing to this structure. If base is NULL, this timer has not been called to run; otherwise, this pointer tells which data structure (that is, which CPU) is running it.

Whenever kernel code registers a timer (via add_timer or mod_timer), the operation is ultimately performed by internal_add_timer (in kernel/timer.c), which adds the new timer to the "cascade table" associated with the current CPU "in the timer one-way array.

How cascading tables work:

If the timer expires within the next 0 to 255 jiffies, it is added to one of the 256 arrays specially provided for short-term timers, using expires (that is, which array is added to is determined by the expiration time bits) The lower 8 bits of the determined) determine which array to add to

If it expires further in the future (but before 16384 jiffies), it is added to one of the 64 arrays. These 64 arrays are related to the 8-13 bits of expires. The 6 of expires The bits determine which array is used.

Similar techniques are applied to expires bits 14-19, 20-25 and 26-31.

If the timer expires at a later date

The above is the detailed content of A closer look at Linux kernel timers: interrupt-based asynchronous mechanisms and non-process context principles. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?Apr 25, 2025 am 09:10 AM

For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

What are the differences in how Linux and Windows handle device drivers?What are the differences in how Linux and Windows handle device drivers?Apr 25, 2025 am 12:13 AM

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

Compare and contrast the security models of Linux and Windows.Compare and contrast the security models of Linux and Windows.Apr 24, 2025 am 12:03 AM

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.