📚 Operating Systems: Processes and CPU Scheduling | 操作系统:进程与CPU调度
An operating system (OS) is the fundamental software that manages hardware and software resources, providing a stable environment for applications to run. For A-Level Computer Science, understanding how the OS handles processes and schedules CPU time is essential — it underpins everything from multitasking to responsiveness in real-time systems. This article explores key concepts: process states, the Process Control Block, context switching, and a range of scheduling algorithms such as FCFS, SJF, Round Robin, and priority-based methods.
操作系统(OS)是管理硬件和软件资源的基础软件,为应用程序提供稳定的运行环境。对于A-Level计算机科学而言,理解操作系统如何处理进程以及如何调度CPU时间至关重要——这构成了从多任务处理到实时系统响应能力的一切基础。本文将探讨核心概念:进程状态、进程控制块、上下文切换,以及一系列调度算法,例如先来先服务、短作业优先、轮转调度和基于优先级的方法。
1. Introduction to Operating Systems | 操作系统简介
An operating system acts as an intermediary between the user and the computer hardware. It hides the complexity of hardware by providing a set of services and a user interface. Key examples include Windows, Linux, macOS, and real-time operating systems (RTOS) used in embedded devices. Without an OS, every application would need to directly control the hardware, leading to chaos and massive duplication of effort.
操作系统充当用户与计算机硬件之间的中介。它通过提供一组服务和用户界面来隐藏硬件的复杂性。典型的例子包括Windows、Linux、macOS,以及用于嵌入式设备的实时操作系统(RTOS)。如果没有操作系统,每个应用程序都必须直接控制硬件,这会导致混乱和大量的重复工作。
2. Functions of an Operating System | 操作系统的功能
The OS performs several critical functions: process management, memory management, file system management, I/O device management, security and access control, and networking. In this article we focus on process management — how the OS creates, schedules, and terminates processes, and how it allocates the CPU among them using various scheduling algorithms.
操作系统执行若干关键功能:进程管理、内存管理、文件系统管理、I/O设备管理、安全与访问控制以及网络功能。在本文中,我们着重讨论进程管理——操作系统如何创建、调度和终止进程,以及如何运用各种调度算法在它们之间分配CPU时间。
3. What is a Process? | 什么是进程?
A process is a program in execution. While a program is a passive set of instructions stored on disk, a process is an active entity with its own memory space, program counter, registers, and execution context. Modern operating systems are multiprogramming, meaning several processes can reside in memory simultaneously, competing for the CPU. The OS must ensure fair, efficient, and safe sharing of the processor.
进程是正在运行的程序。程序是存储在磁盘上的一组被动指令,而进程是一个活跃的实体,拥有自己的内存空间、程序计数器、寄存器和执行上下文。现代操作系统都是多道程序设计的,这意味着多个进程可以同时驻留在内存中,竞争CPU资源。操作系统必须确保处理器的共享是公平、高效且安全的。
4. Process States | 进程状态
During its lifetime, a process moves through several discrete states. The classic five-state model includes: New (process being created), Ready (waiting to be assigned to the CPU), Running (instructions are being executed), Blocked (or Waiting, waiting for an event such as I/O completion), and Terminated (finished execution). The transitions between states are triggered by events like interrupts or I/O requests.
在其生命周期中,进程会经历几个离散的状态。经典的五状态模型包括:新建(进程正在创建)、就绪(等待被分配CPU)、运行(正在执行指令)、阻塞(或等待,例如等待I/O完成)和终止(执行完毕)。状态之间的转换由中断或I/O请求等事件触发。
5. Process Control Block (PCB) | 进程控制块
To manage a process, the OS maintains a data structure called the Process Control Block (PCB). The PCB contains all information needed to track and resume the process: process ID (PID), program counter (PC), CPU registers, memory limits, list of open files, and the process state. When a context switch occurs, the OS saves the current PCB and loads the PCB of the next process, allowing seamless multitasking.
为了管理进程,操作系统维护一个称为进程控制块(PCB)的数据结构。PCB包含了追踪和恢复进程所需的所有信息:进程ID(PID)、程序计数器(PC)、CPU寄存器、内存界限、打开文件列表以及进程状态。当发生上下文切换时,操作系统保存当前PCB并加载下一个进程的PCB,从而实现无缝的多任务处理。
6. Introduction to CPU Scheduling | CPU调度简介
CPU scheduling determines which process in the ready queue gets the CPU next. The scheduler aims to maximise CPU utilisation and throughput, minimise turnaround time, waiting time, and response time. Scheduling algorithms can be non-preemptive (once a process gets the CPU, it keeps it until it voluntarily releases it) or preemptive (the OS can force a process off the CPU, typically via a timer interrupt).
CPU调度决定就绪队列中哪个进程下一个获得CPU。调度程序的目标是最大化CPU利用率和吞吐量,最小化周转时间、等待时间和响应时间。调度算法可以是非抢占式的(一旦进程获得CPU,它将一直保持直到自愿释放)或抢占式的(操作系统可以强制进程离开CPU,通常是通过定时器中断)。
7. First-Come, First-Served (FCFS) | 先来先服务
FCFS is the simplest scheduling algorithm: processes are executed in the order they arrive. Implementation is straightforward using a FIFO queue. However, FCFS suffers from the ‘convoy effect’ — a long CPU-bound process can hold up a queue of short I/O-bound processes, leading to poor average waiting time. It is non-preemptive and typically not used as a stand-alone scheduler in modern interactive systems.
FCFS是最简单的调度算法:进程按照到达的顺序执行。使用FIFO队列实现起来非常直接。但是,FCFS存在“护航效应”的问题——一个长CPU密集型进程可能会阻塞一队短的I/O密集型进程,导致平均等待时间很差。它是非抢占式的,在现代交互式系统中通常不会作为独立调度器使用。
8. Shortest Job First (SJF) | 短作业优先
SJF selects the process with the smallest total expected CPU burst time. It can be non-preemptive or preemptive (Shortest Remaining Time First, SRTF). SJF is provably optimal in terms of minimising average waiting time for a given set of processes. The drawback is that it requires knowing in advance the length of the next CPU burst, which is rarely possible. Ageing techniques can be used to prevent long jobs from starving.
SJF选择具有最小预期CPU执行总时间的进程。它可以是非抢占式或抢占式的(最短剩余时间优先,SRTF)。可以证明,对于给定的一组进程,SJF在最小化平均等待时间方面是最优的。其缺点是需要提前知道下一次CPU执行的长度,而这几乎是不可能的。可以使用老化技术来防止长作业饥饿。
9. Round Robin (RR) | 轮转调度
Round Robin is a preemptive algorithm designed for time-sharing systems. Each process is given a small fixed unit of CPU time called a time quantum (typically 10–100 ms). If a process does not finish within its quantum, it is preempted and placed at the end of the ready queue. RR ensures fair CPU distribution and guarantees a low response time. Performance depends heavily on the size of the quantum: too small causes excessive context switches, too large degenerates to FCFS.
轮转调度是一种为分时系统设计的抢占式算法。每个进程被分配一个固定的CPU时间片,称为时间量子(通常为10–100毫秒)。如果进程在其量子内未完成,它会被抢占并放回就绪队列末尾。RR确保了CPU分配的公平性,并保证了较低的响应时间。其性能严重依赖于量子的大小:太小会导致过多的上下文切换,太大则会退化为FCFS。
10. Priority Scheduling | 优先级调度
Priority scheduling associates a priority value (integer) with each process. The CPU is allocated to the highest-priority ready process. This can be preemptive or non-preemptive. A major problem is starvation, where low-priority processes may never execute. This is often solved by ‘ageing’, i.e. gradually increasing the priority of a waiting process. Real-world systems often combine priority with other algorithms, e.g. a preemptive priority system where same-priority processes are scheduled RR.
优先级调度为每个进程关联一个优先级值(整数)。CPU分配给具有最高优先级的就绪进程。这可以是抢占式或非抢占式的。一个主要问题是饥饿,即低优先级的进程可能永远无法执行。这通常通过“老化”来解决,即逐渐提高等待进程的优先级。实际系统常常将优先级与其他算法相结合,例如在一个抢占式优先级系统中,相同优先级的进程按RR进行调度。
11. Multilevel Queue Scheduling | 多级队列调度
In multilevel queue scheduling, the ready queue is partitioned into several separate queues, each with its own scheduling algorithm. Processes are permanently assigned to a queue based on properties like memory size, priority, or process type (foreground interactive vs background batch). For example, a foreground queue might use RR for good interactivity, while a background queue might use FCFS. Scheduling among queues is usually done via fixed-priority preemptive or time-sliced allocation.
在多级队列调度中,就绪队列被划分为几个独立的队列,每个队列有自己的调度算法。进程根据内存大小、优先级或进程类型(前台交互式与后台批处理)等属性被永久分配到一个队列。例如,前台队列可能使用RR以获得良好的交互性,而后台队列则使用FCFS。队列之间的调度通常通过固定优先级抢占或时间片分配来完成。
12. Scheduling in Real-Time Systems | 实时系统调度
Real-time systems (RTS) must guarantee that critical tasks complete within strict time constraints. Scheduling algorithms such as Rate Monotonic (RM) and Earliest Deadline First (EDF) are used. In RM, processes with shorter periods are given higher priority (static priority). EDF is a dynamic preemptive scheme where the process closest to its deadline gets the CPU. These algorithms prioritise predictability over fairness, and they require careful analysis of task execution times.
实时系统必须保证关键任务在严格的时间限制内完成。常用的调度算法包括单调速率调度(RM)和最早截止时间优先(EDF)。RM中,周期越短的进程优先级越高(静态优先级)。EDF是一种动态抢占式方案,最接近其截止时间的进程获得CPU。这些算法将可预测性置于公平性之上,并且需要对任务执行时间进行仔细分析。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply