A1.3 · Computer fundamentals · HL
A1.3 · 计算机基础 · HL
Managing Multitasking and Resource Allocation
管理多任务与资源分配
Your machine seems to run a browser, a music player and a dozen background tasks at once on a handful of cores. The OS makes that work by switching fast, sharing resources fairly, and untangling the jams that result.
你的机器看起来在区区几个核心上同时运行浏览器、音乐播放器和十几个后台任务。操作系统靠快速切换、公平分享资源,以及解开由此产生的堵塞,让这一切运转。
Explainer
讲解
Multitasking and resource allocation
多任务与资源分配
Multitasking is the OS running many processes on the same machine by switching between them so quickly that they appear to run at the same time. Resource allocation is the OS giving each running task the CPU time, memory and I/O access it needs to make progress.
多任务是指操作系统在同一台机器上运行许多进程,通过极快地在它们之间切换,使它们看起来在同时运行。资源分配是指操作系统给每个运行中的任务分配其取得进展所需的 CPU 时间、内存和 I/O 访问。
Each switch is a context switch: the OS saves the running process’s registers and state and loads the next one’s. Done fast enough, this creates the illusion of parallelism on a single core. The OS keeps a table of every process (its state, priority and resources held) to manage this.
每一次切换都是一次上下文切换:操作系统保存正在运行进程的寄存器和状态,再载入下一个进程的。切换得足够快,就在单个核心上营造出并行的错觉。操作系统维护一张记录每个进程(其状态、优先级和持有的资源)的表来管理这一切。
The guide names three challenges the OS must handle: task scheduling, resource contention and deadlock.
学科指南列出了操作系统必须应对的三个挑战:任务调度、资源争用和死锁。
Explainer
讲解
The three challenges and the OS response
三个挑战与操作系统的应对
| Challenge | What it is | How the OS responds |
|---|---|---|
| Task scheduling | Deciding the order processes run in, balancing quick response for interactive work against steady progress for background work | Uses scheduling algorithms (round robin, priority, multilevel queue) and adjusts priorities as load changes |
| Resource contention | Several processes wanting the same resource (a file, a device, a block of memory) at once | Enforces mutual exclusion with locks, semaphores or monitors so a shared resource is held by at most one process at any moment |
| Deadlock | A cycle where each process holds a resource another needs, so none can proceed | Prevention (design it out), avoidance (only grant requests that keep the system safe), or detection and recovery (spot the cycle and stop or roll back a process) |
| 挑战 | 它是什么 | 操作系统如何应对 |
|---|---|---|
| 任务调度 | 决定进程运行的顺序,在交互工作的快速响应与后台工作的稳步推进之间取得平衡 | 使用调度算法(轮转、优先级、多级队列),并随负载变化调整优先级 |
| 资源争用 | 多个进程同时想要同一资源(一个文件、一个设备、一块内存) | 用锁、信号量或管程强制互斥,使共享资源同一时刻只被一个进程使用 |
| 死锁 | 一个循环:每个进程都持有另一个进程需要的资源,于是谁都无法推进 | 预防(从设计上排除)、避免(只批准能让系统保持安全的请求),或检测与恢复(发现循环并停止或回滚某个进程) |
Explore
探索
Inside the resource-allocation problem
深入资源分配问题
Click each piece to see what it is, why it is hard, and what the OS does about it.
点击每一部分,看看它是什么、为什么难,以及操作系统对此做了什么。
Check your understanding
检验你的理解
Two processes are stuck: process A holds the printer and is waiting for the scanner; process B holds the scanner and is waiting for the printer. What is this, and how might the OS deal with it?
两个进程卡住了:进程 A 持有打印机,正在等扫描仪;进程 B 持有扫描仪,正在等打印机。这是什么,操作系统可能如何处理?
Common mistake
常见错误
RecurringTwo problems recur. First, answers name the challenges (“scheduling, contention, deadlock”) without explaining the mechanism or the consequence: explain needs both, how the OS handles each and what would go wrong if it did not. Second, deadlock and starvation are confused: starvation is one process indefinitely denied a resource; deadlock is a circular wait where a set of processes each block the next.
反复出现有两个问题反复出现。第一,答案只点出挑战的名字(“调度、争用、死锁”),却不解释机制或后果:解释需要两者兼备,即操作系统如何处理每一项,以及如果不处理会出什么问题。第二,死锁和饥饿被搞混:饥饿是某一个进程被无限期地拒绝分配资源;死锁是一种循环等待,一组进程各自挡住下一个。
Names only: “The OS manages multitasking by dealing with scheduling, resource contention and deadlock.” Lists the three challenges but explains none of them.
只有名字: “操作系统通过处理调度、资源争用和死锁来管理多任务。”列出了三个挑战,却一个也没解释。
Mechanism plus consequence: “The OS interleaves processes with a context switch, saving one process’s state and loading another’s, so a single core appears to run several at once; without this, one program would hold the CPU and freeze the rest. When processes contend for a shared file, the OS enforces mutual exclusion with a lock so writes cannot corrupt each other. If a circular wait forms, the OS detects the deadlock and pre-empts a resource to break the cycle, or prevents it by fixing the order resources are requested in.”
机制加后果: “操作系统用上下文切换把进程交错执行,保存一个进程的状态并载入另一个的,使单个核心看起来同时运行多个;没有这一点,一个程序就会占住 CPU,让其余程序卡死。当进程争用一个共享文件时,操作系统用锁强制互斥,使写入不会互相破坏。如果形成循环等待,操作系统检测到死锁并抢占一项资源来打破循环,或通过固定资源请求的顺序来预防它。”
Revision questions
复习题
Specimen-paper style. Write an answer, then reveal the marking points and mark yourself.
采用样卷风格。写下答案,然后展开评分要点给自己打分。
[6]Explain the role of the operating system in managing multitasking.
[6]解释操作系统在管理多任务中的作用。
Marking points
评分要点
- Multitasking = running many processes by switching between them fast (context switching) so they appear simultaneous on few cores.
- The OS keeps a process table of each process's state, priority and resources.
- It schedules which ready process runs next, balancing interactive responsiveness against background progress.
- Without this one program would hold the CPU and freeze the rest.
- 多任务 = 通过快速切换(上下文切换)运行许多进程,使它们在少数核心上看起来同时运行。
- 操作系统维护一张进程表,记录每个进程的状态、优先级和资源。
- 它调度下一个运行哪个就绪进程,在交互响应性与后台推进之间取得平衡。
- 没有这一点,一个程序就会占住 CPU,让其余程序卡死。
[6]Explain how the operating system manages resource contention between processes.
[6]解释操作系统如何管理进程之间的资源争用。
Marking points
评分要点
- Contention = several processes wanting the same resource at once (a file, a device, memory).
- The OS enforces mutual exclusion: a lock, semaphore or monitor lets only one process into the critical section at a time.
- Others wait until the holder releases it, so operations cannot corrupt each other.
- It must also avoid side effects such as starvation and priority inversion.
- 争用 = 多个进程同时想要同一资源(文件、设备、内存)。
- 操作系统强制互斥:锁、信号量或管程让同一时刻只有一个进程进入临界区。
- 其他进程等到持有者释放,使操作不会互相破坏。
- 它还必须避免饥饿、优先级反转等副作用。
[4]Explain what deadlock is and how an operating system can deal with it.
[4]解释死锁是什么,以及操作系统如何处理它。
Marking points
评分要点
- Deadlock = a circular wait where each process holds a resource another needs, so none can proceed.
- The OS can: prevent it (e.g. require resources to be requested in one fixed order so no cycle forms); avoid it (grant only requests that keep the system in a safe state); or detect the cycle and recover by pre-empting a resource or terminating a process.
- 死锁 = 一种循环等待:每个进程都持有另一个进程需要的资源,于是谁都无法推进。
- 操作系统可以:预防(例如要求按一个固定顺序请求资源,使循环无法形成);避免(只批准能让系统保持安全状态的请求);或检测到循环后通过抢占一项资源或终止某个进程来恢复。
[4]Explain how context switching supports multitasking, and why frequent switching reduces performance.
[4]解释上下文切换如何支持多任务,以及为什么频繁切换会降低性能。
Marking points
评分要点
- Context switching saves one process's state and loads another's, so a single core can run several processes in turn and appear to run them at once.
- Each switch does no useful work; it is overhead.
- If the OS switches very often, a large fraction of CPU time goes on saving/restoring state instead of running programs, so performance drops.
- 上下文切换保存一个进程的状态并载入另一个的,使单个核心能轮流运行多个进程,看起来同时运行。
- 每次切换本身不做有用的工作;它是开销。
- 如果操作系统切换非常频繁,很大一部分 CPU 时间花在保存 / 恢复状态而非运行程序上,性能因此下降。
[3]Explain the difference between deadlock and starvation.
[3]解释死锁与饥饿的区别。
Marking points
评分要点
- Starvation: one process is perpetually denied a resource it needs (e.g. a low-priority job while higher-priority jobs keep arriving). The system as a whole still makes progress.
- Deadlock: a set of processes each hold a resource and wait for one another in a cycle, so none of them can proceed.
- 饥饿:某一个进程持续被拒绝分配它所需的资源(例如当更高优先级的任务不断到来时的低优先级任务)。系统整体仍在推进。
- 死锁:一组进程各持有一项资源并循环地互相等待,于是它们谁都无法推进。
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.5, and every other IB CS 2027 statement.
免费账户:解锁 A1.3.5 以及所有其他 IB CS 2027 考纲陈述的每一道复习题,并同步你的学习进度。