📚 Operating Systems: Processes and Memory Management | 操作系统:进程与内存管理
An operating system (OS) is the fundamental software that manages computer hardware and provides a platform for applications. In this article, we explore two core responsibilities of an OS – process management and memory management – covering scheduling algorithms, process states, paging, segmentation and virtual memory.
操作系统(OS)是管理计算机硬件并为应用程序提供平台的基础软件。本文将探讨操作系统的两大核心职责——进程管理与内存管理,涵盖调度算法、进程状态、分页、分段以及虚拟内存。
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 use the hardware efficiently. It manages resources such as the processor, memory, storage and I/O devices. In a multi‑tasking environment, the OS must allocate these resources fairly and safely among competing processes.
操作系统充当用户与硬件之间的中介。其主要目标是执行用户程序、让计算机系统便于使用以及高效地利用硬件。它管理处理器、内存、存储和 I/O 设备等资源。在多任务环境中,操作系统必须在竞争进程之间公平且安全地分配这些资源。
Key functions include providing a user interface, executing programs, handling I/O operations, managing the file system, enabling communication between processes, and detecting and recovering from errors. Without an OS, each application would need to include full hardware‑control code, making systems enormous and incompatible.
关键功能包括提供用户界面、执行程序、处理 I/O 操作、管理文件系统、支持进程间通信以及错误检测与恢复。如果没有操作系统,每一个应用程序都需要包含完整的硬件控制代码,导致系统臃肿且不兼容。
2. Processes and Process Control Blocks | 进程与进程控制块
A process is a program in execution. It is more than just the program code (the text section); it also includes the current activity, as represented by the program counter, registers and variable contents. Each process has its own address space and is represented in the OS by a process control block (PCB).
进程是正在执行的程序。它不仅仅是程序代码(文本段),还包含由程序计数器、寄存器和变量内容所代表的当前活动。每个进程都有自己独立的地址空间,并在操作系统中用一个进程控制块(PCB)来表示。
The PCB contains all the information the OS needs to manage the process: process ID, state, program counter, CPU registers, memory limits, list of open files, and scheduling priority. When the OS performs a context switch, it saves the current PCB and loads the PCB of the next process to be executed.
PCB 包含了操作系统管理进程所需的全部信息:进程标识符、状态、程序计数器、CPU 寄存器、内存界限、打开文件列表以及调度优先级。当操作系统执行上下文切换时,它会保存当前 PCB 并加载下一个要执行进程的 PCB。
3. Process States | 进程状态
As a process executes, it changes state. The typical five states in a multi‑programming system are: new, ready, running, waiting (blocked) and terminated. The process arrives in the ‘new’ state and is admitted into the ‘ready’ queue where it waits for CPU time.
随着进程的执行,它的状态会发生变化。多道程序系统中典型的五个状态是:新建、就绪、运行、等待(阻塞)和终止。进程以“新建”状态到来,被接纳后进入“就绪”队列等待 CPU 时间。
The scheduler dispatches a ready process onto the CPU, moving it to the ‘running’ state. If the process needs to wait for an event (e.g. I/O completion), it moves to the ‘waiting’ state. Once the event occurs, it returns to the ‘ready’ state. When it finishes execution, it transitions to the ‘terminated’ state, where the OS reclaims its resources.
调度程序将一个就绪进程分派到 CPU,使其进入“运行”状态。如果进程需要等待某个事件(例如 I/O 完成),则进入“等待”状态;事件发生后,它返回“就绪”状态。当进程执行结束时,转换到“终止”状态,操作系统回收其资源。
4. Scheduling Algorithms | 调度算法
Process scheduling decides which ready process gets the CPU next. The goal is to maximise CPU utilisation, throughput, and minimise turnaround time, waiting time and response time. Common algorithms include:
进程调度决定哪一个就绪进程接下来获得 CPU。目标是最大限度地提高 CPU 利用率和吞吐量,并最小化周转时间、等待时间和响应时间。常见算法包括:
- First‑Come, First‑Served (FCFS): the simplest, non‑preemptive policy where the first process to request the CPU is allocated the CPU first. It can lead to the convoy effect.
- 先来先服务(FCFS): 最简单的非抢占策略,最先请求 CPU 的进程最先获得 CPU;可能导致车队效应。
- Shortest Job First (SJF): non‑preemptive (or preemptive SRTF) algorithm that selects the process with the smallest next CPU burst. Optimal for minimising average waiting time but requires knowledge of future burst lengths.
- 最短作业优先(SJF): 非抢占(或抢占式 SRTF)算法,选择下一次 CPU 爆发最短的进程;可最小化平均等待时间,但需要知道将来的爆发长度。
- Round Robin (RR): preemptive algorithm that gives each process a small time quantum. If the process does not finish within the quantum, it is returned to the ready queue. Good for interactive systems.
- 轮转调度(RR): 抢占式算法,为每个进程分配一个较小时限。若进程在时限内未完成,则返回就绪队列;适用于交互式系统。
- Priority Scheduling: each process has a priority; the CPU is allocated to the highest‑priority process. Preemptive or non‑preemptive. Can cause starvation of low‑priority processes, solved by ageing.
- 优先级调度: 每个进程具有优先级;CPU 分配给最高优先级的进程。可为抢占或非抢占。可能导致低优先级进程饥饿,通过老化技术解决。
5. Memory Management Overview | 内存管理概述
Memory is a large array of words or bytes, each with its own address. The OS must keep track of which parts of memory are in use and which are free, allocate memory to processes when needed, and deallocate it when they terminate. Effective memory management is vital for system performance and security.
内存是由字或字节组成的大型数组,每个单元都有自己的地址。操作系统必须跟踪哪些内存区域正在使用、哪些是空闲的,在需要时为进程分配内存,并在进程终止时回收。有效的内存管理对系统性能和安全至关重要。
In a multiprogramming environment, several processes reside in memory simultaneously. The OS must ensure that one process does not access another’s memory space, preventing security breaches and accidental data corruption. Memory protection uses base and limit registers or hardware address translation.
在多道程序环境中,多个进程同时驻留在内存中。操作系统必须确保一个进程不能访问另一个进程的内存空间,防止安全漏洞和意外数据损坏。内存保护通过基址寄存器和界限寄存器或硬件地址转换来实现。
6. Paging | 分页
Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. Physical memory is divided into fixed‑size blocks called frames, and logical memory is divided into blocks of the same size called pages. Each page of a process can be loaded into any available frame.
分页是一种内存管理方案,消除了物理内存连续分配的需要。物理内存被划分为大小固定的块,称为帧;逻辑内存被划分为同样大小的块,称为页。进程的每一页可以加载到任意可用的帧中。
A page table maps logical page numbers to physical frame numbers. Every memory access generated by the CPU is split into a page number and an offset. The page number is used as an index into the page table, retrieving the corresponding frame number. The physical address is then formed by concatenating the frame number with the offset.
页表将逻辑页号映射到物理帧号。CPU 产生的每一次内存访问都被分为页号和偏移量。页号用作页表的索引,取出相应的帧号;然后将帧号与偏移量拼接,构成物理地址。
Paging eliminates external fragmentation but may cause internal fragmentation if the process does not use an exact multiple of the page size. It also requires hardware support for fast address translation through a Translation Look‑aside Buffer (TLB).
分页消除了外部碎片,但如果进程没有恰好用完页大小的整数倍,则可能产生内部碎片。它还需要硬件支持通过转译后备缓冲器(TLB)实现快速地址转换。
7. Segmentation | 分段
Segmentation is a memory‑management scheme that supports a programmer’s view of memory. A program is seen as a collection of segments – for example, code, data, stack and heap – each of variable length. Each segment has a name and a length, and a logical address consists of a segment number and an offset.
分段是一种支持程序员视角的内存管理方案。程序被视为一组段——例如代码段、数据段、栈和堆——每个段长度可变。每个段都有名称和长度,逻辑地址由段号和偏移量组成。
A segment table maps segment numbers to physical base addresses and limits. The offset is checked against the limit to prevent illegal accesses. Segmentation allows sharing and protection at the segment level, and it avoids internal fragmentation, though it can suffer from external fragmentation.
段表将段号映射到物理基地址和界限。偏移量会与界限进行比较,以防止非法访问。分段允许在段级别上共享和保护,避免了内部碎片,但可能会出现外部碎片。
Many modern systems combine paging and segmentation into a paged‑segmentation scheme, where segments themselves are paged, yielding the benefits of both approaches.
许多现代系统将分页和分段结合成一种段页式方案,其中段本身再被分页,从而兼有两者的优点。
8. Virtual Memory | 虚拟内存
Virtual memory is a technique that allows the execution of processes that may not be completely in main memory. It abstracts physical memory into an array of storage locations known as the logical address space. The logical address space can be much larger than the physical memory, enabling large programs to run even on machines with limited RAM.
虚拟内存是一种允许执行不一定完全在主存中的进程的技术。它将物理内存抽象为称为逻辑地址空间的存储位置阵列。逻辑地址空间可以远大于物理内存,使得大型程序甚至能在 RAM 有限的机器上运行。
The OS keeps only the active parts of a process in memory; the rest resides on disk. When a process references a page that is not in memory (a page fault), the OS loads the required page from disk, possibly evicting an existing page. Demand paging and page replacement algorithms such as FIFO, LRU and Optimal are used to manage this process.
操作系统仅将进程的活动部分保持在内存中,其余部分驻留在磁盘上。当进程引用不在内存中的页(缺页故障)时,操作系统从磁盘加载所需的页,并可能换出现有页。请求分页和页面置换算法(如 FIFO、LRU 和最优算法)被用来管理此过程。
Virtual memory allows more concurrent processes, enhances CPU utilisation, and simplifies memory management. However, excessive page faults lead to thrashing, a situation where the system spends more time paging than executing.
虚拟内存允许更多的并发进程,提高了 CPU 利用率并简化了内存管理。但过多的缺页会导致抖动,即系统花费在页面交换上的时间多于执行时间。
9. Interrupts and Exception Handling | 中断与异常处理
Interrupts are signals from hardware or software that cause the CPU to temporarily stop its current execution and switch to the OS’s interrupt handler. They are fundamental to preemptive multitasking and I/O management. An interrupt can be generated by a device (e.g., a keyboard press or disk completion) or by a program (trap, exception).
中断是由硬件或软件发出的信号,使 CPU 暂时停止当前执行并切换到操作系统的中断处理程序。它们是抢占式多任务和 I/O 管理的基础。中断可以由设备(如键盘按下或磁盘完成)产生,也可以由程序(陷阱、异常)产生。
When an interrupt occurs, the processor saves the current state (program counter, registers) on the stack, determines the cause via the interrupt vector, and runs the corresponding service routine. After handling, the original context is restored and execution resumes. This mechanism allows the OS to regain control and enforce time quanta.
中断发生时,处理器将当前状态(程序计数器、寄存器)保存在栈上,通过中断向量确定原因,并运行相应的服务例程。处理完毕后,恢复原始上下文并继续执行。此机制使操作系统可以重新获得控制权并强制执行时间片。
10. User Mode and Kernel Mode | 用户模式与内核模式
Modern processors provide dual‑mode operation to protect the OS from user programs. A mode bit indicates whether the processor is in user mode (1) or kernel mode (0). Certain privileged instructions – such as those that control I/O, modify page tables, or halt the processor – can only be executed in kernel mode.
现代处理器提供双模式运行以保护操作系统免受用户程序的损害。模式位指示处理器是处于用户模式(1)还是内核模式(0)。某些特权指令——例如控制 I/O、修改页表或停机——只能在核心模式下执行。
When a user program requests an OS service (e.g., reading a file), it executes a system call that generates a trap, switching the processor to kernel mode. The kernel validates the request, performs the operation, and returns control to the user program in user mode. This separation ensures system integrity and security.
当用户程序请求操作系统服务(例如读取文件)时,它会执行一个系统调用,产生陷阱,将处理器切换到核心模式。内核验证请求、执行操作,并在用户模式下将控制权返回给用户程序。这种分离保证了系统的完整性和安全性。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导