📚 Operating Systems: Processes, Scheduling and Memory Management | 操作系统:进程、调度与内存管理
An operating system (OS) is the foundational software that manages computer hardware and provides services for application programs. In the Edexcel A-Level Computer Science specification, a deep understanding of how an OS handles processes, schedules CPU time, and controls memory allocation is essential. This article synthesises key concepts from the Pearson ActiveLearn combined resource on operating systems, focusing on process management, scheduling algorithms, concurrency, and memory management techniques.
操作系统(OS)是管理计算机硬件并为应用程序提供服务的底层软件。在 Edexcel A-Level 计算机科学考纲中,深入理解操作系统如何处理进程、调度 CPU 时间以及控制内存分配至关重要。本文综合了 Pearson ActiveLearn 操作系统综合资源中的关键概念,重点关注进程管理、调度算法、并发性和内存管理技术。
1. The Role of an Operating System | 操作系统的角色
The OS acts as an intermediary between the user and the hardware. Its primary goals are to execute user programs, make the computer system convenient to use, and manage hardware resources efficiently. Without an OS, each application would need to include code to drive the hardware directly, leading to enormous complexity and conflicts.
操作系统充当用户与硬件之间的中介。其主要目标是执行用户程序、使计算机系统易于使用并高效管理硬件资源。没有操作系统,每个应用程序都必须包含直接驱动硬件的代码,从而导致极大的复杂性和冲突。
Core functions include process management, memory management, file system management, I/O management, and providing a user interface. In an embedded context, the OS may be minimal, but the same resource‑arbitration principles apply.
核心功能包括进程管理、内存管理、文件系统管理、输入输出管理以及提供用户界面。在嵌入式环境中,操作系统可能很精简,但相同的资源仲裁原则仍然适用。
2. Process Concept and Process States | 进程概念与进程状态
A process is a program in execution. It is more than just the program code (sometimes called the text section); it also includes the program counter, processor registers, a stack for temporary data, and a data section containing global variables. The OS maintains a Process Control Block (PCB) for every process, which stores all the information needed to manage and resume it.
进程是正在执行的程序。它不仅是程序代码(有时称为文本段),还包括程序计数器、处理器寄存器、用于临时数据的栈以及包含全局变量的数据段。操作系统为每个进程维护一个进程控制块(PCB),其中存储了管理和恢复该进程所需的所有信息。
During its lifetime, a process transitions through several states: New (being created), Ready (waiting for the CPU), Running (executing instructions), Waiting (blocked for I/O or an event), and Terminated (finished execution). A typical state diagram shows how a process moves from Ready to Running and then either back to Ready if its time slice expires or to Waiting if an I/O request occurs.
在生命周期中,进程会经历多个状态:新建(正在创建)、就绪(等待 CPU)、运行(正在执行指令)、等待(因 I/O 或事件而阻塞)和终止(执行完毕)。典型的状态图展示了进程如何从就绪变为运行,然后如果时间片用完就返回就绪,或者若发生 I/O 请求就进入等待状态。
3. Context Switching and Multitasking | 上下文切换与多任务
To give the illusion of parallelism on a single‑core CPU, the OS rapidly switches between processes. This procedure, called a context switch, involves saving the state of the currently running process (PCB) and loading the saved state of the next process to execute. Context switching is pure overhead; the CPU does no useful work during the switch.
为了在单核 CPU 上造成并行的假象,操作系统会快速在进程之间切换。这个称为上下文切换的过程,需要保存当前运行进程的状态(PCB)并加载下一个要执行进程的已保存状态。上下文切换完全是额外开销;在切换期间 CPU 没有做有用功。
The frequency of context switches is determined by the scheduling algorithm and the time quantum. Too frequent switching reduces throughput, while too long a quantum can make the system feel sluggish and unresponsive.
上下文切换的频率由调度算法和时间片决定。切换过于频繁会降低吞吐量,而时间片过长则会使系统感觉迟钝、反应不灵敏。
4. CPU Scheduling Objectives | CPU 调度目标
Scheduling aims to keep the CPU busy at all times and to maximise the number of processes completed per unit time (throughput). Other criteria include minimising turnaround time (total time from submission to completion), waiting time (time spent in the ready queue), and response time (time from request to first response).
调度的目标是让 CPU 始终保持忙碌,并最大化单位时间内完成的进程数量(吞吐量)。其他标准包括最小化周转时间(从提交到完成的总时间)、等待时间(在就绪队列中花费的时间)和响应时间(从请求到首次响应的时间)。
Different systems have different objectives; a batch system cares about throughput and turnaround time, whereas an interactive system prioritises low response time. Real‑time systems must meet hard deadlines.
不同系统有不同的目标;批处理系统关心吞吐量和周转时间,而交互式系统则优先考虑低响应时间。实时系统必须满足硬性截止时间。
5. Scheduling Algorithms: FCFS and SJF | 调度算法:先来先服务和最短作业优先
First‑Come, First‑Served (FCFS) is the simplest algorithm. Processes are executed in the order they arrive, and they run to completion without pre‑emption. Its major drawback is the convoy effect: short processes get stuck behind long ones, drastically increasing average waiting time.
先来先服务(FCFS)是最简单的算法。进程按照到达顺序执行,并且一直运行到完成而不被抢占。它的主要缺点是护航效应:短进程被长进程阻塞,极大地增加了平均等待时间。
Shortest Job First (SJF) selects the process with the smallest next CPU burst. It is provably optimal in terms of minimum average waiting time, but it requires knowing the length of future CPU bursts, which is generally impossible. Pre‑emptive SJF (Shortest Remaining Time First) can interrupt a running process if a new one with a shorter burst arrives.
最短作业优先(SJF)选择下一次 CPU 脉冲最短的进程。它在最小平均等待时间方面是公认最优的,但需要预先知道未来 CPU 脉冲的长度,这通常是不可能的。抢占式 SJF(最短剩余时间优先)可在有更短脉冲的新进程到达时中断当前运行进程。
6. Round Robin and Priority Scheduling | 轮转调度与优先级调度
Round Robin (RR) is designed for time‑sharing systems. Each process gets a small time quantum (e.g., 10–100 ms). After the quantum expires, the process is pre‑empted and moved to the back of the ready queue. RR gives good response times, but performance depends heavily on the quantum size.
轮转调度(RR)专为分时系统设计。每个进程获得一个小的时间片(例如 10–100 毫秒)。时间片用完后,进程被抢占并移到就绪队列末尾。RR 提供良好的响应时间,但性能很大程度上取决于时间片的大小。
Priority scheduling associates a priority with each process. The CPU is allocated to the highest‑priority ready process. To prevent starvation of low‑priority processes, aging is used: a process’s priority gradually increases over time. Priority inversion can occur in systems with shared resources, often resolved by priority inheritance protocols.
优先级调度将每个进程关联一个优先级。CPU 分配给优先级最高的就绪进程。为防止低优先级进程饥饿,会使用老化机制:进程的优先级随等待时间逐渐提升。在存在共享资源的系统中可能发生优先级反转,通常通过优先级继承协议来解决。
7. Multilevel Queue and Fair Share Scheduling | 多级队列与公平共享调度
Modern operating systems often divide the ready queue into multiple queues based on process type (foreground, background). Each queue has its own scheduling algorithm. For example, an interactive queue might use RR, while a batch queue uses FCFS. Scheduling between queues is typically done using fixed‑priority pre‑emptive scheduling or time‑slicing.
现代操作系统通常根据进程类型(前台、后台)将就绪队列划分为多个队列。每个队列有自己的调度算法。例如,交互式队列可能使用 RR,而批处理队列使用 FCFS。队列之间的调度通常采用固定优先级抢占式调度或时间片划分。
Fair share scheduling allocates CPU resources not just per process but per user or group, ensuring that no single user can monopolise the system. It uses a weighted mechanism where each group receives a fraction of the CPU time proportional to its weight.
公平共享调度不仅按进程分配 CPU 资源,还按用户或用户组分配,确保没有任何单一用户能够独占系统。它使用加权机制,每个组按其权重比例获得 CPU 时间片段。
8. Concurrency and the Critical Section Problem | 并发性与临界区问题
When multiple processes or threads share data, race conditions can occur if the final outcome depends on the order of execution. To avoid this, we must protect critical sections: code segments that access shared resources. A solution must satisfy mutual exclusion (only one process in the critical section at a time), progress (if no process is in the critical section, selection cannot be postponed indefinitely), and bounded waiting (no process should wait forever).
当多个进程或线程共享数据时,如果最终结果取决于执行顺序,就可能发生竞态条件。为避免这种情况,我们必须保护临界区:即访问共享资源的代码段。解决方案必须满足互斥(一次仅允许一个进程进入临界区)、进展(若无进程在临界区,不能无限推迟选择)和有限等待(没有任何进程会永远等待)。
Various mechanisms can enforce mutual exclusion, including disabling interrupts, using atomic test‑and‑set instructions, semaphores, and monitors. Semaphores use two atomic operations: wait() and signal(). A binary semaphore acts as a simple lock, whereas a counting semaphore controls access to a finite number of identical resources.
多种机制可用于强制互斥,包括关中断、使用原子 test‑and‑set 指令、信号量和管程。信号量使用两个原子操作:wait() 和 signal()。二元信号量相当于简单锁,而计数信号量可控制对有限数量同质资源的访问。
9. Deadlock: Conditions and Prevention | 死锁:条件与预防
A deadlock is a situation where a set of processes are blocked because each is holding a resource and waiting for another resource held by another process in the set. Four necessary conditions must hold simultaneously: mutual exclusion, hold and wait, no pre‑emption, and circular wait.
死锁是指一组进程中的每一个都在等待本组其他进程持有的资源,从而导致所有进程都被阻塞的状况。四个必要条件必须同时成立:互斥、持有并等待、不可抢占和循环等待。
Deadlock prevention works by ensuring that at least one of these conditions never occurs. For example, a system can require that a process request all its resources at once (breaking hold and wait) or impose a total ordering on resource types (breaking circular wait). Deadlock avoidance, using algorithms like the Banker’s algorithm, requires advance knowledge of maximum resource needs and dynamically checks for unsafe states.
死锁预防通过确保至少一个必要条件永不成立来避免死锁。例如,系统可以要求进程一次性申请所有资源(打破持有并等待)或给资源类型强加全序(打破循环等待)。死锁避免(如使用银行家算法)需要预先知道最大资源需求,并动态检查不安全状态。
10. Memory Management: Basics and Address Binding | 内存管理:基础与地址绑定
Memory management keeps track of which parts of memory are in use, allocates memory to processes, and deallocates it when they finish. Address binding maps logical addresses (generated by the CPU) to physical addresses (actual memory locations). Binding can happen at compile time, load time, or execution time, with execution‑time binding supporting the most flexibility, enabling processes to be moved during execution.
内存管理负责记录内存哪些部分正在使用、为进程分配内存并在进程结束时回收。地址绑定将逻辑地址(CPU 生成的地址)映射到物理地址(实际内存位置)。绑定可在编译时、加载时或执行时发生,其中执行时绑定提供最大的灵活性,允许进程在执行期间移动。
The Memory Management Unit (MMU) hardware performs the run‑time mapping by adding a relocation register’s value to every logical address, protecting processes from accessing each other’s memory.
内存管理单元(MMU)硬件通过在每一个逻辑地址上加上重定位寄存器的值来完成运行时映射,从而保护各进程互不侵犯内存。
11. Paging and Segmentation | 分页与分段
Paging divides physical memory into fixed‑sized blocks called frames, and logical memory into blocks of the same size called pages. When a process is loaded, its pages are placed into any available frames; a page table maps each logical page to a physical frame. This eliminates external fragmentation but suffers from internal fragmentation (the last page may not be fully used).
分页将物理内存划分为固定大小的块,称为帧;将逻辑内存划分为相同大小的块,称为页。加载进程时,其页面被放入任何可用的帧中;页表负责将每个逻辑页映射到物理帧。这消除了外部碎片,但存在内部碎片(最后一页可能未完全利用)。
Segmentation divides memory into variable‑sized segments that correspond to logical units such as code, stack, or heap. Each segment has a base address and a limit. Segmentation matches the programmer’s view of memory but introduces external fragmentation, often mitigated by compaction or combined paged segmentation.
分段将内存划分为可变大小的段,每个段对应一个逻辑单元,如代码段、栈段或堆段。每个段有一个基址和界限。分段符合程序员对内存的视图,但会引入外部碎片,通常通过紧凑化或结合分页来缓解。
12. Virtual Memory and Demand Paging | 虚拟内存与请求调页
Virtual memory allows the execution of processes that are not completely in memory. The logical address space can be larger than physical memory because only the active parts of a process need reside in RAM at any time. Demand paging loads a page into memory only when it is referenced; an invalid page reference triggers a page fault, causing the OS to bring the required page from disk, potentially swapping out another page.
虚拟内存允许执行不全部驻留在内存中的进程。逻辑地址空间可以大于物理内存,因为在任何时刻只有进程的活动部分需要驻留在 RAM 中。请求调页仅在页面被引用时才将其加载入内存;无效的页面引用会触发缺页中断,导致操作系统从磁盘调入所需页面,并可能换出另一个页面。
Page replacement algorithms (FIFO, Optimal, LRU) select a victim frame when free frames are scarce. Thrashing occurs when the system spends more time swapping pages than executing processes, severely degrading performance. The working set model helps prevent thrashing by ensuring each process has enough frames to hold its frequently accessed pages.
页面置换算法(FIFO、最优、LRU)在空闲帧不足时选择一个牺牲帧。当系统花费在换页上的时间比执行进程还多时,就会发生颠簸,严重降低性能。工作集模型通过确保每个进程有足够帧来容纳其频繁访问的页面,从而防止颠簸。
Published by TutorHao | Computing Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply