search

what is interrupt in linux

Apr 20, 2023 pm 04:34 PM
linux

In Linux, an interrupt is a mechanism by which the hardware sends a signal to the CPU when needed, and the CPU temporarily stops its ongoing work to handle the hardware request. When the hardware is busy, the CPU is likely to do a lot of useless work (each poll is skipped and not processed); therefore, in order to improve the performance of the collaborative work between the CPU and peripheral hardware (hard disk, keyboard, mouse, etc.), introduce The interrupt mechanism.

what is interrupt in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

1. Interrupt definition

Interrupt is when the hardware sends a signal to the CPU when needed, and the CPU temporarily stops the ongoing work to process it. A mechanism for hardware requests.

Specifically:
Interrupt refers to the CPU temporarily stopping the running program due to internal or external events or events pre-arranged by the program during normal operation of the CPU, and switches to the internal or external event. Or go to the program of the pre-arranged event service, and then return to continue running the temporarily interrupted program after the service is completed.

1.1 Background (why interrupts are needed)

1. Without interrupts, collaboration between the CPU and peripheral devices The only way to work/communicate is by polling: the CPU regularly checks the hardware status and processes it when it needs to be processed, otherwise it skips it.
2. The speed of the processor is not in the same order of magnitude as the peripheral hardware devices, so a mechanism is provided for the hardware to send signals to the kernel when needed.

Disadvantages of polling/Introduction of interrupt mechanism:
When the hardware is busy, the CPU is likely to do a lot of useless work (each polling is skipped and not processed).
Therefore, in order to improve the performance of collaborative work between the CPU and peripheral hardware (hard disk, keyboard, mouse, etc.), an interrupt mechanism is introduced.

1.2 Interrupts and signals

中断: 硬件/进程发,内核收
信号:内核发,进程收,或者进程发进程收

##1.3 Interrupt handling Process (refer to CSAPP books)

中断是异步发生的,是来自处理器外部的I/O设备的信号的结果

1. 硬件中断不是由任何一条专门的指令造成的,从这个意义上来说它是异步的
2. 硬件中断的异常处理程序常常被称为中断处理程序(interrupt handler)
The following figure outlines the processing of an interrupt.

Figure 8.5

what is interrupt in linux

I/O devices, such as network adapters, disk controllers, and timer chips, signal an exception to a pin on the processor chip. The exception number is placed on the system bus to trigger an interrupt. This exception number identifies the device that caused the interrupt.

Before the current instruction completes execution, the processor notices that the voltage on the interrupt pin has become high, reads the exception number from the system bus, and then calls the appropriate interrupt handler. When the handler returns, it returns control to the next instruction (that is, the instruction that would be after the current instruction in the control flow if no interrupt had occurred). The result is that program execution continues as if the interruption had not occurred.

剩下的异常类型(陷阱、故障和终止)是同步发生的,是执行当前指令的结果
我们把这类指令叫做故障指令(faulting instruction)

1.4 The essence of interrupt and processing mechanism/process

The essence of interrupt is a special electrical signal

what is interrupt in linux

Processing process:

Interrupts are generated by hardware devices and sent directly to the input pins of the interrupt controller (simple electronic chip). The interrupt controller uses multiplexing The technology uses multiple interrupt pipelines to communicate with the processor through only one pipeline connected to the processor. Once the processor detects this signal, it interrupts its current work and processes the interrupt. When hardware devices generate interrupts, they do not consider synchronization with the processor's clock, that is, interrupts can be generated at any time, so the kernel may be interrupted at any time by new interrupts.

2. Interrupt types (categories)

Linux is usually divided into external interrupts (also called hardware interrupts) and internal interrupts (also called abnormal).

2.1 Synchronous interrupt (exception/internal interrupt)

Synchronous interrupt (exception/internal interrupt): Synchronous interrupt Generated by the CPU itself, also known as internal interrupt or exception

2.1.1 Synchronous interrupt example: page missing interrupt

The CPU is executing a When executing the instruction, if it is found that the page he wants to access (the page of the virtual address) is not in the physical memory, then the execution of the instruction will be stopped and a page non-existence exception will be generated.

An executable file may be large and placed in On the disk, only a part of it is read into the memory at a time (cpu locality principle).
When it wants to access the remaining content, a page fault interrupt will occur. At this time, it will be swapped in from the disk.

2.2 Asynchronous interrupt (interrupt/external interrupt)

Asynchronous interrupt (interrupt/external interrupt): Asynchronous interrupt is generated by an external hardware device , also known as external interrupt or interrupt

2.2.1 异步中断举例:网卡的工作原理

当网卡接受到数据包时,通知内核,触发中断,所谓的上半部就是,及时读取数据包到内存,防止因为延迟导致丢失,这是很急迫的工作。
读到内存后,对这些数据的处理不再紧迫,此时内核可以去执行中断前运行的程序,而对网络数据包的处理则交给下半部处理。

2.3 中断与异常的区别

异常与中断不同,中断是由硬件引起的;
异常则发生在编程失误而导致错误指令,或者在执行期间出现特殊情况必须要靠内核来处理的时候(比如缺页)。它在产生时必须考虑与处理器时钟同步,因此异常也称同步中断。

3、中断请求实现:上下半部机制

3.1 背景

中断处理程序运行需要快速执行(因为不可阻塞),同时要能完成尽可能多的工作,这里存在矛盾。

因此把中断处理切分为两个部分,上半部分(top half)接收到一个中断后立即执行,但是只做有严格时限的工作,例如对接收到的中断进行应答或复位硬件。能够被允许稍后完成的工作会推迟到下半部分(bottom half)去,此后在合适的时机下半部分会被中断执行,Linux提供了实现下半部分的各种机制。

优点:这种设计可以使系统处于中断屏蔽状态的时间尽可能的短,以此来提高系统的响应能力。

3.2 上半部:

中断处理程序是上半部——接受中断,他就立即开始执行,但只有做严格时限的工作。
上半部简单快速,执行时禁止一些或者全部中断。
工作内容:处理紧急功能,取寄存器状态。

3.3 下半部:

能够被允许稍后完成的工作会推迟到下半部去,此后,在合适的时机,下半部执行
工作内容:完成中断事件绝大多数任务。
下半部稍后执行,而且执行期间可以响应所有的中断。
下半部的实现有软中断实现, tasklet 实现和工作队列实现。

3.4 上下半部划分原则

1) 如果一个任务对时间非常敏感,将其放在中断处理程序中执行;
2) 如果一个任务和硬件有关,将其放在中断处理程序中执行;
3) 如果一个任务要保证不被其他中断打断,将其放在中断处理程序中执行;
4) 其他所有任务,考虑放置在下半部执行

3.5 举例子: 用网卡来解释一下这两半。

当网卡接受到数据包时,通知内核,触发中断,所谓的上半部就是,及时读取数据包到内存,防止因为延迟导致丢失,这是很急迫的工作。
读到内存后,对这些数据的处理不再紧迫,此时内核可以去执行中断前运行的程序,而对网络数据包的处理则交给下半部处理。

4、中断号

中断对应着一个中断号,内核通过这个中断号查找相应的中断服务程序。

每个中断都通过一个唯一的数字标志,这样操作系统才能够给不同的中断提供对应的中断处理程序。
这些中断值即中断请求线,例如IRQ 0是时钟中断、IRQ 1是键盘中断。对于连接在PCI总线上的设备而言,中断请求线是动态分配的。

5、中断上下文

中断服务程序不在进程上下文中执行,而是在一个与所有进程都无关的、专门的中断上下文中运行,以此保证中断服务程序能够在第一时间响应和处理中断请求,然后快速地退出。

处理器在任何指定时间点上的活动必然属于以下三种情况之一:

运行于用户空间,执行用户进程;
运行于内核空间,处于进程上下文,代表某个特定的进程执行;
			 (CPU空闲时,内核执行空进程)
运行于内核空间,处于中断上下文,与任何进程无关,处理某个特定的中断;

相关推荐:《Linux视频教程

The above is the detailed content of what is interrupt in linux. 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
What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

Linux Architecture: Unveiling the 5 Basic ComponentsLinux Architecture: Unveiling the 5 Basic ComponentsApr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.