Chapter 3 Memory Management A

内存管理(Memory Management)


内存管理的目标与任务

以下是对内存管理目标与任务的详细讲解,包括内存层次结构、程序加载、内存管理的目标以及具体任务。


1. 理想的内存特性

程序员对内存的期望:
What Programmers Want from Memory:
大容量(Large):
◦ 内存容量越大,程序可以处理的数据和任务越多。
The larger the memory, the more data and tasks a program can handle.
高速度(Fast):
◦ 快速的内存可以提高程序的执行效率。
Fast memory improves program execution efficiency.
非易失性(Nonvolatile):
◦ 非易失性内存可以在断电后保留数据,避免数据丢失。
Nonvolatile memory retains data after power loss, preventing data loss.
低成本(Inexpensive):
◦ 低成本的内存可以降低系统整体成本。
Inexpensive memory reduces the overall cost of the system.

现实中的内存特性:
Reality of Memory Characteristics:
• 内存的速度、容量和成本之间存在权衡关系。
There is a trade-off among speed, capacity, and cost in memory.


2. 内存层次结构(Memory Hierarchy)

内存层次结构的组成:
Components of the Memory Hierarchy:
1. 高速缓存(Cache Memory):
特点: 少量、快速、昂贵、易失性。
Characteristics: Small, fast, expensive, volatile.
作用: 提供最快的访问速度,存储 CPU 最近或最常用的数据。
Purpose: Provides the fastest access speed, storing the most recently or frequently used data by the CPU.
2. 主内存(Main Memory):
特点: 中等容量、中等速度、中等价格、易失性。
Characteristics: Medium capacity, medium speed, moderate price, volatile.
作用: 存储当前正在运行的程序和数据。
Purpose: Stores the programs and data currently being executed.
3. 磁盘存储(Disk Storage):
特点: 大容量、慢速、便宜、非易失性。
Characteristics: Large capacity, slow speed, inexpensive, nonvolatile.
作用: 提供长期存储,存储程序和数据的备份。
Purpose: Provides long-term storage, storing backups of programs and data.

内存层次结构的特点:
Characteristics of the Memory Hierarchy:
速度与成本的权衡:
◦ 越靠近 CPU 的存储器速度越快,但容量越小、成本越高。
Trade-off between speed and cost: The closer the memory is to the CPU, the faster it is, but the smaller its capacity and the higher its cost.
存储器的互补性:
◦ 不同层次的存储器共同协作,提供高效的内存管理。
Complementary nature of storage: Different levels of memory work together to provide efficient memory management.


3. 内存管理器的职责(Memory Manager's Role)

内存管理器的作用:
Role of the Memory Manager:
• 管理内存层次结构,协调不同层次的存储器之间的数据交换。
Manages the memory hierarchy, coordinating data exchange between different levels of storage.
• 确保程序能够高效地访问内存,同时满足系统的性能和安全性需求。
Ensures that programs can access memory efficiently while meeting system performance and security requirements.


4. 存储层次结构(Storage Hierarchy)

存储层次结构的组成:
Components of the Storage Hierarchy:
1. 寄存器(Registers):
◦ CPU 内部的高速存储单元,速度最快,容量最小。
High-speed storage units inside the CPU, fastest but smallest in capacity.
2. 高速缓存(Cache):
◦ 位于 CPU 和主内存之间,提供快速的数据访问。
Located between the CPU and main memory, providing fast data access.
3. 主内存(Main Memory):
◦ 存储当前正在运行的程序和数据。
Stores the programs and data currently being executed.
4. 磁盘存储(Disk Storage):
◦ 提供长期存储,容量大但速度较慢。
Provides long-term storage, large capacity but slower speed.
5. 磁带存储(Tape Storage):
◦ 用于归档和备份,容量最大但速度最慢。
Used for archiving and backup, largest capacity but slowest speed.


5. 程序加载与内存管理

程序加载的基本要求:
Basic Requirements for Program Loading:
1. 程序必须加载到内存中:
◦ 程序需要被加载到内存中,并分配到一个进程的地址空间中才能运行。
Programs must be loaded into memory and allocated to a process's address space to run.
2. 连续的地址空间:
◦ 程序的代码和数据通常存储在连续的地址空间中。
Program code and data are typically stored in contiguous address spaces.
3. 系统段与用户段:
◦ 内存分为系统段(操作系统代码和数据)和用户段(用户程序和数据)。
Memory is divided into system segments (operating system code and data) and user segments (user program and data).


6. 内存管理的目标(Objectives of Memory Management)

支持多道程序设计(Support Multiprogramming):
• 允许多个程序同时驻留在内存中,提高系统的资源利用率。
Allows multiple programs to reside in memory simultaneously, improving resource utilization.

方便用户(Convenience to User):
• 隐藏硬件细节,自动加载用户程序,简化用户的操作。
Hides hardware details, automatically loads user programs, and simplifies user operations.

解决程序空间大于内存空间的问题(Solve Program Space > Memory Space):
• 通过虚拟内存等技术,使程序可以使用比物理内存更大的地址空间。
Solves the problem where program space exceeds memory space using techniques like virtual memory.

进程在内存中的灵活性(Process Flexibility in Memory):
• 支持进程的动态加载、运行和卸载,允许进程在内存中灵活移动。
Supports dynamic loading, execution, and unloading of processes, allowing flexible movement of processes in memory.

快速访问(Quick Access):
• 确保程序能够快速访问所需的内存资源,减少延迟。
Ensures quick access to required memory resources, reducing latency.

存储保护与安全性(Storage Protection and Security):
• 防止进程访问其他进程的内存,保护系统的安全性和稳定性。
Prevents processes from accessing each other's memory, protecting system security and stability.

共享与通信(Share and Communication):
• 支持进程间的内存共享和通信,提高系统的协作能力。
Supports memory sharing and communication between processes, improving system collaboration.

性能与成本的平衡(Performance and Cost):
• 在性能和成本之间取得平衡,优化内存的使用效率。
Balances performance and cost, optimizing memory usage efficiency.


7. 内存管理的任务(Tasks of Memory Management)

内存分配与释放(Memory Allocation and Freeing):
任务描述:
◦ 在程序运行时分配所需的内存空间,并在程序结束时释放内存。
Task Description: Allocates the required memory space during program execution and frees memory when the program ends.
挑战:
◦ 如何高效地分配和释放内存,避免内存碎片。
Challenges: How to allocate and free memory efficiently, avoiding memory fragmentation.

地址重定位(Address Relocation):
任务描述:
◦ 将程序的逻辑地址转换为物理地址,确保程序能够正确访问内存。
Task Description: Converts the program's logical addresses into physical addresses, ensuring correct memory access.
实现方式:
◦ 静态重定位:在程序加载时完成地址转换。
Static Relocation: Completes address conversion during program loading.
◦ 动态重定位:在程序运行时动态完成地址转换。
Dynamic Relocation: Dynamically completes address conversion during program execution.

内存共享与保护(Memory Sharing and Protection):
任务描述:
◦ 确保多个进程可以安全地共享内存,同时防止非法访问。
Task Description: Ensures that multiple processes can safely share memory while preventing illegal access.
实现方式:
◦ 使用页表和段表管理内存访问权限。
Uses page tables and segment tables to manage memory access permissions.

内存扩展(Memory Expansion):
任务描述:
◦ 通过虚拟内存技术扩展内存容量,使程序可以使用比物理内存更大的地址空间。
Task Description: Expands memory capacity through virtual memory technology, allowing programs to use a larger address space than physical memory.
实现方式:
◦ 使用磁盘作为扩展内存,按需加载数据。
Uses disk as extended memory, loading data on demand.


8. 内存管理的挑战与优化

内存碎片(Memory Fragmentation):
问题:
◦ 频繁的内存分配和释放可能导致内存碎片,降低内存利用率。
Problem: Frequent memory allocation and release can lead to memory fragmentation, reducing memory utilization.
解决方法:
◦ 使用内存池化技术或内存压缩技术减少碎片。
Solutions: Use memory pooling or memory compression techniques to reduce fragmentation.

虚拟内存的性能开销(Performance Overhead of Virtual Memory):
问题:
◦ 地址映射和页表管理可能带来额外的性能开销。
Problem: Address mapping and page table management may introduce additional performance overhead.
解决方法:
◦ 使用硬件加速(如 TLB)和优化页面置换算法。
Solutions: Use hardware acceleration (e.g., TLB) and optimize page replacement algorithms.

内存保护与共享的平衡(Balancing Protection and Sharing):
问题:
◦ 过于严格的内存保护可能限制进程间的通信和共享。
Problem: Overly strict memory protection may limit inter-process communication and sharing.
解决方法:
◦ 使用共享内存段和访问控制机制,在保护的同时支持共享。
Solutions: Use shared memory segments and access control mechanisms to support sharing while ensuring protection.


9. 总结

内存管理的目标:
• 支持多道程序设计,提高资源利用率。
Support multiprogramming to improve resource utilization.
• 提供方便的用户接口,隐藏硬件细节。
Provide a user-friendly interface, hiding hardware details.
• 解决程序空间大于内存空间的问题,支持虚拟内存。
Solve the problem of program space exceeding memory space, supporting virtual memory.
• 确保内存的安全性、灵活性和高效性。
Ensure memory security, flexibility, and efficiency.

内存管理的任务:
• 内存分配与释放、地址重定位、内存共享与保护、内存扩展。
Memory allocation and freeing, address relocation, memory sharing and protection, memory expansion.

内存管理的挑战:
• 内存碎片、虚拟内存性能开销、内存保护与共享的平衡。
Memory fragmentation, performance overhead of virtual memory, balancing memory protection and sharing.

优化方向:
• 使用硬件加速、优化页面置换算法、改进内存分配策略。
Optimization directions: Use hardware acceleration, optimize page replacement algorithms, improve memory allocation strategies.

内存管理的演进:从无内存抽象到多程序并发运行

以下是对内存管理的进一步讲解,涵盖无内存抽象、多程序并发运行的挑战与解决方案,以及早期硬件支持的保护机制和重定位技术。


7. 无内存抽象(No Memory Abstraction)

定义:
Definition:
• 在无内存抽象的情况下,程序直接访问物理内存,操作系统不提供任何内存隔离或管理机制。
In the absence of memory abstraction, programs directly access physical memory, and the operating system provides no memory isolation or management mechanisms.

特点:
Characteristics:
1. 简单性:
◦ 程序直接操作物理内存,操作系统无需复杂的内存管理逻辑。
Simplicity: Programs directly manipulate physical memory, and the operating system does not require complex memory management logic.
2. 局限性:
无法运行多个程序:
◦ 多个程序无法同时驻留在内存中,因为它们会相互干扰。
Cannot run multiple programs: Multiple programs cannot reside in memory simultaneously because they interfere with each other.
安全性差:
◦ 程序可以直接访问其他程序的内存,可能导致数据破坏或系统崩溃。
Poor security: Programs can directly access the memory of other programs, potentially causing data corruption or system crashes.

内存组织方式:
Memory Organization:
单程序模式:
◦ 内存完全分配给一个程序,操作系统不驻留在内存中。
Single Program Mode: Memory is fully allocated to one program, and the operating system is not resident in memory.
操作系统与程序共存:
◦ 操作系统驻留在内存的一部分,程序运行在另一部分。
Operating System and Program Coexistence: The operating system resides in one part of memory, while the program runs in another part.


8. 多程序并发运行(Multiple Programs in Memory)

目标:
Goals:
• 允许多个程序同时驻留在内存中运行,而不会相互干扰。
Allow multiple programs to reside in memory and run simultaneously without interfering with each other.

需要解决的两个问题:
Two Problems to Solve:
1. 保护(Protection):
◦ 如何防止进程之间相互干扰?
How does the system prevent processes from interfering with each other?
2. 重定位(Relocation):
◦ 进程可能不会被加载到主内存的相同区域,如何让任务或进程在主内存的不同位置运行?
How does a task or process run in different locations in main memory when it may not be loaded into the same region?


9. 无内存抽象下的多程序运行(Multiple Programs without Memory Abstraction)

早期解决方案:
Early Solutions:
• 通过引入特殊硬件,可以实现多个程序的并发运行。
With the addition of some special hardware, it is possible to run multiple programs concurrently.

早期 IBM 360 的保护机制:
Protection Mechanism of Early IBM 360:
1. 内存分块:
◦ 将内存划分为 2 KB 的块。
Divide memory into 2 KB blocks.
2. 保护键(Protection Key):
◦ 每个块分配一个 4 位的保护键,存储在 CPU 的特殊寄存器中。
Assign each block a 4-bit protection key, stored in special registers of the CPU.
3. 进程保护键:
◦ 每个进程也有一个保护键值,存储在程序状态字(PSW)中。
Each process also has a protection key value associated with it, stored in the Program Status Word (PSW).
4. 访问控制:
◦ 当进程访问内存时,比较内存块的保护键与 PSW 中的键值。
When a process accesses memory, compare the protection key of the memory block with the key in the PSW.

早期 IBM 360 的重定位问题:
Relocation Problem of Early IBM 360:
问题描述:
◦ 如果两个程序分别运行在内存中,可能会因为直接引用物理地址而导致错误。
Problem Description: If two programs run in memory, they may cause errors by directly referencing physical addresses.
示例:
◦ 程序 A 和程序 B 各占 16 KB 内存。
◦ 当程序 A 运行结束后,操作系统加载程序 B。
◦ 如果程序 A 和程序 B 的代码中引用了相同的物理地址(如 JMP 28),则会导致错误。
Example:
▪ Programs A and B each occupy 16 KB of memory.
▪ When program A finishes, the operating system loads program B.
▪ If both programs reference the same physical address (e.g., JMP 28), an error occurs.


10. 静态重定位(Static Relocation)

定义:
Definition:
• 在加载进程时,将程序中的逻辑地址静态地修改为物理地址。
Modify the logical addresses in a program statically to physical addresses when loading the process.

工作原理:
How It Works:
• 假设程序 A 被加载到内存的基地址为 0,程序 B 被加载到内存的基地址为 16,384(16 KB)。
◦ 如果程序 B 中的指令 JMP 28 需要跳转到地址 28 + 16,384 = 16,412,则在加载时将地址 28 修改为 16,412。
If program B contains an instruction JMP 28 that needs to jump to address 28 + 16,384 = 16,412, the address 28 is modified to 16,412 during loading.

优点:
Advantages:
1. 无需硬件支持:
◦ 静态重定位完全由软件完成,不需要额外的硬件机制。
No hardware support required: Static relocation is entirely software-based and does not require additional hardware mechanisms.

缺点:
Disadvantages:
1. 加载速度慢:
◦ 每次加载程序时都需要修改地址,增加了加载时间。
Slow loading: Addresses need to be modified during each program load, increasing loading time.
2. 无法动态移动:
◦ 一旦程序被加载到内存中,其代码或数据无法在不重新加载的情况下移动到其他内存位置。
Cannot be dynamically moved: Once a program is loaded into memory, its code or data cannot be moved to another memory location without reloading.
3. 地址区分复杂:
◦ 加载器需要区分地址和常量,增加了实现的复杂性。
Complex address distinction: The loader needs a way to distinguish between addresses and constants, increasing implementation complexity.
示例:
◦ 指令 MOVE REGISTER1, 28 中的 28 是常量还是地址?
Example:
▪ Is 28 in the instruction MOVE REGISTER1, 28 a constant or an address?


11. 动态重定位(Dynamic Relocation)

定义:
Definition:
• 在程序运行时,通过硬件机制动态地将逻辑地址转换为物理地址。
Dynamically convert logical addresses to physical addresses during program execution through hardware mechanisms.

实现方式:
Implementation:
1. 基址寄存器(Base Register):
◦ 每个进程有一个基址寄存器,存储进程在内存中的起始地址。
Each process has a base register that stores the starting address of the process in memory.
2. 界限寄存器(Limit Register):
◦ 每个进程有一个界限寄存器,存储进程在内存中的结束地址。
Each process has a limit register that stores the ending address of the process in memory.
3. 地址转换:
◦ 当进程访问内存时,逻辑地址加上基址寄存器的值,得到物理地址。
When a process accesses memory, the logical address is added to the value in the base register to obtain the physical address.
◦ 如果逻辑地址超出界限寄存器的范围,则触发错误。
If the logical address exceeds the range of the limit register, an error is triggered.

优点:
Advantages:
1. 支持动态加载:
◦ 程序可以在内存中动态移动,而无需重新加载。
Supports dynamic loading: Programs can be dynamically moved in memory without reloading.
2. 提高灵活性:
◦ 允许多个程序共享内存,提高资源利用率。
Improves flexibility: Allows multiple programs to share memory, improving resource utilization.

缺点:
Disadvantages:
1. 硬件依赖:
◦ 动态重定位需要基址寄存器和界限寄存器的支持,增加了硬件复杂性。
Hardware dependency: Dynamic relocation requires base and limit registers, increasing hardware complexity.
2. 性能开销:
◦ 每次访问内存时都需要进行地址转换,可能带来额外的性能开销。
Performance overhead: Address conversion is required for each memory access, potentially introducing additional performance overhead.


12. 内存管理的演进总结

无内存抽象:
• 简单但无法支持多程序运行,安全性差。
Simple but cannot support multi-programming and has poor security.

静态重定位:
• 解决了多程序运行的基本问题,但加载速度慢且无法动态移动程序。
Solves basic problems of multi-programming but has slow loading and cannot dynamically move programs.

动态重定位:
• 提供了更高的灵活性和性能,但需要硬件支持并带来一定的性能开销。
Provides higher flexibility and performance but requires hardware support and introduces some performance overhead.

现代内存管理:
• 结合虚拟内存、分页、分段等技术,进一步优化了内存利用率和系统性能。
Modern memory management combines virtual memory, paging, segmentation, etc., to further optimize memory utilization and system performance.


地址空间(Address Spaces)

以下是对地址空间的简要讲解,包括其定义、作用、地址绑定、动态重定位以及硬件支持的实现。


13. 地址空间的背景与需求

内存的多道程序设计需求:
Memory Needs for Multiprogramming:
1. 保护(Protection):
◦ 系统需要防止进程之间相互干扰,确保每个进程只能访问自己的内存区域。
The system needs to prevent processes from interfering with each other, ensuring that each process can only access its own memory area.
2. 重定位(Relocation):
◦ 进程可能需要加载到主内存的不同位置,系统需要支持进程在不同内存位置运行。
Processes may need to be loaded into different locations in main memory, and the system needs to support running processes in different memory locations.

更好的解决方案:地址空间(Address Space):
A Better Solution: Address Space:
• 地址空间提供了一种逻辑上的内存管理方式,解决了保护和重定位的问题。
Address space provides a logical way to manage memory, solving the problems of protection and relocation.


14. 地址空间的定义与特点

地址空间的定义:
Definition of Address Space:
• 地址空间是进程可以用来访问内存的一组地址。
An address space is a set of addresses that a process can use to access memory.

特点:
Characteristics:
1. 独立性:
◦ 每个进程都有自己独立的地址空间,与其他进程的地址空间隔离。
Independence: Each process has its own independent address space, isolated from the address spaces of other processes.
2. 逻辑地址与虚拟地址:
◦ 地址空间中的地址是逻辑地址(Logical Address),也称为虚拟地址(Virtual Address)。
Logical and Virtual Addresses: Addresses in the address space are logical addresses, also known as virtual addresses.

地址绑定(Address Binding):
Address Binding:
• 将程序中的逻辑地址与物理内存地址关联的过程称为地址绑定,也称为重定位(Relocation)。
The process of associating logical addresses in a program with physical memory addresses is called address binding, or relocation.


15. 动态重定位与硬件支持

动态重定位的定义:
Definition of Dynamic Relocation:
• 动态重定位将每个进程的地址空间映射到物理内存的不同部分。
Dynamic relocation maps each process's address space to different parts of physical memory.

物理地址空间:
Physical Address Space:
• 物理地址空间的范围为 \(R+0\)\(R+\text{max}\),其中 \(R\) 是基址值。
The range of the physical address space is \(R+0\) to \(R+\text{max}\), where \(R\) is the base value.

物理地址与真实地址:
Physical and Real Addresses:
• 物理地址(Physical Address)是内存中实际的硬件地址,也称为真实地址(Real Address)。
Physical addresses are the actual hardware addresses in memory, also known as real addresses.

硬件支持:
Hardware Support:
1. 基址寄存器(Base Register):
◦ 存储进程地址空间的起始位置。
Stores the starting location of the process's address space.
2. 界限寄存器(Limit Register):
◦ 存储进程地址空间的大小限制。
Stores the size limit of the process's address space.

工作原理:
How It Works:
• 当进程访问逻辑地址时,CPU 将逻辑地址加上基址寄存器的值,生成物理地址。
When a process accesses a logical address, the CPU adds the logical address to the value in the base register to generate the physical address.
• 如果逻辑地址超出界限寄存器的范围,则触发错误。
If the logical address exceeds the range of the limit register, an error is triggered.


总结

地址空间的作用:
• 提供了进程间的内存隔离,解决了保护和重定位问题。
Provides memory isolation between processes, solving protection and relocation problems.

动态重定位的优势:
• 支持进程在内存中的灵活加载和运行,提高了内存利用率。
Supports flexible loading and running of processes in memory, improving memory utilization.

硬件支持的必要性:
• 基址寄存器和界限寄存器是实现动态重定位的关键硬件机制。
**Base and limit registers are key hardware mechanisms for implementing dynamic relocation.

基址寄存器与界限寄存器(Base and Limit Registers)

以下是对基址寄存器和界限寄存器的详细讲解,包括其工作原理、示例、优点和缺点。


16. 基址寄存器与界限寄存器的工作原理

硬件组成:
Hardware Components:
基址寄存器(Base Register, BA):
◦ 存储进程地址空间的起始物理地址。
Stores the starting physical address of the process's address space.
界限寄存器(Limit Register, LA):
◦ 存储进程地址空间的大小(或结束地址)。
Stores the size (or ending address) of the process's address space.

地址转换过程:
Address Translation Process:

  1. CPU 生成逻辑地址(Logical Address, LA)。
    The CPU generates a logical address (LA).
  2. CPU 将逻辑地址与基址寄存器的值相加,生成物理地址(Physical Address, PA)。
    The CPU adds the logical address to the value in the base register to generate the physical address (PA).
    \[ PA = LA + BA \]
  3. CPU 检查逻辑地址是否超出界限寄存器的范围:
    ◦ 如果 \(LA < \text{Limit Register}\),则访问合法,继续执行。
    ◦ 如果 \(LA \geq \text{Limit Register}\),则触发错误(Fault)。
    If \(LA < \text{Limit Register}\), the access is valid and execution continues.
    If \(LA \geq \text{Limit Register}\), an error (Fault) is triggered.

示意图:
Diagram:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CPU
|
Logical Address (LA)
|
+-------------------+
| Base Register (BA)| --> PA = LA + BA
+-------------------+
|
Physical Address (PA)
|
<-------------------+
| Limit Register | --> Check: LA < Limit?
+-------------------+
|
Fault (if LA >= Limit)


17. 基址寄存器与界限寄存器的示例

示例场景:
Example Scenario:
• 假设有两个程序,每个程序占用 16 KB 内存。
Assume there are two programs, each occupying 16 KB of memory.

程序 1 的地址空间:
Address Space of Program 1:
• 基址寄存器(BA):0
• 界限寄存器(LA):16,384(16 KB)
Base Register (BA): 0
Limit Register (LA): 16,384 (16 KB)

程序 2 的地址空间:
Address Space of Program 2:
• 基址寄存器(BA):16,384
• 界限寄存器(LA):32,768(16 KB)
Base Register (BA): 16,384
Limit Register (LA): 32,768 (16 KB)

工作过程:
Working Process:
• 当程序 1 访问逻辑地址 \(x\) 时,物理地址为 \(PA = x + 0\)
◦ 如果 \(x \geq 16,384\),触发错误。
When Program 1 accesses logical address \(x\), the physical address is \(PA = x + 0\).
If \(x \geq 16,384\), an error is triggered.
• 当程序 2 访问逻辑地址 \(y\) 时,物理地址为 \(PA = y + 16,384\)
◦ 如果 \(y \geq 16,384\),触发错误。
When Program 2 accesses logical address \(y\), the physical address is \(PA = y + 16,384\).
If \(y \geq 16,384\), an error is triggered.


18. 动态重定位的优点与缺点

优点:
Advantages:
1. 支持进程迁移:
◦ 操作系统可以在运行时轻松地将进程移动到内存的其他位置。
The operating system can easily move processes to other locations in memory during execution.
◦ 只需更新基址寄存器的值即可完成迁移。
Only the value of the base register needs to be updated to complete the migration.
2. 支持进程增长:
◦ 操作系统可以通过调整界限寄存器的值,允许进程动态增长。
The operating system can allow processes to grow dynamically by adjusting the value of the limit register.
3. 硬件简单:
◦ 只需要两个特殊寄存器(基址寄存器和界限寄存器)以及加法和比较操作。
The hardware is simple: only two special registers (base and limit registers) and addition and comparison operations are required.

缺点:
Disadvantages:
1. 性能开销:
◦ 每次访问内存时都需要进行加法和比较操作,降低了性能。
Every memory access requires addition and comparison operations, reducing performance.
2. 无法共享内存:
◦ 每个进程有独立的地址空间,无法直接共享内存。
Each process has an independent address space and cannot directly share memory.
3. 受限于物理内存:
◦ 进程的地址空间大小受限于物理内存的大小。
The size of the process's address space is limited by the size of the physical memory.
4. 内存管理复杂性:
◦ 动态重定位增加了内存管理的复杂性,尤其是在多进程环境下。
Dynamic relocation increases the complexity of memory management, especially in multi-process environments.


总结

基址寄存器与界限寄存器的作用:
• 提供了一种简单的动态重定位机制,支持进程的地址空间隔离和保护。
Provide a simple dynamic relocation mechanism, supporting address space isolation and protection for processes.

动态重定位的优点:
• 支持进程迁移和增长,硬件实现简单。
Supports process migration and growth, with simple hardware implementation.

动态重定位的缺点:
• 性能开销较大,无法共享内存,受限于物理内存大小。
High performance overhead, inability to share memory, and limitation by physical memory size.


改进方向

虚拟内存技术:
• 通过虚拟内存技术,可以解决物理内存限制和进程间共享内存的问题。
Virtual memory technology can solve the problems of physical memory limitations and inter-process memory sharing.

分页与分段机制:
• 结合分页和分段机制,可以更高效地管理内存,减少碎片化问题。
Combining paging and segmentation mechanisms can manage memory more efficiently and reduce fragmentation.


交换(Swapping)与内存管理

以下是对交换(Swapping)的详细讲解,包括其定义、作用、动态内存分配、进程增长的处理、内存碎片问题以及解决方案。


19. 交换的基本概念

背景问题:
Background Problem:
• 当系统中运行的程序总大小超过物理内存容量时,如何有效管理内存?
When the total size of programs exceeds the physical memory capacity, how to manage memory effectively?

交换的定义:
Definition of Swapping:
交换(Swapping): 将整个进程从磁盘加载到内存中运行一段时间,然后将其移回磁盘以腾出内存空间。
Swapping: Bringing the whole process into memory, running it for a while, and then moving it back to disk to free up memory space.

虚拟内存的对比:
Comparison with Virtual Memory:
虚拟内存(Virtual Memory): 允许程序在仅部分加载到主内存的情况下运行,通过分页或分段机制实现。
Virtual Memory: Allows programs to run even when they are only partially in main memory, using paging or segmentation mechanisms.

交换的优势:
Advantages of Swapping:
• 允许多个进程共享内存分区,提高内存利用率。
Allows multiple processes to share memory partitions, improving memory utilization.


20. 交换的内存分配变化

动态内存分配:
Dynamic Memory Allocation:
• 当进程进入内存时,分配足够的内存空间;如果内存不足,则进程等待在磁盘上。
When a process enters memory, allocate sufficient space; if memory is insufficient, the process waits on disk.

内存分配示意图:
Memory Allocation Diagram:
• 内存中的阴影区域表示未使用的内存。
The shaded areas in memory represent unused memory.


21. 进程增长的挑战

问题描述:
Problem Description:
• 如果大多数进程在运行时会增长(如动态分配内存),如何处理内存分配问题?
If most processes grow during execution (e.g., dynamic memory allocation), how to handle memory allocation?

解决方案:
Solutions:

  1. 交换进程:
    ◦ 将增长的内存进程交换到更大的内存分区中。
    Swap out the growing process and swap it back into a larger memory partition.
  2. 问题:
    ◦ 频繁的交换操作会导致性能下降,因为磁盘 I/O 操作较慢。
    Problem: Frequent swapping operations can lead to performance degradation due to slow disk I/O.

22. 进程增长的解决方案

动态分配空间:
Dynamic Space Allocation:
数据段增长:
◦ 为增长的数据段分配额外的内存空间。
Allocate additional memory space for the growing data segment.
栈增长:
◦ 为增长的栈和数据段分配额外的内存空间。
Allocate additional memory space for the growing stack and data segment.

内存分配示意图:
Memory Allocation Diagrams:
• (a) 数据段增长的内存分配。
(a) Memory allocation for growing data segment.
• (b) 栈和数据段同时增长的内存分配。
(b) Memory allocation for growing stack and data segment.


23. 内存碎片问题

内存碎片的产生:
Memory Fragmentation:
• 随着进程的交换和释放,内存中会出现许多不连续的小块空闲内存(碎片)。
As processes are swapped in and out, many small, non-contiguous free memory blocks (fragments) appear in memory.

内存碎片的影响:
Impact of Memory Fragmentation:
• 碎片化导致无法分配大块连续内存,即使总空闲内存足够。
Fragmentation prevents the allocation of large contiguous memory blocks, even if the total free memory is sufficient.

内存碎片示意图:
Memory Fragmentation Diagram:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Before Compaction:
Operating System: 128 KB
Process 1: 320 KB
Free: 576 KB (fragmented)
Process 2: 224 KB
Free: 288 KB (fragmented)
Process 3: 224 KB
Free: 64 KB (fragmented)

After Compaction:
Operating System: 128 KB
Free: 640 KB (contiguous)
Process 1: 320 KB
Process 2: 224 KB
Process 3: 288 KB

解决方案:
Solutions:
1. 内存压缩(Memory Compaction):
◦ 将所有进程移动到内存的一端,将碎片合并为一个大的连续空闲区域。
Move all processes to one end of memory and merge fragments into a large contiguous free area.
2. 问题:
◦ 内存压缩需要额外的 CPU 时间,可能影响系统性能。
Memory compaction requires additional CPU time and may affect system performance.


交换与虚拟内存的对比

特性 交换(Swapping) 虚拟内存(Virtual Memory)
定义 将整个进程加载到内存中运行一段时间,然后移回磁盘。 允许程序仅加载部分到内存中运行,通过分页或分段机制实现。
优点 - 简单易实现。
- 提高内存利用率,允许多个进程共享内存。
- 支持更大的地址空间。
- 提高内存利用率,减少内存浪费。
- 支持进程的部分加载和运行。
缺点 - 频繁交换导致性能下降。
- 无法支持部分加载。
- 内存碎片问题严重。
- 需要硬件支持(如页表、TLB)。
- 页面置换算法可能导致性能波动(如 Belady 异常)。
适用场景 - 早期操作系统或资源受限的环境。 - 现代操作系统,支持多任务和高性能需求。

内存管理的改进方向

虚拟内存的引入:
• 通过分页(Paging)和分段(Segmentation)机制,解决交换中的性能问题和内存碎片问题。
Introduce virtual memory with paging and segmentation mechanisms to solve the performance and fragmentation issues of swapping.

内存压缩优化:
• 使用更高效的内存压缩算法,减少压缩操作对系统性能的影响。
Optimize memory compaction algorithms to reduce the performance impact of compaction operations.

动态内存分配策略:
• 使用更智能的内存分配策略(如伙伴系统、SLAB 分配器)来减少碎片化。
Use smarter memory allocation strategies (e.g., buddy system, SLAB allocator) to reduce fragmentation.


总结

交换的核心思想:
• 将进程整体加载到内存中运行一段时间,然后移回磁盘,适合早期资源受限的系统。
The core idea of swapping is to load a process entirely into memory, run it for a while, and then move it back to disk, suitable for early resource-constrained systems.

虚拟内存的优势:
• 支持部分加载和更大的地址空间,是现代操作系统的主流内存管理方式。
Virtual memory supports partial loading and larger address spaces, making it the mainstream memory management method in modern operating systems.

内存碎片的挑战:
• 内存碎片会导致内存利用率下降,需要通过压缩或优化分配策略来解决。
Memory fragmentation reduces memory utilization and needs to be addressed through compaction or optimized allocation strategies.

未来方向:
• 结合虚拟内存、分页、分段等技术,进一步优化内存管理效率。
Future directions involve combining virtual memory, paging, and segmentation to further optimize memory management efficiency.


内存管理中的紧凑(Compaction)与内存分配跟踪方法

以下是对内存紧凑(Compaction)、位图(Bitmap)和链表(Linked Lists)三种内存管理方法的详细讲解,包括它们的原理、优缺点以及实际应用场景。


26. 内存紧凑(Compaction)

定义:
Definition:
• 内存紧凑是一种通过移动进程来合并内存碎片的技术,生成一个大的连续空闲内存区域。
Memory compaction is a technique that moves processes to merge memory fragments and create a large contiguous free memory area.

假设条件:
Assumptions:
1. 程序是可重定位的(Relocatable),即程序可以在内存中移动。
Programs are relocatable, meaning they can be moved in memory.
2. 在紧凑过程中,所有进程必须暂停(Suspended)。
All processes must be suspended during compaction.

触发条件:
Trigger Condition:
• 紧凑操作仅在内存碎片非常严重时执行。
Compaction is performed only when fragmentation becomes very severe.

工作过程:
Working Process:
• 紧凑操作将所有进程移动到内存的一端,空闲内存被合并为一个大的连续区域。
Compaction moves all processes to one end of memory, merging free memory into a large contiguous area.

优点:
Advantages:
1. 解决了内存碎片问题,生成大块连续空闲内存。
Resolves memory fragmentation and generates large contiguous free memory.
2. 提高了内存分配效率,避免了因碎片化导致的分配失败。
Improves memory allocation efficiency and avoids allocation failures due to fragmentation.

缺点:
Disadvantages:
1. 性能开销大:
◦ 紧凑操作需要移动所有进程,消耗大量 CPU 时间。
High performance overhead: Compaction requires moving all processes, consuming significant CPU time.
2. 进程暂停:
◦ 在紧凑过程中,所有进程必须暂停,可能导致系统响应时间变长。
Process suspension: All processes must be suspended during compaction, potentially increasing system response time.

适用场景:
Applicable Scenarios:
• 内存碎片化严重的早期系统。
Early systems with severe memory fragmentation.
• 现代系统中较少使用,通常通过虚拟内存或其他技术避免碎片化问题。
Rarely used in modern systems, which typically avoid fragmentation through virtual memory or other techniques.


27. 内存管理中的跟踪方法

问题:
Problem:
• 如何跟踪内存的使用情况?
How to track memory usage?

两种主要方法:
Two Main Methods:
1. 位图(Bitmap):
◦ 使用位图记录每个分配单元的状态(空闲或已分配)。
Uses a bitmap to record the status of each allocation unit (free or allocated).
2. 链表(Linked Lists):
◦ 使用链表记录每个空闲或已分配的内存段,包括起始地址、长度和指向下一个节点的指针。
Uses a linked list to record each free or allocated memory segment, including its starting address, length, and a pointer to the next node.

img

28. 位图(Bitmap)

定义:
Definition:
• 内存被划分为固定大小的分配单元(Allocation Unit),每个分配单元在位图中用 1 位表示。
Memory is divided into fixed-size allocation units, and each allocation unit is represented by 1 bit in the bitmap.
0 表示空闲,1 表示已分配。
0: Free, 1: Allocated.

优点:
Advantages:

  1. 简单高效:
    ◦ 位图结构简单,易于实现和管理。
    Simple and efficient: The bitmap structure is simple and easy to implement and manage.
  2. 快速查找:
    ◦ 可以快速判断某个分配单元是否空闲。
    Fast lookup: Quickly determine whether an allocation unit is free.

缺点:
Disadvantages:
1. 位图大小问题:
◦ 分配单元越小,位图越大,可能占用大量内存。
Bitmap size issue: Smaller allocation units result in a larger bitmap, potentially consuming significant memory.
2. 查找连续空闲单元慢:
◦ 在位图中查找一段连续的空闲单元(如长度为 \(n\) 的连续空闲区域)是一个缓慢的操作。
Slow search for contiguous free units: Searching for a run of contiguous free units in the bitmap is a slow operation.

适用场景:
Applicable Scenarios:
• 内存分配单元较大且碎片化不严重的系统。
Systems with large allocation units and low fragmentation.


29. 链表(Linked Lists)

定义:
Definition:
• 内存管理器维护一个链表,每个节点记录一个空闲段(Hole)或已分配段(Process),包括起始地址、长度和指向下一个节点的指针。
The memory manager maintains a linked list, where each node records a hole (free segment) or a process (allocated segment), including its starting address, length, and a pointer to the next node.

链表排序方式:
Sorting Methods for the Linked List:
1. 按地址排序:
◦ 节点按内存地址顺序排列。
Nodes are sorted by memory address.
2. 按大小排序:
◦ 节点按空闲段的大小顺序排列。
Nodes are sorted by the size of the free segments.

按地址排序的优点:
Advantages of Sorting by Address:
1. 进程终止或交换时更新简单:
◦ 当一个进程终止或被交换出内存时,更新链表非常直接。
Updating the list is straightforward when a process terminates or is swapped out.
2. 适合合并相邻空闲段:
◦ 按地址排序便于合并相邻的空闲段。
Sorting by address facilitates merging adjacent free segments.

按大小排序的优点:
Advantages of Sorting by Size:
1. 适合首次适配(First Fit)算法:
◦ 按大小排序可以更快地找到满足需求的最小空闲段。
Sorting by size is suitable for the First Fit algorithm, as it can quickly find the smallest free segment that satisfies the requirement.

缺点:
Disadvantages:

  1. 链表维护开销:
    ◦ 每次分配或释放内存时,都需要更新链表,可能带来额外的开销。
    Overhead of maintaining the linked list: The linked list must be updated during each memory allocation or release, potentially introducing additional overhead.
  2. 碎片化问题:
    ◦ 链表无法直接解决内存碎片问题,需要结合其他技术(如紧凑操作)。
    Fragmentation issue: Linked lists cannot directly resolve memory fragmentation and require additional techniques (e.g., compaction).

适用场景:
Applicable Scenarios:
• 动态内存分配需求较高的系统。
Systems with high dynamic memory allocation requirements.


30. 内存管理中的链表节点更新(Process Termination or Swapping Out)

问题描述:
Problem Description:
• 当一个进程终止或被交换出内存时,需要更新链表以合并相邻的空闲段。
When a process terminates or is swapped out, the linked list must be updated to merge adjacent free segments.

四种邻居组合:
Four Neighbor Combinations for the Terminating Process \(X\):
1. 上方有空闲段:
◦ 合并上方空闲段。
Merge with the free segment above.
2. 下方有空闲段:
◦ 合并下方空闲段。
Merge with the free segment below.
3. 上方和下方都有空闲段:
◦ 合并上方和下方的空闲段。
Merge with both the free segment above and below.
4. 没有相邻空闲段:
◦ 不需要合并,直接标记为新的空闲段。
No adjacent free segments; no merging needed, mark as a new free segment.


位图与链表的对比

特性 位图(Bitmap) 链表(Linked Lists)
定义 使用位图记录每个分配单元的状态(空闲或已分配)。 使用链表记录每个空闲段或已分配段,包括起始地址、长度和指针。
优点 - 简单高效。
- 快速判断某个分配单元是否空闲。
- 适合动态内存分配。
- 按地址排序便于合并相邻空闲段。
缺点 - 位图大小与分配单元大小相关,可能占用大量内存。
- 查找连续空闲单元慢。
- 链表维护开销较大。
- 无法直接解决内存碎片问题,需要结合其他技术(如紧凑操作)。
适用场景 - 内存分配单元较大且碎片化不严重的系统。 - 动态内存分配需求较高的系统。

总结

位图的优势与局限:
优势: 简单高效,适合快速判断分配单元状态。
Advantages: Simple and efficient, suitable for quickly determining the status of allocation units.
局限: 位图大小与分配单元相关,查找连续空闲单元较慢。
Limitations: Bitmap size depends on allocation unit size, and searching for contiguous free units is slow.

链表的优势与局限:
优势: 动态管理灵活,适合动态内存分配需求高的场景。
Advantages: Flexible dynamic management, suitable for scenarios with high dynamic memory allocation requirements.
局限: 维护开销较大,无法直接解决内存碎片问题。
Limitations: High maintenance overhead, cannot directly resolve memory fragmentation issues.

改进方向:
• 结合位图和链表的优点,设计混合内存管理机制。
Combine the advantages of bitmaps and linked lists to design a hybrid memory management mechanism.
• 使用更高效的算法(如伙伴系统、SLAB 分配器)来减少碎片化问题。
Use more efficient algorithms (e.g., buddy system, SLAB allocator) to reduce fragmentation issues.


内存分配策略(Storage Placement Strategies)

以下是对常见内存分配策略的简要讲解,包括首次适配(First Fit)、下次适配(Next Fit)、最佳适配(Best Fit)、最差适配(Worst Fit)和快速适配(Quick Fit)的原理、优缺点及示例。


31. 内存分配策略的基本方法

问题描述:
Problem Description:
• 如何从一个空闲洞(Free Holes)列表中满足大小为 \(n\) 的内存请求?

主要分配策略:
Main Placement Strategies:
1. 首次适配(First Fit)
◦ 使用第一个足够大的空闲洞。
Use the first available hole that is large enough to meet the need.
问题: 容易产生平均大小的内存碎片。
Problem: Creates average-sized holes.

  1. 下次适配(Next Fit)
    ◦ 首次适配的变体,从上次分配结束的位置开始搜索。
    A minor variation of First Fit: Start searching from the last hole that was stopped at.
    问题: 性能略低于首次适配。
    Problem: Slightly worse performance than First Fit.

  2. 最佳适配(Best Fit)
    ◦ 使用大小等于需求的最小空闲洞,如果没有完全匹配,则选择最接近且更大的空闲洞。
    Use the hole that is equal to the need, or if none is equal, the hole that is larger but closest in size.
    问题: 容易产生大量无法使用的小碎片。
    Problem: Creates small holes that cannot be used.

  3. 最差适配(Worst Fit)
    ◦ 使用最大的可用空闲洞。
    Use the largest available hole.
    问题: 容易导致大洞被分割,难以运行大程序。
    Problem: Eliminates large holes, making it difficult to run large programs.

  4. 快速适配(Quick Fit)
    ◦ 维护一些常见请求大小的单独链表,快速找到最接近的匹配。
    Maintain separate lists for some of the more common sizes requested and find the closest fit quickly.
    优点: 分配速度非常快。
    Advantage: Very fast allocation scheme.
    缺点: 合并空闲洞的开销较大。
    Disadvantage: Merging free holes is expensive.


32. 存储分配策略的对比

策略 定义 优点 缺点
首次适配(First Fit) 使用第一个足够大的空闲洞。 - 简单高效。
- 分配速度快。
- 容易产生平均大小的内存碎片。
下次适配(Next Fit) 从上次分配结束的位置开始搜索第一个足够大的空闲洞。 - 比首次适配稍快,因为不需要从头开始搜索。 - 性能略低于首次适配,可能导致内存分布不均。
最佳适配(Best Fit) 使用最小的足够大的空闲洞,或最接近且更大的空闲洞。 - 减少内存浪费,适合小内存请求。 - 容易产生大量无法使用的小碎片。
最差适配(Worst Fit) 使用最大的可用空闲洞。 - 减少小碎片的产生。 - 容易导致大洞被分割,难以运行大程序。
快速适配(Quick Fit) 维护常见大小的链表,快速找到最接近的匹配。 - 分配速度非常快。 - 合并空闲洞的开销较大,可能增加碎片化。

33. 示例:内存分配请求的处理

初始内存状态:
Initial Memory State:

1
2
3
4
5
6
7
8
9
Before Allocation:
8K (Free)
12K (Allocated)
6K (Free)
8K (Free)
14K (Allocated)
6K (Free)
2K (Free)
Last Allocated Block: 14K

请求:
Request:
• 请求分配 16K 的内存。

分配策略的处理结果:
Results of Different Placement Strategies:

##### 首次适配(First Fit)
过程:
◦ 从空闲列表中找到第一个足够大的空闲洞(8K + 6K + 2K = 16K)。
Process: Find the first hole large enough for 16K (8K + 6K + 2K = 16K).
◦ 分配后,合并的空闲洞被分割为 12K 和 6K。
After allocation, the free hole is split into 12K and 6K.
结果:

1
2
3
4
5
6
7
After Allocation:
8K (Allocated)
12K (Allocated)
6K (Free)
12K (Free)
6K (Free)
2K (Free)

##### 下次适配(Next Fit)
过程:
◦ 从上次分配结束的位置(14K)开始搜索,找到第一个足够大的空闲洞(8K + 6K + 2K = 16K)。
Process: Start searching from the last allocated block (14K) and find the first hole large enough for 16K (8K + 6K + 2K = 16K).
◦ 分配后,合并的空闲洞被分割为 12K 和 6K。
After allocation, the free hole is split into 12K and 6K.
结果:

1
2
3
4
5
6
7
After Allocation:
8K (Allocated)
12K (Allocated)
6K (Free)
12K (Free)
6K (Free)
2K (Free)

##### 最佳适配(Best Fit)
过程:
◦ 找到最小的足够大的空闲洞(8K + 6K + 2K = 16K)。
Process: Find the smallest hole large enough for 16K (8K + 6K + 2K = 16K).
◦ 分配后,剩余的空闲洞被分割为多个小碎片。
After allocation, the remaining free hole is split into multiple small fragments.
结果:

1
2
3
4
5
6
7
After Allocation:
8K (Allocated)
12K (Allocated)
6K (Free)
2K (Free)
14K (Allocated)
6K (Free)

##### 最差适配(Worst Fit)
过程:
◦ 找到最大的空闲洞(14K),分配后剩余 2K。
Process: Find the largest hole (14K), allocate 16K, and leave 2K as a fragment.
◦ 剩余的空闲洞较小,难以满足后续大内存请求。
The remaining free hole is small and difficult to satisfy future large memory requests.
结果:

1
2
3
4
5
6
After Allocation:
8K (Allocated)
12K (Allocated)
6K (Free)
2K (Free)
14K (Allocated)

##### 快速适配(Quick Fit)
过程:
◦ 使用常见大小的链表,快速找到一个接近 16K 的空闲洞(8K + 8K)。
Process: Use a list of common sizes to quickly find a hole close to 16K (8K + 8K).
◦ 分配后,剩余的空闲洞可能较大或较小,取决于链表的维护方式。
After allocation, the remaining free hole may be large or small, depending on how the list is maintained.
结果:

1
2
3
4
5
6
7

After Allocation:
8K (Allocated)
8K (Allocated)
12K (Free)
6K (Free)
2K (Free)
img

内存分配策略的优缺点总结

策略 优点 缺点
首次适配(First Fit) - 简单高效。
- 分配速度快。
- 容易产生平均大小的内存碎片。
下次适配(Next Fit) - 比首次适配稍快,因为不需要从头开始搜索。 - 性能略低于首次适配,可能导致内存分布不均。
最佳适配(Best Fit) - 减少内存浪费,适合小内存请求。 - 容易产生大量无法使用的小碎片。
最差适配(Worst Fit) - 减少小碎片的产生。 - 容易导致大洞被分割,难以运行大程序。
快速适配(Quick Fit) - 分配速度非常快。 - 合并空闲洞的开销较大,可能增加碎片化。

实际应用中的选择

首次适配(First Fit):
• 最常用的策略,适合大多数场景。
Most commonly used strategy, suitable for most scenarios.

最佳适配(Best Fit):
• 适合内存资源紧张、需要减少内存浪费的场景。
Suitable for scenarios with limited memory resources where reducing waste is critical.

最差适配(Worst Fit):
• 适合需要减少小碎片的场景,但可能导致大洞被分割。
Suitable for scenarios where reducing small fragments is critical, but may lead to fragmentation of large holes.

快速适配(Quick Fit):
• 适合对分配速度要求极高的场景,但需要额外的维护成本。
Suitable for scenarios requiring extremely fast allocation, but incurs additional maintenance costs.


总结

首次适配(First Fit):
• 简单高效,适合大多数场景。
Simple and efficient, suitable for most scenarios.

最佳适配(Best Fit):
• 减少内存浪费,但容易产生小碎片。
Reduces memory waste but creates small fragments.

最差适配(Worst Fit):
• 减少小碎片,但可能导致大洞被分割。
Reduces small fragments but may fragment large holes.

快速适配(Quick Fit):
• 分配速度快,但合并开销大。
Fast allocation but high merge cost.

实际选择:
• 根据系统需求选择合适的策略,通常首次适配是最平衡的选择。
Choose the strategy based on system requirements; First Fit is often the most balanced choice.


覆盖技术(Overlaying)

以下是对覆盖技术(Overlaying)的详细讲解,包括其定义、工作原理、优缺点以及示例。


34. 覆盖技术的定义与背景

定义:
Definition:
• 覆盖技术是一种在内存有限的情况下,将程序分割成多个小块(称为覆盖,Overlay),并只将需要的部分加载到内存中运行的技术。
Overlaying is a technique used in memory-constrained environments where a program is divided into small pieces called overlays, and only the required pieces are loaded into memory for execution.

背景:
Background:
• 覆盖技术起源于 20 世纪 60 年代,当时计算机的内存资源非常有限。
Overlaying originated in the 1960s when computer memory resources were very limited.

工作原理:
Working Principle:
1. 程序被分割成多个覆盖(Overlays),每个覆盖是一个独立的代码或数据块。
The program is divided into multiple overlays, each of which is an independent block of code or data.
2. 操作系统负责将需要的覆盖加载到内存中,并在需要时将其替换为其他覆盖。
The operating system is responsible for loading the required overlays into memory and replacing them with other overlays when needed.
3. 不同覆盖之间可以共享公共内存空间。
Overlays can share common memory space.

程序员的职责:
Programmer's Responsibility:
• 程序员需要手动将程序分割成多个覆盖,这增加了编程的复杂性。
The programmer must manually divide the program into overlays, which increases the complexity of programming.


35. 覆盖技术的结构

覆盖管理器(Overlay Manager):
Overlay Manager:
• 操作系统中的一个模块,负责管理覆盖的加载和替换。
A module in the operating system responsible for managing the loading and replacement of overlays.

覆盖区(Overlay Area):
Overlay Area:
• 内存中专门用于存储覆盖的区域。
A region of memory dedicated to storing overlays.

主程序(Main Program):
Main Program:
• 程序的核心部分,通常始终驻留在内存中。
The core part of the program, usually always resident in memory.

覆盖块(Overlays):
Overlays:
• 程序被分割成的多个小块,按需加载到覆盖区中。
Small pieces of the program that are loaded into the overlay area as needed.

辅助存储(Secondary Storage):
Secondary Storage:
• 存储程序的所有覆盖块,当需要时由操作系统加载到内存中。
Stores all the overlays of the program and is used by the operating system to load overlays into memory when needed.


36. 覆盖技术的示例

示例 1:覆盖的内存分配
Example 1: Memory Allocation for Overlays

1
2
3
4
Overlay Area:
0K - 5K: Overlay 1
5K - 12K: Overlay 2
12K - 19K: Overlay 3

示例 2:程序的内存优化
Example 2: Memory Optimization for a Program
• 程序由多个模块组成,每个模块的大小如下:
The program consists of multiple modules, each with the following sizes:

1
2
3
4
5
6
A: 20K
B: 50K
C: 30K
D: 20K
E: 40K
F: 30K

未使用覆盖技术的内存需求:
◦ 如果所有模块都需要同时加载到内存中,则总内存需求为:
\[ 20K + 50K + 30K + 20K + 40K + 30K = 190K \]使用覆盖技术的内存优化:
◦ 假设程序的核心部分(Resident)为 20K,其他模块可以按需加载:

1
2
3
Resident: 20K
Overlay 0 (B, D, E): 50K
Overlay 1 (C, F): 30K
◦ 总内存需求减少为:
\[ 20K + 50K + 30K = 100K \]


37. 覆盖技术的优缺点

优点:
Advantages:
1. 节省内存:
◦ 通过只加载需要的覆盖块,显著减少了内存需求。
Saves memory by loading only the required overlays, significantly reducing memory requirements.
2. 适合早期系统:
◦ 在内存资源非常有限的系统中,覆盖技术是一种有效的解决方案。
Suitable for early systems with very limited memory resources.

缺点:
Disadvantages:
1. 编程复杂性高:
◦ 程序员需要手动将程序分割成多个覆盖块,增加了编程的复杂性。
High programming complexity: The programmer must manually divide the program into overlays, increasing programming complexity.
2. 运行效率低:
◦ 覆盖块的加载和替换需要额外的时间,可能影响程序的运行效率。
Low runtime efficiency: Loading and replacing overlays takes additional time, potentially affecting program performance.
3. 依赖操作系统支持:
◦ 需要操作系统提供覆盖管理功能,否则无法实现。
Dependent on operating system support: Requires the operating system to provide overlay management functionality.


38. 覆盖技术的现代应用

现代系统的替代方案:
Modern Alternatives to Overlaying:
• 覆盖技术在现代操作系统中已基本被淘汰,取而代之的是虚拟内存和分页技术。
Overlaying has largely been replaced in modern operating systems by virtual memory and paging techniques.
• 虚拟内存允许程序在仅部分加载到内存的情况下运行,极大地简化了编程复杂性。
Virtual memory allows programs to run with only part of their code and data loaded into memory, greatly simplifying programming complexity.

覆盖技术的遗留影响:
• 覆盖技术的思想仍然影响了现代内存管理的设计,例如模块化加载和动态链接库(DLL)。
The concept of overlaying still influences modern memory management designs, such as modular loading and dynamic link libraries (DLLs).


覆盖技术与虚拟内存的对比

特性 覆盖技术(Overlaying) 虚拟内存(Virtual Memory)
定义 将程序分割成多个覆盖块,按需加载到内存中运行。 允许程序在仅部分加载到内存的情况下运行,通过分页或分段机制实现。
优点 - 节省内存。
- 适合早期内存资源有限的系统。
- 支持更大的地址空间。
- 提高内存利用率,减少内存浪费。
- 支持部分加载和运行,简化编程复杂性。
缺点 - 编程复杂性高。
- 运行效率低。
- 需要手动分割程序。
- 需要硬件支持(如页表、TLB)。
- 页面置换算法可能导致性能波动(如 Belady 异常)。
适用场景 - 早期操作系统或内存资源非常有限的系统。 - 现代操作系统,支持多任务和高性能需求。
编程复杂性 - 高:程序员需要手动分割程序。 - 低:操作系统自动管理内存,程序员无需关心内存分配细节。

总结

覆盖技术的核心思想:
• 在内存有限的情况下,通过分割程序并按需加载覆盖块来节省内存。
The core idea of overlaying is to save memory by dividing a program into pieces and loading overlays as needed in memory-constrained environments.

覆盖技术的局限性:
• 编程复杂性高,运行效率低,已逐渐被虚拟内存取代。
Overlaying has high programming complexity and low runtime efficiency, and has been largely replaced by virtual memory.

虚拟内存的优势:
• 提供了更大的地址空间,支持部分加载和运行,极大地简化了编程复杂性。
Virtual memory provides a larger address space, supports partial loading and execution, and greatly simplifies programming complexity.

现代内存管理的演进:
• 覆盖技术的思想仍然影响了现代内存管理的设计,但其功能已被虚拟内存和分页技术所取代。
The concept of overlaying still influences modern memory management designs, but its functionality has been replaced by virtual memory and paging techniques.


虚拟内存(Virtual Memory)

以下是对虚拟内存的详细讲解,包括其定义、原理、实现方式(分页与分段)、工作原理以及示例。


37. 虚拟内存的定义与特点

定义:
Definition:
• 虚拟内存是一种内存管理技术,通过将用户的逻辑地址空间与物理内存分离,为用户提供比实际物理内存更大的地址空间。
Virtual memory is a memory management technique that separates a user's logical address space from physical memory, providing a larger address space than the actual physical memory.

特点:
Features:
1. 逻辑地址空间大于物理地址空间:
◦ 用户的逻辑地址空间可以远大于物理内存的大小。
The logical address space can be much larger than the physical address space.
2. 存储在磁盘上:
◦ 虚拟内存存储在磁盘上,只有部分程序需要运行时才会加载到内存中。
Virtual memory is stored on disk and only parts of the program are loaded into memory when needed.
3. 地址空间共享:
◦ 允许多个进程共享地址空间,提高内存利用率。
Allows address spaces to be shared by multiple processes, improving memory utilization.
4. 高效进程创建:
◦ 进程创建时无需一次性加载整个程序,只需加载必要的部分。
Enables more efficient process creation by loading only necessary parts of the program.
5. 透明性:
◦ 地址转换由硬件和操作系统自动完成,用户程序无需干预。
Transparent to the user: Address translation is automatically completed by hardware and the operating system.


38. 局部性原理(Principle of Locality)

定义:
Definition:
• 局部性原理是指程序在执行过程中,通常只访问其地址空间中的一小部分页面。
The principle of locality states that during execution, a process typically accesses only a small fraction of its address space.

分类:
Types:
1. 时间局部性(Time Locality):
◦ 如果某个页面被访问过,那么它很可能在不久的将来再次被访问。
If a page is accessed, it is likely to be accessed again in the near future.
2. 空间局部性(Space Locality):
◦ 如果某个页面被访问过,那么与其相邻的页面也很可能被访问。
If a page is accessed, its neighboring pages are also likely to be accessed.

意义:
Significance:
• 局部性原理是虚拟内存技术的基础,确保了虚拟内存的高效性。
The principle of locality is the foundation of virtual memory technology, ensuring its efficiency.


39. 虚拟内存的实现方式

分页(Paging):
Paging:
• 现代主流的虚拟内存实现方式,将地址空间划分为固定大小的页(Page)。
The modern and most common approach to implementing virtual memory, dividing the address space into fixed-size pages.

分段(Segmentation):
Segmentation:
• 早期的虚拟内存实现方式,将地址空间划分为逻辑段(Segment),每段可以动态调整大小。
An earlier approach to virtual memory, dividing the address space into logical segments that can dynamically adjust in size.

分页与分段结合(Combined Paging and Segmentation):
Combined Paging and Segmentation:
• 结合分页和分段的优点,提供更灵活的内存管理方式。
Combines the advantages of paging and segmentation, providing a more flexible memory management approach.


40. 分页(Paging)

定义:
Definition:
• 分页是一种实现虚拟内存的技术,将虚拟地址空间和物理内存划分为固定大小的页(Page)和页框(Page Frame)。
Paging is a technique used to implement virtual memory by dividing the virtual address space and physical memory into fixed-size pages and page frames.

页表(Page Table):
Page Table:
• 页表用于记录虚拟页到物理页框的映射关系。
The page table records the mapping relationship between virtual pages and physical page frames.

内存管理单元(MMU):
Memory Management Unit (MMU):
• MMU 负责将虚拟地址转换为物理地址。
The MMU is responsible for translating virtual addresses into physical addresses.


41. 分页的工作原理

虚拟地址与物理地址的关系:
Relationship Between Virtual and Physical Addresses:
• 虚拟地址由页号(Page Number)和页内偏移(Page Offset)组成。
A virtual address consists of a page number and a page offset.
• 页表将页号映射到物理页框号(Frame Number),结合页内偏移生成物理地址。
The page table maps the page number to a physical frame number, and the page offset is used to generate the physical address.

页错误(Page Fault):
Page Fault:
• 当访问的页未映射到物理内存时,会触发页错误。
A page fault occurs when a page is not mapped to physical memory.
• 操作系统会将所需页从磁盘加载到内存,并更新页表。
The operating system loads the required page from disk into memory and updates the page table.


42. 分页的示例

假设条件:
Assumptions:
• 计算机可以生成 16 位地址(地址范围为 0 到 64 KB)。
The computer can generate 16-bit addresses (address range 0 to 64 KB).
• 计算机只有 32 KB 的物理内存。
The computer has only 32 KB of physical memory.
• 虚拟地址空间为 64 KB,但只能部分加载到内存中。
The virtual address space is 64 KB, but only part of it can be loaded into memory.

页表的作用:
Role of the Page Table:
• 页表记录哪些页已映射到物理内存,哪些页未映射。
The page table records which pages are mapped to physical memory and which are not.
• 如果访问未映射的页,会触发页错误。
Accessing an unmapped page triggers a page fault.


43. 分页的地址转换过程

虚拟地址的组成:
Components of a Virtual Address:
• 虚拟地址由页号(Page Number)和页内偏移(Page Offset)组成。
A virtual address consists of a page number and a page offset.
• 页号用于索引页表,页内偏移用于定位页内的具体位置。
The page number is used to index the page table, and the page offset is used to locate the specific position within the page.

物理地址的组成:
Components of a Physical Address:
• 物理地址由页框号(Frame Number)和页内偏移(Page Offset)组成。
A physical address consists of a frame number and a page offset.
• 页表将虚拟页号映射到物理页框号,结合页内偏移生成物理地址。
The page table maps the virtual page number to a physical frame number, and the page offset is used to generate the physical address.


44. 分页模型示例

假设条件:
Assumptions:
• 逻辑地址空间为 \(2^{32}\) 字节(4 GB),页大小为 4 KB。
Logical address space is \(2^{32}\) bytes (4 GB), and page size is 4 KB.
• 页号占 20 位,页内偏移占 12 位。
The page number occupies 20 bits, and the page offset occupies 12 bits.

地址转换过程:
Address Translation Process:
1. 虚拟地址被分为页号和页内偏移。
The virtual address is divided into a page number and a page offset.
2. 页号用于索引页表,找到对应的物理页框号。
The page number is used to index the page table to find the corresponding physical frame number.
3. 物理页框号与页内偏移结合,生成物理地址。
The physical frame number is combined with the page offset to generate the physical address.


45. 分页的优缺点

优点:
Advantages:
1. 实现简单:
◦ 分页是一种简单且高效的内存管理方式。
Simple and efficient memory management.
2. 支持大地址空间:
◦ 用户的逻辑地址空间可以远大于物理内存的大小。
Supports a large address space, where the user's logical address space can be much larger than the physical memory size.
3. 内存利用率高:
◦ 通过按需加载和页面置换,提高内存利用率。
High memory utilization through demand loading and page replacement.

缺点:
Disadvantages:
1. 内部碎片:
◦ 页大小固定,可能导致内部碎片。
Internal fragmentation due to fixed page size.
2. 页错误开销:
◦ 页错误会导致额外的磁盘 I/O 操作,影响性能。
Page faults incur additional disk I/O operations, affecting performance.


46. 分页与分段的对比

特性 分页(Paging) 分段(Segmentation)
定义 将地址空间划分为固定大小的页。 将地址空间划分为逻辑段,每段大小可变。
优点 - 实现简单。
- 支持大地址空间。
- 内存利用率高。
- 支持逻辑分段,便于共享和保护。
- 段大小可变,灵活性高。
缺点 - 固定页大小可能导致内部碎片。
- 页错误开销较大。
- 段大小可变可能导致外部碎片。
- 实现复杂,管理开销较大。
适用场景 - 现代操作系统,支持大内存和高性能需求。 - 早期操作系统,适合逻辑分段需求高的场景。

47. 分页与分段的结合

结合方式:
Combination Approach:
• 将分页和分段结合,既支持逻辑分段,又通过分页实现固定大小的页。
Combines segmentation and paging, supporting logical segmentation while implementing fixed-size pages through paging.

优点:
Advantages:
1. 灵活性高:
◦ 支持逻辑分段,同时通过分页提高内存利用率。
High flexibility: Supports logical segmentation while improving memory utilization through paging.
2. 便于管理:
◦ 分段便于共享和保护,分页便于内存管理。
Ease of management: Segmentation facilitates sharing and protection, while paging facilitates memory management.

缺点:
Disadvantages:
1. 实现复杂:
◦ 需要同时维护段表和页表,增加了实现的复杂性。
Complex implementation: Requires maintaining both segment tables and page tables, increasing complexity.


总结

虚拟内存的核心思想:
• 将逻辑地址空间与物理内存分离,通过分页或分段技术实现高效的内存管理。
The core idea of virtual memory is to separate the logical address space from physical memory and achieve efficient memory management through paging or segmentation techniques.

分页的优势与局限:
优势: 实现简单,支持大地址空间,内存利用率高。
Advantages: Simple implementation, supports large address space, high memory utilization.
局限: 固定页大小可能导致内部碎片,页错误开销较大。
Limitations: Fixed page size may cause internal fragmentation, and page faults incur significant overhead.

分段的独特优势:
• 支持逻辑分段,便于共享和保护,灵活性高。
Supports logical segmentation, facilitates sharing and protection, and offers high flexibility.

现代内存管理的趋势:
• 现代操作系统通常采用分页技术,同时结合分段的思想,提供更灵活的内存管理方式。
Modern operating systems typically use paging techniques combined with segmentation to provide more flexible memory management.


页表(Page Table)与转换后备缓冲器(TLB)

以下是对页表及其相关机制的详细讲解,包括页表的结构、问题、多级页表、TLB 的工作原理及其优化。


50. 页表的基本概念

定义:
Definition:
• 页表(Page Table)是操作系统用于管理虚拟地址到物理地址映射的核心数据结构。
A page table is a core data structure used by the operating system to manage the mapping of virtual addresses to physical addresses.

页表的作用:
Role of the Page Table:
1. 虚拟页号(VPN)到物理页框号(PFN)的映射:
◦ 每个虚拟页号(VPN)通过页表映射到一个物理页框号(PFN)。
Mapping of virtual page numbers (VPN) to physical frame numbers (PFN).
2. 支持虚拟内存:
◦ 通过页表,操作系统可以实现虚拟内存的分页管理。
Supports virtual memory by enabling paging management.

页表的存储:
Storage of Page Tables:
• 每个进程通常有一个独立的页表,由操作系统管理。
Each process typically has its own page table, managed by the operating system.


51. 典型的页表项(Page Table Entry, PTE)

页表项的内容:
Contents of a Page Table Entry (PTE):
1. 物理页框号(PFN):
◦ 记录虚拟页对应的物理页框号。
Physical frame number (PFN): Records the physical frame number corresponding to the virtual page.
2. 有效/无效位(Present/Absent Bit):
◦ 标识该页表项是否有效(即该虚拟页是否映射到物理内存)。
Present/Absent Bit: Indicates whether the page table entry is valid (i.e., whether the virtual page is mapped to physical memory).
3. 保护位(Protection Bit):
◦ 指定对该页的访问权限(如读、写、执行)。
Protection Bit: Specifies access permissions for the page (e.g., read, write, execute).
4. 修改位(Modified Bit, Dirty Bit):
◦ 标识该页是否被修改过,若被修改过,则在写回磁盘时需要保存。
Modified Bit (Dirty Bit): Indicates whether the page has been modified and needs to be saved when written back to disk.
5. 引用位(Referenced Bit):
◦ 标识该页是否被访问过,用于页面置换算法(如 LRU)。
Referenced Bit: Indicates whether the page has been accessed, used in page replacement algorithms (e.g., LRU).
6. 缓存禁用位(Cache Disabled Bit):
◦ 指定该页是否可以被缓存。
Cache Disabled Bit: Specifies whether the page can be cached.


58. 页表的问题

主要问题:
Major Issues with Page Tables:
1. 映射速度问题:
◦ 每次内存访问都需要进行地址转换,页表查找必须非常快。
Mapping speed issue: Address translation is required for every memory access, and page table lookups must be very fast.
2. 页表规模问题:
◦ 页表可能非常大。例如,32 位地址空间、4 KB 页大小时,页表需要 \(2^{20} = 1,048,576\) 个条目。
Page table size issue: Page tables can be extremely large. For example, with a 32-bit address space and 4 KB page size, the page table requires \(2^{20} = 1,048,576\) entries.


59. 页表的存储方式

单页表(Single Page Table):
Single Page Table:
• 使用硬件寄存器数组存储页表。
The page table is stored in an array of hardware registers.
优点:
◦ 实现简单。
Advantage: Simple implementation.
缺点:
◦ 如果页表很大,加载整个页表的开销较高,尤其是在上下文切换时。
Disadvantage: Expensive if the table is large, and loading the full page table during every context switch hurts performance.

页表存储在内存中:
Page Table Stored in Memory:
• 使用一个寄存器指向内存中的页表。
A single register points to the page table in memory.
优点:
◦ 上下文切换成本低。
Advantage: Cheap context switch.
缺点:
◦ 每次访问页表项需要额外的内存访问,增加了延迟。
Disadvantage: Requires one or more memory references to read page table entries, increasing latency.


60. 虚拟地址到物理地址的查找

问题:
Problem:
• 每次内存访问都需要将虚拟地址转换为物理地址,可能涉及多级页表查找。
Each memory access requires translating a virtual address to a physical address, which may involve walking through hierarchical page tables.
• 页表存储在内存中,因此每次地址转换可能需要多次内存访问。
Page tables are stored in memory, so each address translation may require multiple memory accesses.

解决方案:
Solution:
• 使用转换后备缓冲器(TLB)缓存“活跃”页表项,减少内存访问次数。
Use a Translation Lookaside Buffer (TLB) to cache "active" page table entries and reduce memory accesses.


61. TLB 页表项的位

常见位(Common Bits):
1. 虚拟页号(Virtual Page Number, VPN):
◦ 用于匹配虚拟地址。
Virtual Page Number (VPN): Matches the virtual address.
2. 物理页号(Physical Page Number, PFN):
◦ 转换后的物理地址。
Physical Page Number (PFN): Translated physical address.
3. 有效位(Valid Bit):
◦ 标识该页表项是否有效。
Valid Bit: Indicates whether the page table entry is valid.
4. 访问位(Access Bits):
◦ 区分内核和用户访问权限(如无、读、写)。
Access Bits: Differentiate kernel and user access permissions (e.g., nil, read, write).

可选位(Optional Bits):
1. 进程标签(Process Tag):
◦ 标识该页表项属于哪个进程。
Process Tag: Identifies which process the page table entry belongs to.
2. 引用位(Reference Bit):
◦ 标识该页是否被访问过。
Reference Bit: Indicates whether the page has been accessed.
3. 修改位(Modify Bit):
◦ 标识该页是否被修改过。
Modify Bit: Indicates whether the page has been modified.
4. 可缓存位(Cacheable Bit):
◦ 指定该页是否可以被缓存。
Cacheable Bit: Specifies whether the page can be cached.


62. 转换后备缓冲器(TLB)的功能

TLB 的工作原理:
How TLB Works:
1. TLB 命中(TLB Hit):
◦ 如果虚拟地址在 TLB 中找到匹配项,则直接从 TLB 获取页表项,无需访问页表。
TLB Hit: If the virtual address matches an entry in the TLB, the page table entry is retrieved directly from the TLB without accessing the page table.
2. TLB 未命中(TLB Miss):
◦ 如果虚拟地址未在 TLB 中找到匹配项,则需要访问页表进行地址转换,并将新页表项加载到 TLB 中。
TLB Miss: If the virtual address does not match any entry in the TLB, the page table is accessed for address translation, and the new page table entry is loaded into the TLB.

TLB 命中率(TLB Hit Ratio):
• TLB 命中率是指 TLB 中找到匹配项的百分比,命中率越高,地址转换效率越高。
TLB Hit Ratio: The percentage of time a page table entry is found in the TLB. A higher hit ratio means better address translation efficiency.


66. 软件控制的 TLB

TLB 未命中的处理:
Handling TLB Misses:
1. 生成 TLB 故障(TLB Fault),并陷入操作系统(软件处理)。
Generate a TLB fault and trap to the operating system (software handling).
2. 检查包含页表项的页是否在内存中:
◦ 如果不在内存中,则触发页错误(Page Fault)。
Check if the page containing the PTE is in memory: If not, trigger a page fault.
3. 如果没有空闲 TLB 条目,则写回一个条目,并将新的页表项加载到 TLB 中。
If no free TLB entry is available, evict an entry and load the new PTE into the TLB.
4. 重新启动导致故障的指令。
Restart the faulting instruction.


67. 多级页表(Multilevel Page Tables)

问题:
Problem:
• 单级页表可能非常大,占用大量内存。
Single-level page tables can be very large and consume significant memory.

解决方案:
Solution:
• 使用多级页表,将页号分为两级或多级索引。
Use multilevel page tables, dividing the page number into two or more levels of indexing.
优点:
1. 减小页表的大小。
Reduces the size of the page table.
2. 只存储需要的页表部分,节省内存。
Stores only the required parts of the page table, saving memory.

多级页表的结构:
Structure of Multilevel Page Tables:
• 第一级页表存储第二级页表的索引。
The first-level page table stores indexes to second-level page tables.
• 第二级页表存储实际的页框号(PFN)。
The second-level page table stores the actual physical frame numbers (PFNs).


68. 多级页表的优势

稀疏地址空间的支持:
• 多级页表可以高效地支持稀疏地址空间,避免为未使用的虚拟地址分配页表项。
Support for sparse address spaces: Multilevel page tables efficiently handle sparse address spaces by avoiding allocation of PTEs for unused virtual addresses.

更简单的页面置换:
• 由于页表是分级的,操作系统可以更容易地管理页表的加载和置换。
Simpler page replacement: Since the page table is hierarchical, the operating system can more easily manage loading and replacement of page tables.


总结

页表的核心作用:
• 页表是虚拟内存管理的关键,负责虚拟地址到物理地址的映射。
The core role of the page table is to manage the mapping of virtual addresses to physical addresses in virtual memory.

页表的挑战:
规模问题: 页表可能非常大,占用大量内存。
Size issue: Page tables can be very large and consume significant memory.
速度问题: 每次内存访问都需要页表查找,影响性能。
Speed issue: Page table lookups are required for every memory access, affecting performance.

TLB 的优化:
• TLB 缓存活跃页表项,显著提高地址转换效率。
The TLB caches active page table entries, significantly improving address translation efficiency.

多级页表的优势:
• 减小页表规模,支持稀疏地址空间,优化内存利用率。
Multilevel page tables reduce size, support sparse address spaces, and optimize memory utilization.

未来趋势:
• 随着硬件性能的提升,TLB 和多级页表的结合将成为主流,进一步提高虚拟内存的效率。
With advancements in hardware, the combination of TLBs and multilevel page tables will become mainstream, further improving virtual memory efficiency.


多级页表系统与倒排页表(Inverted Page Tables)

以下是对多级页表系统、倒排页表及其优化方法的详细讲解,包括地址转换过程、性能分析、优缺点以及实际应用。


69. 多级页表系统的地址分配示例

逻辑地址的组成:
Composition of Logical Address:
• 在 32 位机器上,页大小为 4 KB(\(2^{12}\) 字节),逻辑地址被分为两部分:
On a 32-bit machine with a 4 KB page size, the logical address is divided into two parts:
1. 页号(Page Number): 20 位,用于索引页表。
Page Number: 20 bits, used to index the page table.
2. 页内偏移(Page Offset): 12 位,用于定位页内的具体位置。
Page Offset: 12 bits, used to locate the specific position within the page.

多级页表的分级:
Hierarchical Division of Page Numbers:
• 将 20 位页号进一步分为两级:
◦ 第一级页号:10 位。
◦ 第二级页号:10 位。
First-level Page Number: 10 bits.
Second-level Page Number: 10 bits.

地址转换过程:
Address Translation Process:

  1. 使用第一级页号查找第一级页表,找到第二级页表的基址。
    Use the first-level page number to find the base address of the second-level page table.
  2. 使用第二级页号查找第二级页表,找到物理页框号(PFN)。
    Use the second-level page number to find the physical frame number (PFN) in the second-level page table.
  3. 将物理页框号与页内偏移结合,生成物理地址。
    Combine the physical frame number with the page offset to generate the physical address.

70. 多级页表的性能分析

多级页表的内存访问次数:
Memory Accesses in Multilevel Page Tables:
• 在四级页表系统中,地址转换可能需要 五次内存访问
◦ 四次访问各级页表。
◦ 一次访问物理内存以读取数据。
Five memory accesses in a four-level paging system: ◦ Four accesses to traverse the page tables. ◦ One access to read data from physical memory.**

性能瓶颈:
Performance Bottleneck:
• 多级页表的地址转换需要多次内存访问,可能导致性能下降。
Address translation in multilevel page tables requires multiple memory accesses, potentially degrading performance.


71. 倒排页表(Inverted Page Tables)的核心思想

定义:
Definition:
• 倒排页表是一种优化页表存储的方式,每个物理页框(Physical Page Frame)对应一个页表项(PTE),而不是为每个虚拟页分配一个页表项。
An inverted page table is an optimization for storing page tables, where each physical page frame has one PTE, rather than allocating one PTE for each virtual page.

工作原理:
How It Works:
1. 使用哈希函数将虚拟页号(Vpage)和进程 ID(pid)映射到倒排页表的索引。
A hash function maps the virtual page number (Vpage) and process ID (pid) to an index in the inverted page table.
2. 在倒排页表中查找对应的物理页框号(PFN)。
The corresponding physical frame number (PFN) is looked up in the inverted page table.


72. 线性倒排页表(Linear Inverted Page Tables)

定义:
Definition:
• 线性倒排页表是一种全局页表,每个物理页框对应一个页表项,记录该物理页框中存储的虚拟页号及其所属进程。
A linear inverted page table is a global page table where each physical page frame has one entry, recording the virtual page number stored in that frame and the owning process.

页表项的内容:
Contents of a Page Table Entry:
• 虚拟页号(Virtual Page Number)。
• 所属进程 ID(Process ID)。

地址转换过程:
Address Translation Process:
1. 使用哈希函数将虚拟页号和进程 ID 映射到页表索引。
Hash the virtual page number and process ID to map to a page table index.
2. 在页表中查找对应的物理页框号。
Search for the corresponding physical frame number in the page table.


73. 线性倒排页表的优缺点

优点:
Advantages:
1. 节省内存:
◦ 对于大地址空间,倒排页表的大小与物理内存大小成正比,而不是与虚拟地址空间大小成正比。
Memory efficient: For large address spaces, the size of the inverted page table is proportional to the physical memory size, not the virtual address space size.
2. 适合稀疏地址空间:
◦ 只存储实际使用的物理页框信息,避免为未使用的虚拟页分配页表项。
Suitable for sparse address spaces: Only stores information about used physical page frames, avoiding PTEs for unused virtual pages.

缺点:
Disadvantages:
1. 查找困难:
◦ 需要通过哈希查找或线性搜索找到对应的页表项,增加了地址转换的复杂性和延迟。
Difficult to search: Requires hash-based or linear search to find the corresponding PTE, increasing complexity and latency.
2. 哈希冲突问题:
◦ 哈希表可能发生冲突,需要额外的管理开销。
Hash collision issues: Hash tables may experience collisions, requiring additional management overhead.


76. 线性倒排页表的地址转换方案

地址转换流程:
Address Translation Flow:
1. 使用虚拟页号和进程 ID 计算哈希值,定位到倒排页表的索引。
Compute the hash value using the virtual page number and process ID to locate the index in the inverted page table.
2. 在页表中查找对应的物理页框号。
Search for the corresponding physical frame number in the page table.
3. 如果找到匹配项,则生成物理地址;否则触发页错误。
If a match is found, generate the physical address; otherwise, trigger a page fault.

img

77. 线性倒排页表的示例

假设条件:
Assumptions:
• 虚拟地址空间为 32 位,页大小为 4 KB。
Virtual address space is 32 bits, with a page size of 4 KB.
• 物理内存为 1 GB,包含 \(1 \text{ GB} / 4 \text{ KB} = 262,144\) 个物理页框。
Physical memory is 1 GB, containing \(1 \text{ GB} / 4 \text{ KB} = 262,144\) physical page frames.

倒排页表的大小:
Size of the Inverted Page Table:
• 倒排页表的大小为 262,144 个条目,远小于单级页表的 \(2^{20} = 1,048,576\) 个条目。
The inverted page table has 262,144 entries, much smaller than the \(2^{20} = 1,048,576\) entries of a single-level page table.


78. 哈希倒排页表(Hashed Inverted Page Tables)

定义:
Definition:
• 哈希倒排页表在倒排页表的基础上增加了一级哈希表,用于快速定位页表项。
A hashed inverted page table adds a hash table to quickly locate page table entries in the inverted page table.

工作原理:
How It Works:
1. 使用哈希函数将虚拟页号和进程 ID 映射到哈希表的索引。
Hash the virtual page number and process ID to map to an index in the hash table.
2. 在哈希表中查找对应的倒排页表项。
Search for the corresponding inverted page table entry in the hash table.

哈希冲突的处理:
Handling Hash Collisions:
• 使用链地址法(Chaining)解决哈希冲突:
◦ 在哈希表的每个条目中维护一个链表,存储所有映射到该索引的页表项。
Use chaining to handle hash collisions: Maintain a linked list in each hash table entry to store all PTEs mapped to that index.


79. 哈希倒排页表的查找过程

查找流程:
Lookup Process:
1. 使用虚拟页号和进程 ID 计算哈希值,定位到哈希表的索引。
Compute the hash value using the virtual page number and process ID to locate the hash table index.
2. 在哈希表中查找对应的倒排页表项:
◦ 比较进程 ID 和虚拟页号是否匹配。
Compare the process ID and virtual page number to check for a match.
◦ 如果不匹配,则检查链表中的下一个条目。
If no match, check the next entry in the linked list.


80. 哈希倒排页表的示例

假设条件:
Assumptions:
• 哈希表大小为 65,536,每个哈希表条目维护一个链表。
Hash table size is 65,536, with each entry maintaining a linked list.
• 每个链表平均包含 4 个页表项。
Each linked list contains an average of 4 PTEs.

查找性能:
Lookup Performance:
• 平均需要 \(1 + 4 = 5\) 次比较完成查找。
On average, 5 comparisons are required to complete a lookup.

img

81. 哈希倒排页表与传统页表的对比

特性 传统页表(Traditional Page Table) 哈希倒排页表(Hashed Inverted Page Table)
内存占用 与虚拟地址空间大小成正比,可能非常大。 与物理内存大小成正比,节省内存。
查找性能 每次地址转换需要多级页表查找,可能涉及多次内存访问。 使用哈希表加速查找,平均查找时间接近 \(O(1)\)
优点 - 支持稀疏地址空间。
- 实现简单。
- 内存效率高。
- 查找速度快。
缺点 - 内存占用大。
- 查找性能可能较差。
- 哈希冲突可能导致性能下降。
- 管理哈希表的开销较大。

82. 倒排页表的实际应用

应用场景:
Application Scenarios:
• 倒排页表目前在一些 IBM 和 Hewlett Packard 的工作站中使用。
Inverted page tables are currently used in some IBM and Hewlett Packard workstations.
• 随着 64 位机器的普及,倒排页表的应用将更加广泛。
With the widespread adoption of 64-bit machines, inverted page tables will become more common.

优点:
Advantages:
1. 高效的内存利用:
◦ 对于大地址空间,倒排页表显著减少了页表的存储需求。
Efficient memory utilization: Significantly reduces the storage requirements of page tables for large address spaces.
2. 适合稀疏地址空间:
◦ 只存储实际使用的物理页框信息,避免浪费内存。
Suitable for sparse address spaces: Only stores information about used physical page frames, avoiding memory waste.

缺点:
Disadvantages:
1. 查找复杂:
◦ 哈希查找或线性搜索可能增加地址转换的延迟。
Complex lookup: Hash-based or linear search may increase the latency of address translation.
2. 哈希冲突管理:
◦ 需要额外的开销来处理哈希冲突。
Hash collision management: Requires additional overhead to handle hash collisions.


总结

多级页表的核心思想:
• 通过分级存储页表,减少内存占用,支持大地址空间。
The core idea of multilevel page tables is to store page tables hierarchically, reducing memory usage and supporting large address spaces.

倒排页表的核心思想:
• 通过为每个物理页框分配一个页表项,减少页表的存储需求,适合大地址空间和稀疏地址空间。
The core idea of inverted page tables is to allocate one PTE for each physical page frame, reducing storage requirements and being suitable for large and sparse address spaces.

哈希倒排页表的优势:
• 使用哈希表加速查找,平均查找时间接近 \(O(1)\)
Hashed inverted page tables use hash tables to accelerate lookups, with average lookup times approaching \(O(1)\).

倒排页表的挑战:
• 哈希冲突管理和查找复杂性可能影响性能。
Hash collision management and lookup complexity may impact performance.

未来趋势:
• 随着硬件性能的提升,倒排页表和哈希倒排页表将更加高效,成为虚拟内存管理的主流技术。
With advancements in hardware, inverted and hashed inverted page tables will become more efficient and mainstream in virtual memory management.