A1.3 · Computer fundamentals
A1.3 · 计算机基础
Polling and Interrupt Handling
轮询与中断处理
A keyboard, a network card and a hard drive all need the processor’s attention at unpredictable moments. The OS can either wait to be told (interrupts) or keep asking (polling), and the right choice depends on the situation.
键盘、网卡和硬盘都会在难以预料的时刻需要处理器的关注。操作系统要么等着被通知(中断),要么不停地问(轮询),而正确的选择取决于具体情况。
Explainer
讲解
Two ways for the CPU to notice a device
CPU 察觉设备的两种方式
With interrupt handling, a device sends the CPU a signal when it needs attention. The CPU pauses its current work, saves its state, runs an interrupt service routine (ISR) that handles the event, then restores its state and carries on.
在中断处理方式下,设备在需要处理时向 CPU 发送一个信号。CPU 暂停当前工作,保存自己的状态,运行一段处理该事件的中断服务程序(ISR),然后恢复状态并继续。
With polling, the CPU itself repeatedly checks each device at regular intervals to ask whether it needs service. It is a loop that runs whether or not anything has actually happened.
在轮询方式下,CPU 自己以固定间隔反复检查每个设备,询问它是否需要服务。这是一个循环,无论是否真的发生了什么都在运行。
Neither is simply better. Which one wins depends on the situation, and the guide names six factors to weigh.
两者并非哪一个单纯更好。哪一个胜出取决于具体情况,学科指南列出了六个要权衡的因素。
Explainer
讲解
The six factors to weigh
要权衡的六个因素
| Factor | Interrupts | Polling |
|---|---|---|
| Event frequency | Efficient when events are rare: the CPU is only disturbed when something happens | Efficient when events are frequent and regular: little is wasted between checks |
| CPU processing overhead | Low when events are rare, but each interrupt costs a context switch | High: cycles are spent checking even when nothing has happened |
| Power source | Better for battery devices: the CPU can sleep until an interrupt wakes it | Worse for battery: the CPU must stay awake to keep checking |
| Event predictability | Better for unpredictable events: response is immediate whenever they occur | Better for predictable events: checks can be timed to match them |
| Controlled latency | Fast response, but very frequent interrupts make response times vary | Predictable response: the delay is at most one polling interval |
| Security concerns | A flood of interrupts can be used to overload the CPU (denial of service) | Long polling gaps can delay detection of a critical or malicious event |
| 因素 | 中断 | 轮询 |
|---|---|---|
| 事件频率 | 事件稀少时高效:只有发生事情时 CPU 才被打扰 | 事件频繁且规律时高效:两次检查之间几乎不浪费 |
| CPU 处理开销 | 事件稀少时开销低,但每次中断都要付一次上下文切换 | 开销高:即使什么都没发生也在花时间检查 |
| 电源 | 对电池设备更好:CPU 可以睡眠,直到中断把它唤醒 | 对电池更差:CPU 必须保持清醒以持续检查 |
| 事件可预测性 | 对不可预测的事件更好:一旦发生就立即响应 | 对可预测的事件更好:检查时机可以与之匹配 |
| 可控延迟 | 响应快,但非常频繁的中断会使响应时间波动 | 响应可预测:延迟最多为一个轮询间隔 |
| 安全考量 | 大量中断可被用来使 CPU 过载(拒绝服务) | 轮询间隔过长会延迟对关键或恶意事件的检测 |
Explore
探索
Which method each scenario uses
每种场景使用哪种方法
The guide names five real-world settings. Click each to see which method is normally chosen and which factors decide it.
学科指南列出了五种现实场景。点击每一种,看看通常选择哪种方法,以及是哪些因素决定的。
Check your understanding
检验你的理解
A battery-powered sensor in a remote field reports a reading only a few times a day. Which approach fits best, and why?
一个装在偏远田地里、靠电池供电的传感器每天只上报几次读数。哪种方式最合适,为什么?
Common mistake
常见错误
RecurringWeak answers state a blanket rule: “interrupts are better than polling”. Evaluate means weigh both against the situation using the named factors and reach a supported judgement. The right method depends on event frequency and predictability, the power source, the latency the system needs, and security. Say which factors dominate in this scenario and let the judgement follow.
反复出现薄弱的答案会给出一条一刀切的规则:“中断比轮询好”。评价意味着用列出的因素,针对具体情况权衡两者,并得出有依据的判断。正确的方法取决于事件频率和可预测性、电源、系统需要的延迟,以及安全。要说明在这个场景中哪些因素占主导,判断随之而来。
Blanket rule: “Interrupt handling is always the better choice because the CPU does not waste time checking devices.” Ignores that polling wins for frequent, predictable events and can give more controlled latency.
一刀切规则: “中断处理永远是更好的选择,因为 CPU 不会浪费时间去检查设备。”忽视了轮询在频繁、可预测的事件上更优,而且能提供更可控的延迟。
Weighed against the scenario: “For keyboard input, events are infrequent and unpredictable, so interrupts are chosen: the CPU stays free and responds instantly on a keypress, and the context-switch cost is negligible at typing speed. For a robot arm sampling a sensor every 10 ms at a fixed rate, polling is chosen: the events are perfectly predictable, so timed checks give tightly controlled latency without interrupt setup.”
针对场景权衡: “对于键盘输入,事件不频繁且不可预测,所以选择中断:CPU 保持空闲,一旦按键立即响应,而在打字速度下上下文切换的成本可忽略。对于每 10 毫秒以固定速率采样一次传感器的机械臂,选择轮询:事件完全可预测,所以定时检查能提供严格可控的延迟,又不需要中断的设置开销。”
Revision questions
复习题
Specimen-paper style. Write an answer, then reveal the marking points and mark yourself.
采用样卷风格。写下答案,然后展开评分要点给自己打分。
[6]Evaluate the use of polling and interrupt handling for keyboard and mouse input.
[6]评价轮询和中断处理在键盘和鼠标输入上的使用。
Marking points
评分要点
- Keyboard/mouse events are infrequent (on a CPU timescale) and unpredictable.
- Interrupts are chosen: the CPU stays free and responds instantly on a keypress; the context-switch cost is negligible at typing speed.
- Polling would waste CPU cycles constantly checking idle devices and could add lag if the CPU is busy.
- Judgement: interrupts, because event frequency and the need for immediate response dominate here.
- 键盘 / 鼠标事件(以 CPU 时间尺度看)不频繁且不可预测。
- 选择中断:CPU 保持空闲,按键时立即响应;打字速度下上下文切换成本可忽略。
- 轮询会浪费 CPU 周期不断检查空闲设备,若 CPU 忙还可能增加延迟。
- 判断:中断,因为这里事件频率和即时响应的需求占主导。
[6]Evaluate polling versus interrupt handling for a battery-powered embedded sensor.
[6]针对一个靠电池供电的嵌入式传感器,评价轮询与中断处理的取舍。
Marking points
评分要点
- Events are rare and the device runs on a battery.
- Interrupts let the CPU sleep in a low-power state and wake only when a reading actually arrives, which is the most power-efficient choice.
- Polling would keep the CPU awake all day checking a sensor that almost never has news, draining the battery.
- Judgement: interrupts, because power source and low event frequency dominate; if events were perfectly regular, polling could be argued.
- 事件稀少,且设备靠电池供电。
- 中断让 CPU 处于低功耗睡眠,只在真的有读数到来时才唤醒,这是最省电的选择。
- 轮询会让 CPU 整天醒着去检查一个几乎从不带来消息的传感器,把电池耗光。
- 判断:中断,因为电源和低事件频率占主导;若事件完全规律,也可论证用轮询。
[4]Explain how the rate at which events occur, and the CPU overhead each method adds, affect whether polling or interrupts is chosen.
[4]解释事件发生的频率,以及每种方法给 CPU 增加的开销,如何影响究竟选择轮询还是中断。
Marking points
评分要点
- Event frequency: rare events suit interrupts (CPU undisturbed until something happens); frequent regular events suit polling.
- CPU overhead: polling wastes cycles checking even when nothing has happened; very frequent interrupts cost many context switches.
- So a high-frequency, predictable stream may favour polling, while sporadic events favour interrupts.
- 事件频率:稀少的事件适合中断(在有事发生前 CPU 不被打扰);频繁而规律的事件适合轮询。
- CPU 开销:即使什么都没发生,轮询也在花周期检查;非常频繁的中断带来许多次上下文切换。
- 所以高频、可预测的数据流可能倾向轮询,而零星的事件倾向中断。
[4]Explain why interrupt handling is usually preferred for network communications.
[4]解释为什么网络通信通常首选中断处理。
Marking points
评分要点
- Packets arrive at unpredictable times.
- An interrupt lets the network card alert the CPU the moment data is ready, so it is processed with minimal latency.
- Polling would either waste cycles on an idle card or miss packets / add latency if the CPU is busy when data arrives.
- 数据包在不可预测的时刻到达。
- 中断让网卡在数据就绪的那一刻提醒 CPU,使数据以最小延迟被处理。
- 轮询要么在空闲网卡上浪费周期,要么在数据到达时 CPU 正忙而错过数据包 / 增加延迟。
[3]Explain what is meant by controlled latency and why it matters in a real-time system.
[3]解释“可控延迟”是什么意思,以及它为什么在实时系统中重要。
Marking points
评分要点
- Controlled latency: the delay between an event and the system's response is predictable and bounded.
- In a real-time system a late response can be unsafe or useless (e.g. an airbag, an industrial controller).
- Timed polling bounds the delay to one polling interval; interrupts give a fast response but can vary if interrupts are very frequent.
- 可控延迟:事件与系统响应之间的延迟是可预测且有上限的。
- 在实时系统中,迟到的响应可能不安全或无用(例如安全气囊、工业控制器)。
- 定时轮询把延迟限制在一个轮询间隔内;中断响应快,但在中断非常频繁时会波动。
Flashcards
记忆卡片
Active recall for this statement’s key terms.
针对本条考纲陈述关键术语的主动回忆练习。
Keep this statement’s practice with you
把这条考纲陈述的练习保存下来
Free account: save your progress and unlock every revision question for A1.3.4, and every other IB CS 2027 statement.
免费账户:解锁 A1.3.4 以及所有其他 IB CS 2027 考纲陈述的每一道复习题,并同步你的学习进度。