📚 Operating Systems: Processes, Scheduling and Memory Management | 操作系统:进程、调度与内存管理
In Edexcel A-Level Programming, the operating system is a core topic because it explains how software and hardware work together to execute programs, manage resources, and provide a stable environment for users and applications. Understanding processes, scheduling algorithms, and memory management is essential for tackling both short-answer and extended-response exam questions.
在 Edexcel A-Level 编程考试中,操作系统是一个核心主题,因为它解释了软件与硬件如何协同工作来执行程序、管理资源,并为用户和应用程序提供稳定的运行环境。理解进程、调度算法和内存管理对于应对简答题和论述题都至关重要。
1. What is an Operating System? | 什么是操作系统?
An operating system is system software that acts as an interface between the user, application software, and computer hardware. It manages hardware resources such as the CPU, memory, storage devices, and input/output peripherals, while providing services like file management, process management, security, and user interfaces.
操作系统是一种系统软件,充当用户、应用软件和计算机硬件之间的接口。它管理 CPU、内存、存储设备和输入/输出外设等硬件资源,同时提供文件管理、进程管理、安全性和用户界面等服务。
Common examples include Windows, Linux, macOS, Android, and iOS. In A-Level questions, you may be asked to distinguish between the kernel, which is the core part of the operating system that stays in memory and controls hardware, and the shell, which is the outer layer that communicates with users.
常见的操作系统包括 Windows、Linux、macOS、Android 和 iOS。在 A-Level 考试中,可能需要区分内核和外壳:内核是操作系统中常驻内存并控制硬件的核心部分,而外壳是与用户通信的外层部分。
2. Process vs Program | 进程与程序
A program is a static set of instructions stored on disk, usually as an executable file. A process is a program in execution, which includes the program code, current activity represented by the program counter, CPU registers, stack, and data section. A single program can create multiple processes.
程序是存储在磁盘上的静态指令集,通常以可执行文件形式存在。进程是正在执行的程序,包括程序代码、由程序计数器表示的当前活动、CPU 寄存器、堆栈和数据段。一个程序可以创建多个进程。
Each process is represented in the operating system by a Process Control Block, often shortened to PCB. The PCB stores process ID, process state, program counter, register values, memory limits, and list of open files. This information is essential for context switching and scheduling.
每个进程在操作系统中由进程控制块表示,通常缩写为 PCB。PCB 存储进程 ID、进程状态、程序计数器、寄存器值、内存限制以及打开文件列表。这些信息对于上下文切换和调度至关重要。
3. Process States | 进程状态
A process moves through several states during its lifetime. The five-state model used in Edexcel includes New, Ready, Running, Blocked, and Terminated. New is when the process is being created; Ready means it is loaded in memory and waiting for CPU time; Running means its instructions are currently being executed; Blocked means it cannot continue until some event occurs, such as I/O completion; Terminated means the process has finished.
进程在其生命周期中会经历多个状态。Edexcel 使用的五状态模型包括新建、就绪、运行、阻塞和终止。新建表示进程正在创建;就绪表示进程已加载到内存中并等待 CPU 时间;运行表示其指令正在被执行;阻塞表示进程在某个事件发生之前无法继续,例如等待 I/O 完成;终止表示进程已经结束。
The transitions between states are important: a process moves from New to Ready, from Ready to Running when the scheduler selects it, from Running to Ready if its time slice expires, from Running to Blocked if it waits for I/O, from Blocked to Ready when the event completes, and finally to Terminated when it exits. Understanding these transitions helps answer questions on pre-emptive and non-pre-emptive scheduling.
状态之间的转换非常重要:进程从新建到就绪,当调度程序选择它时从就绪到运行,如果时间片用完则从运行回到就绪,如果等待 I/O 则从运行到阻塞,当事件完成时从阻塞到就绪,最后退出时到终止。理解这些转换有助于回答关于抢占式和非抢占式调度的问题。
4. Scheduling Algorithms: FCFS, SJF, Round Robin, Priority | 调度算法:FCFS、SJF、轮转、优先级
CPU scheduling decides which ready process gets the CPU next. The four main algorithms required at A-Level are First Come First Served, often called FCFS; Shortest Job First, often called SJF; Round Robin; and Priority Scheduling. Each has advantages and disadvantages in terms of average waiting time, turnaround time, and fairness.
CPU 调度决定哪个就绪进程接下来获得 CPU。A-Level 要求的四种主要算法是先来先服务,通常称为 FCFS;最短作业优先,通常称为 SJF;轮转调度;以及优先级调度。每种算法在平均等待时间、周转时间和公平性方面都有优缺点。
FCFS executes processes in the order they arrive. It is easy to implement with a FIFO queue, but can cause the convoy effect where short jobs wait behind long jobs. SJF selects the process with the shortest CPU burst next, which minimises average waiting time but can lead to starvation of long processes if short jobs keep arriving.
FCFS 按进程到达的顺序执行。它使用先进先出队列易于实现,但可能导致护航效应,即短作业排在长作业后面等待。SJF 选择下一次 CPU 突发时间最短的进程,这可以最小化平均等待时间,但如果短作业不断到达,可能导致长进程饥饿。
Round Robin gives each process a fixed time slice or quantum, typically between 10 and 100 milliseconds. If a process does not finish within its quantum, it is pre-empted and moved to the back of the ready queue. This ensures responsiveness but increases context switching overhead. Priority scheduling always picks the ready process with the highest priority, which can be static or dynamically adjusted to avoid starvation.
轮转调度为每个进程分配一个固定的时间片或量程,通常在 10 到 100 毫秒之间。如果进程在时间片内没有完成,它就会被抢占并移到就绪队列的末尾。这保证了响应性,但增加了上下文切换的开销。优先级调度始终选择就绪进程中优先级最高的进程,优先级可以是静态的,也可以动态调整以避免饥饿。
Summary comparison table:
算法对比总结表:
| Algorithm | 算法 | Pre-emptive? | 是否抢占 | Main advantage | 主要优点 | Main disadvantage | 主要缺点 |
| FCFS | No | Simple, fair in arrival order | Convoy effect, poor average waiting time |
| SJF | No (or yes for SRTF) | Minimises average waiting time | Starvation of long jobs, needs future knowledge |
| Round Robin | Yes | Fair, good for interactive systems | High context switching overhead |
| Priority | Can be either | Reflects importance of processes | Starvation if static low priority |
Turnaround time is the total time from process arrival to completion, while waiting time is the total time a process spends in the ready queue. The formula for turnaround time is:
周转时间是从进程到达到完成的总时间,而等待时间是进程在就绪队列中花费的总时间。周转时间的公式为:
Turnaround time = Completion time − Arrival time
周转时间 = 完成时间 − 到达时间
5. Memory Management: Paging and Segmentation | 内存管理:分页与分段
Memory management is the process of controlling and coordinating computer memory, assigning portions called blocks to various running programs to optimise overall system performance. The two main techniques at A-Level are paging and segmentation. Both allow logical addresses used by a process to be mapped to physical addresses in RAM.
内存管理是控制和协调计算机内存的过程,将称为块的各部分分配给各种正在运行的程序,以优化整体系统性能。A-Level 中的两种主要技术是分页和分段。两者都允许进程使用的逻辑地址映射到 RAM 中的物理地址。
Paging divides physical memory into fixed-size blocks called frames, and logical memory into blocks of the same size called pages. A page table maps each page to a frame. Paging avoids external fragmentation because all blocks are equal in size, but it may cause internal fragmentation if a process does not use the full last page.
分页将物理内存划分为固定大小的块,称为页框;将逻辑内存划分为相同大小的块,称为页面。页表将每个页面映射到一个页框。分页避免了外部碎片,因为所有块大小相同,但如果进程没有用完最后一页的全部空间,可能会产生内部碎片。
Segmentation divides memory into variable-sized logical units called segments, such as code segment, data segment, heap, and stack. Each segment has a base and limit, and a segment table maps logical segments to physical locations. Segmentation reflects the programmer’s view of memory but can cause external fragmentation because segments vary in size.
分段将内存划分为可变大小的逻辑单元,称为段,例如代码段、数据段、堆和栈。每个段都有基址和界限,段表将逻辑段映射到物理位置。分段反映了程序员对内存的视图,但由于段大小不同,可能会产生外部碎片。
6. Virtual Memory | 虚拟内存
Virtual memory is a technique that allows a computer to compensate for shortages of physical memory by temporarily transferring pages of data from RAM to disk storage. This gives the illusion of a much larger memory space than the actual physical RAM. It is implemented using demand paging, where pages are loaded into RAM only when they are needed.
虚拟内存是一种技术,通过将数据页从 RAM 临时转移到磁盘存储,来弥补物理内存的不足。这给人一种比实际物理 RAM 大得多的内存空间的错觉。它通过按需分页实现,即页面仅在需要时才加载到 RAM 中。
When a process tries to access a page that is not in RAM, a page fault occurs. The operating system must then load the required page from disk into a free frame, updating the page table. If no frame is free, a replacement algorithm such as Least Recently Used, often called LRU, selects a victim page to swap out.
当进程尝试访问不在 RAM 中的页面时,就会发生缺页异常。操作系统随后必须将所需页面从磁盘加载到一个空闲页框中,并更新页表。如果没有空闲页框,替换算法(例如最近最少使用,通常称为 LRU)会选择一个牺牲页进行换出。
A high page fault rate leads to thrashing, where the system spends more time swapping pages than executing processes, causing severe performance degradation. Exam questions often ask why increasing RAM reduces page faults and improves performance.
高缺页率会导致系统抖动,即系统花费在换页上的时间多于执行进程的时间,从而造成严重的性能下降。考题经常问为什么增加 RAM 会减少缺页并提高性能。
7. Interrupts and Context Switching | 中断与上下文切换
An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. Hardware interrupts come from devices like the keyboard, mouse, disk, or system timer. Software interrupts, often called traps, are generated by programs, for example to request a system call.
中断是由硬件或软件发出的信号,指示需要立即处理的事件。硬件中断来自键盘、鼠标、磁盘或系统定时器等设备。软件中断通常称为陷阱,由程序产生,例如请求系统调用。
When an interrupt occurs, the CPU saves the current state of the running process, including the program counter and registers, into its Process Control Block. The CPU then executes an interrupt service routine, often called ISR, from the interrupt vector table. After handling the interrupt, the scheduler may restore the previous process or choose a different ready process.
当中断发生时,CPU 将正在运行进程的当前状态(包括程序计数器和寄存器)保存到其进程控制块中。然后 CPU 从中断向量表执行中断服务例程,通常称为 ISR。处理完中断后,调度程序可能恢复之前的进程,或选择另一个就绪进程。
Context switching is the procedure of saving the state of the currently running process and loading the saved state of another process. This allows multitasking but introduces overhead because the CPU does no useful work while switching. A shorter time slice in Round Robin increases responsiveness but also increases context switching overhead.
上下文切换是保存当前运行进程的状态并加载另一个进程已保存状态的过程。这实现了多任务处理,但引入了开销,因为 CPU 在切换期间不做有用工作。轮转调度中较短的时间片提高了响应性,但也增加了上下文切换开销。
8. Concurrency and Deadlock | 并发与死锁
Concurrency means multiple processes make progress at the same time, either on separate CPU cores or by rapidly interleaving on a single core. Concurrent access to shared resources, such as files, memory, or devices, can lead to race conditions where the outcome depends on the exact order of execution.
并发意味着多个进程同时取得进展,可以是在不同的 CPU 核心上,也可以在单个核心上快速交错执行。对共享资源(如文件、内存或设备)的并发访问可能导致竞态条件,即结果取决于执行的精确顺序。
A deadlock is a situation where a group of processes are each waiting for resources held by another process in the group, so none can proceed. Four necessary conditions for deadlock are: Mutual exclusion, Hold and wait, No pre-emption, and Circular wait. Breaking any one of these can prevent deadlock.
死锁是指一组进程各自等待组内另一个进程持有的资源,因此没有一个进程能继续执行的情况。死锁的四个必要条件为:互斥、持有并等待、不可抢占和循环等待。破坏其中任何一个条件都可以防止死锁。
For example, if Process A holds a printer and requests a scanner, while Process B holds the scanner and requests the printer, both may deadlock. Resource allocation graphs with a cycle indicate deadlock if each resource has only one instance.
例如,如果进程 A 持有打印机并请求扫描仪,而进程 B 持有扫描仪并请求打印机,则两者可能发生死锁。资源分配图中如果每个资源只有一个实例,则存在环路即表明死锁。
9. Resource Allocation and Starvation | 资源分配与饥饿
Starvation occurs when a process is repeatedly denied access to a resource even though the resource is available, because other processes are always given higher priority or because the scheduling algorithm favours other types of processes. Unlike deadlock, the starving process is not waiting for resources held by others; it is simply being overtaken indefinitely.
饥饿是指进程反复被拒绝访问某个资源,即使该资源可用,因为其他进程总是被赋予更高优先级,或者调度算法偏向其他类型的进程。与死锁不同,饥饿中的进程并不是等待他人持有的资源,而是被无限期地超越。
A common solution to starvation is ageing, where the priority of a waiting process gradually increases over time. This ensures that even the lowest-priority process eventually becomes the highest-priority process and gets CPU time. Another solution is a fair scheduling algorithm such as Round Robin.
解决饥饿的常见方法是老化,即等待进程的优先级随时间逐渐提高。这确保即使是优先级最低的进程最终也会成为最高优先级进程并获得 CPU 时间。另一种解决方案是使用公平的调度算法,如轮转调度。
In memory allocation, starvation may occur if a process waiting for a large contiguous block of memory is continually bypassed by smaller requests that fit into available fragments. Paging and segmentation with virtual memory reduce this problem because physical memory allocation is non-contiguous.
在内存分配中,如果等待大块连续内存的进程不断被适合现有碎片的小请求绕过,就可能发生饥饿。使用虚拟内存的分页和分段可以减少这个问题,因为物理内存分配是非连续的。
10. Exam-Style Application and Common Errors | 考点应用与常见错误
In Edexcel programming exams, questions often combine process states, scheduling, and memory management. For example, you might be given a table of process arrival times and CPU bursts, and asked to calculate average waiting time using FCFS and Round Robin with a given quantum. Always show your working clearly and label each process’s start and end times.
在 Edexcel 编程考试中,问题通常会结合进程状态、调度和内存管理。例如,可能给出一张进程到达时间和 CPU 突发时间的表格,要求使用 FCFS 和给定时间片的轮转调度计算平均等待时间。务必清晰展示计算步骤,并标注每个进程的开始和结束时间。
A common mistake is confusing waiting time with turnaround time. Waiting time is only the time spent in the ready queue, whereas turnaround time includes execution time and waiting time. Another common error is forgetting that Round Robin pre-empts a process exactly when the quantum expires, even if the process needs only a tiny extra amount of CPU time.
常见错误是将等待时间与周转时间混淆。等待时间仅是在就绪队列中花费的时间,而周转时间包括执行时间和等待时间。另一个常见错误是忘记轮转调度在时间片恰好用完时抢占进程,即使该进程只需要再多一点点 CPU 时间。
When explaining deadlock in a written answer, do not simply say ‘processes wait for each other’. You must refer to the four conditions and explain that none of the processes can release its resource because each is blocked. Exam mark schemes reward precise terminology such as mutual exclusion, hold and wait, and circular wait.
在书面回答中解释死锁时,不要只说“进程互相等待”。必须提到四个条件,并解释每个进程都无法释放其资源,因为每个进程都被阻塞。考试评分方案奖励精确的术语,如互斥、持有并等待和循环等待。
For memory management questions, be careful to distinguish between pages and frames: pages are logical divisions of a process, while frames are physical divisions of RAM. A page table matches page numbers to frame numbers, and a page fault occurs when the required page is not present in memory.
对于内存管理问题,要小心区分页面和页框:页面是进程的逻辑划分,而页框是 RAM 的物理划分。页表将页号与页框号相匹配,当所需页面不在内存中时就会发生缺页异常。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply