【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 机制

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值