First exams 2027 · SL & HL 2027 年首次开考 · SL 与 HL

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.

键盘、网卡和硬盘都会在难以预料的时刻需要处理器的关注。操作系统要么等着被通知(中断),要么不停地问(轮询),而正确的选择取决于具体情况。

Core for SL & HL SL 与 HL 核心内容 Typically 4–6 marks · AO3 通常 4–6 分 · AO3 Command term: evaluate 命令词:evaluate(评价)

Explainer

讲解

Two ways for the CPU to notice a device

CPU 察觉设备的两种方式

Df

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),然后恢复状态并继续。

Df

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

要权衡的六个因素

FactorInterruptsPolling
Event frequencyEfficient when events are rare: the CPU is only disturbed when something happensEfficient when events are frequent and regular: little is wasted between checks
CPU processing overheadLow when events are rare, but each interrupt costs a context switchHigh: cycles are spent checking even when nothing has happened
Power sourceBetter for battery devices: the CPU can sleep until an interrupt wakes itWorse for battery: the CPU must stay awake to keep checking
Event predictabilityBetter for unpredictable events: response is immediate whenever they occurBetter for predictable events: checks can be timed to match them
Controlled latencyFast response, but very frequent interrupts make response times varyPredictable response: the delay is at most one polling interval
Security concernsA 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

常见错误

A recurring weakness on “evaluate polling and interrupt handling” “评价轮询与中断处理”这类题反复出现的弱点

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.

采用样卷风格。写下答案,然后展开评分要点给自己打分。

Question 1 · 6 marks 第 1 题 · 6 分

[6]Evaluate the use of polling and interrupt handling for keyboard and mouse input.

[6]评价轮询和中断处理在键盘和鼠标输入上的使用。

Question 2 · 6 marks 第 2 题 · 6 分

[6]Evaluate polling versus interrupt handling for a battery-powered embedded sensor.

[6]针对一个靠电池供电的嵌入式传感器,评价轮询与中断处理的取舍。

Question 3 · 4 marks 第 3 题 · 4 分

[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 增加的开销,如何影响究竟选择轮询还是中断。

Question 4 · 4 marks 第 4 题 · 4 分

[4]Explain why interrupt handling is usually preferred for network communications.

[4]解释为什么网络通信通常首选中断处理。

Question 5 · 3 marks 第 5 题 · 3 分

[3]Explain what is meant by controlled latency and why it matters in a real-time system.

[3]解释“可控延迟”是什么意思,以及它为什么在实时系统中重要。

Flashcards

记忆卡片

Active recall for this statement’s key terms.

针对本条考纲陈述关键术语的主动回忆练习。

Prototype render · grounded in the IB CS 2027 subject guide and the Hodder and Oxford IB CS 2027 texts for A1.3.4 · not yet wired to live routes