【RTS 笔记4】Interrupts

本文详细介绍了中断机制的概念及其在计算机系统中的应用。主要内容包括中断的基本原理、使用场景、轮询与中断的区别、上下文切换的过程、中断响应时间的影响因素、识别中断源的方法、处理多个中断的策略、中断对共享数据的影响及解决办法,以及实时钟如何通过中断进行时间管理。

Interrupts

introduction 中断简介

An interrupt is a mechanism that allows a microprocessor to respond to events immediately, irrespective of what it happens to be doing.【微处理器停下手上任务,立刻处理相应事件】
When an interrupt signal occurs:

  1. the program temporarily stops【主程序停止】
  2. an interrupt service routine (ISR) runs. 【ISR运行】
  3. After the ISR is finished, the main program resumes 【ISR结束后主程序恢复】
Interrupts Usage 中断使用情况

Data transfer:用来匹配数据传输速度和cpu请求的速度
Real-time clock:定期中断让程序得以访问时钟
Manual override:允许用户控制(比如强行结束)程序
Hardware failure:硬件故障时,丢出错误信息
Power failure:突遇突然断电,CPU可以在最后执行一些命令(如保存)

Polling and Interrupts 轮询与中断

Polling : 主程序里写好了一段check(device)的代码,当cpu运行主程序到这个check代码时,cpu自发的去找设备询问“要不要做事?”
Interrupts :device去主动打断cpu正在处理的主程序,加塞一个新任务给cpu。当cpu完成加塞的任务,再回到主程序去

Context Switching

When an interrupt occurs, the current processor state (the context) must be preserved so that the original task can resume execution after the interrupt has been serviced. 【Context:保存当前程序状态】

  1. saves the state of the CPU immediately prior to the interrupt occurring,
  2. puts the processor in the required state for servicing the interrupt,
  3. restores the suspended process state after the interrupt has been processed.
Interrupt Response Time 中断响应时间

Response time is the sum of the latency and ISR processing time【中断响应时间=处理中断事件用时+上下文切换用时】
在这里插入图片描述

Issues with Interrupts

  • How to identify the source of the interrupt?
  • What to do about a second interrupt occurring while one is being processed?
  • How to deal with shared data?
Interrupt Source Identification 识别中断源

The source determines which interrupt service routine should be executed to handle the interrupt.

Polling【一个个轮询】:

  • The CPU checks the status register of each device in turn. The first device found to be generating an interrupt will be serviced.
  • Pros: Priority is implicitly defined by the order of polling (and can be easily changed by programmer).
  • Cons: Time spent interrogating each device in turn makes this slow, i.e., it increases interrupt latency, particularly for low priority devices.

Vectored 【存个表格】

  • The interrupting device not only sends an interrupt, but also a code that identifies it. This code is used to lookup the address of the corresponding ISR in a vector table
  • Pros: Fast.
  • Cons: Priority is determined, in hardware, by position of device on interrupt acknowledge line (prioritization not under the control of the programmer).
Dealing with Multiple Interrupts 处理多个中断

Simple Solution【中断发生后就不让其他人中断】:

  1. Disable interrupts during the ISR.
  2. Interrupts remain pending until re-enabled.
  3. After ISR completes, the processor checks for additional interrupts.

Complex Solution:【高优先中断能中断低优先的中断】
Associate priorities with interrupts.A higher priority interrupt can interrupt (preempt) the ISR associated with a lower priority one.

Interrupt Nesting【中断嵌套】:When a higher-priority interrupt preempts the processing of lower-priority ISRs, this is known as nesting. Nesting requires multiple context switches. → There must be enough space in memory store all contexts.【高优先中断能中断低优先的中断,不断套娃】

Interrupts and Shared Data 中断共享数据

因为主程序和ISR可能会共享变量
发生中断后 data consistency数据一致性会有问题
两种解决办法:

  • Avoiding sharing data between ISRs and the main program.
  • Enforcing mutual exclusion (i.e., stopping the ISR changing the variable values when they are in use by the main program).

Real-time Clock

Most computers have one or more hardware clocks which record the current “wall clock” time called Real Time Clocks (RTCs).
硬件时钟,主板纽扣电池供电的那个,用来给程序计时。也能用来引发中断
比如windows定时更新,时间一到就中断程序来更新

Q&A

What are the advantages of interrupts over a busy-wait ?

Advantages: Avoids wasted CPU time. Enables aperiodic events to be dealt with.
Weaknesses: Unpredictability (of scheduling). Overheads (context switching, task management).

In a typical real-time system, context switching may occur 100 − 1000 times a second.

(a) Why is context switching performed so often? To provide adequate response time to external events.
(b) Why isn’t context switching performed more often? Because a context switch takes time, and so if context switching is too frequent a high proportion of CPU time will be spent on context switches.

在这里插入图片描述

单词

notify 通知
alternative 替代的
mechanism 机制

<think>好的,我现在要回答用户关于中断(interrupts)在编程和操作系统中的技术概念和实现方法的问题。首先,我需要回顾一下已有的引用资料,确保回答准确。 用户提供的引用中提到了外部事件中断、定时器中断、以及中断在任务调度中的应用。比如引用[1]提到外部事件中断和定时器中断,引用[2]讨论了中断在任务切换中的作用,引用[3]涉及I/O设备与内存管理的关系,而引用[4]可能关于Cortex-M处理器的架构。这些资料可以帮助我构建回答的结构。 接下来,我需要确定中断的基本概念。中断是系统处理外部事件的一种机制,允许处理器暂停当前任务,转而去处理更高优先级的事件。这分为硬件中断和软件中断。硬件中断由外设触发,如键盘输入或定时器;软件中断则通过指令触发,比如系统调用。 然后,实现方法部分需要涵盖中断处理流程:触发、保存上下文、执行处理程序、恢复上下文。这里可以结合引用[1]提到的外部事件和定时器中断的例子,以及引用[2]中关于任务切换的中断应用。比如,当程序等待I/O时,中断会触发,让出CPU给其他任务,提高效率。 在编程中的应用,需要提到中断服务例程(ISR)的编写,强调其简洁性以避免延迟。同时,在操作系统中,中断处理与调度器协同,进行上下文切换。这里引用[2]中的多任务调度例子很合适,说明中断如何帮助任务切换。 可能的相关问题需要涵盖中断类型、处理机制、编程技巧以及应用场景。例如,用户可能想了解硬件与软件中断的区别,或者如何优化中断处理性能。引用[3]提到的I/O设备与内存管理的联系,可能引申到DMA和中断的关系,但用户的问题更偏向基础,这部分可能作为高级内容提及。 最后,要确保回答符合用户的要求:结构清晰,使用中文,正确引用资料,并生成相关问题。需要检查LaTeX格式是否正确,比如数学表达式用$...$,独立公式用$$...$$,但当前问题不涉及复杂公式,可能不需要使用。同时,代码块如果有的话需要用正确格式,但中断处理的具体代码可能根据上下文决定是否添加,比如简单的ISR示例。 总结步骤: 1. 定义中断及其分类。 2. 解释中断处理流程。 3. 讨论编程中的应用和操作系统中的角色。 4. 引用相关文献支持。 5. 生成相关问题。 现在组织语言,确保每个部分自然衔接,引用标注正确。例如,在提到任务切换时引用[2],在外部事件中断时引用[1]。同时,注意用户可能需要更偏向实际应用的信息,比如实现方法或常见问题,因此需要保持回答的实用性。</think>### 中断(Interrupts)的技术概念与实现方法 #### 1. **基本概念** 中断是计算机系统中用于响应外部事件或内部条件的机制,能够**暂停当前执行流程**,转而处理更高优先级的任务,处理完成后恢复原流程[^1]。 - **硬件中断**:由外部设备触发(如键盘输入、定时器到期、网络数据到达)[^1][^4]。 - **软件中断**:通过特定指令触发(如系统调用、异常处理)。 #### 2. **中断处理流程** 实现中断通常包含以下步骤: 1. **中断触发**:硬件发送信号至CPU,或软件执行中断指令。 2. **保存上下文**:CPU保存当前程序计数器(PC)和寄存器状态[^4]。 3. **执行中断服务程序(ISR)**:跳转到预定义的中断处理函数,完成事件响应(如读取键盘输入、更新定时器)[^1]。 4. **恢复上下文**:恢复原任务的寄存器状态,继续执行原程序。 #### 3. **编程中的应用** - **中断服务程序(ISR)**:需短小高效,避免长时间阻塞。 示例(伪代码): ```c void Timer_ISR() { update_timer(); // 更新定时器值 clear_interrupt_flag(); // 清除中断标志 } ``` - **操作系统调度**:中断用于任务切换。例如,当程序等待I/O时,中断触发调度器切换至其他任务[^2]。 #### 4. **操作系统中的关键角色** - **中断控制器**:管理中断优先级和分发(如APIC或NVIC[^4])。 - **中断屏蔽**:允许临时关闭中断,保护关键代码段。 - **中断与内存管理**:I/O设备通过中断与内存交互,DMA技术可减少CPU负担[^3]。 #### 5. **性能优化与挑战** - **延迟优化**:缩短ISR执行时间,使用**下半部机制**(如Linux的Tasklet)。 - **优先级反转**:通过嵌套中断或优先级继承解决。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值