1、存储管理 5.2 windows 本章内容 内存管理概述 Windows 2000/XP内存管理 Windows 2000/XP外存管理 Windows 2000/XP高速缓存管理 内存管理概述 5.4 windows Memory Management Overlays 覆盖 技术 Swapping 交换技术 Contiguous Allocation 连续分配 Paging 分页 Segmentation 分段 Segmentation with Paging 段页式 Demand Paging 请求页式 5.5 windows Background 存储器是计算机系统的重要组成部分,虽然
2、内存容量在不断扩大,但内存仍是宝贵资源,如何提高主存储器利用率,并扩充大主存,对主存信息实现有效保护是存储器管理主要任务,也是各种不同存储管理方法的目标 5.6 windows Background Logical address逻辑地址 (相对地址,虚地址):用户的程序经过汇编或编译后形成目标代码,目标代码通常采用相对地址的形式。 其首地址为 0,其余指令中的地址都相对于首地址来编址。 不能用逻辑地址在内存中读取信息。 Physical address物理地址 (绝对地址,实地址):内存中存储单元的地址。物理地址可直接寻址 5.7 windows Address binding Compil
3、e time (编译时期) : If memory location known a priori, absolute code can be generated; must recompile code if starting location changes. 如果内存位置已知,可生成绝对代码;如果开始位置改变,需要重新编译代码 Load time (装入时期) : Must generate relocatable code if memory location is not known at compile time.如果内存位置在编译时不知道,则必须生成 可重定位代码 Executi
4、on time (执行时期) : Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers). 如果进程在执行时可以在内存中移动,则地址绑定要延迟到运行时。需要硬件对地址映射的支持 MMU (例如基址和限长寄存器 ) 指令和数据地址绑定到内存地址可以在三个不同的阶段发生。 5.8 w
5、indows Multistep Processing of a User Program 5.9 windows Paging(分页) Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 8192 bytes). (把物理内存分成大小固定的 块 ,称为 帧 或页框 。) Divide logical memory into blocks of same size called pages. (把逻辑内存也分位固定大小的块,称为 页 。)
6、Set up a page table to translate logical to physical addresses. (设置一个 页表以便 把逻辑地址转换为物理地址。) 页表 page table列出了进程的逻辑页与其在主存中的物理帧间的对应关系 Internal fragmentation program, no external fragmentation. associative memory or translation look-aside buffers (TLBs) 联想寄存器(快表) 5.10 windows Address Translation Scheme(地址
7、变换机制) Address generated by CPU is divided into (CPU产生的地址被分为) : Page number (p) (页码) used as an index into a page table which contains base address of each page in physical memory.(它包含每个页在物理内存中的基址,用来作为页表的索引) Page offset (d) (偏移) combined with base address to define the physical memory address that is
8、 sent to the memory unit. (同基址相结合,用来确定送入内存单元的物理内存地址。) p d 31 Page number 12 11 page offset 0 地址结构 5.11 windows Address Translation Architecture(地址变换机构) 5.12 windows Paging Example 5.13 windows Page Table Structure Hierarchical Paging 分级页表 Hashed Page Tables 哈希页表 Inverted Page Tables 反向 (反置 )页表 5.14
9、windows Hierarchical Page Tables x86的逻辑地址空间有 232Byte,如页面大小为 4KB(212Byte),则页表项达 1M( 220)个,每个页表项占用 4Byte,故每个进程的页表占用 4MB内存空间,还要求是连续的,显然这是不现实的。 分解逻辑地址空间到多个页表 一个简单的技术是两级页表 在 x86中,为了减少页表所占用的连续的内存空间,采用了两级页表机制 。 Windows , Linux 。 5.15 windows Two-Level Paging Example A logical address (on 32-bit machine wit
10、h 4K page size) is divided into : a page number consisting of 20 bits. a page offset consisting of 12 bits. Since the page table is paged, the page number is further divided into : a 10-bit page number. (一个 10位的页号) a 10-bit page offset. (一个 10位的偏移) Thus, a logical address is as follows : where p1 is
11、 an index into the outer page table, and p2 is the displacement within the page of the outer page table. page number page offset p1 p2 d 10 10 12 5.16 windows Two-Level Page-Table Scheme x86两级页表系统将 32位逻辑地址空间的地址分成三段: 页表目录号(外层页号 p1)和页号(外层页内地址 p2)两项各占 10位,偏移量(页内地址 d)占 12位。 每页的大小为 4KB。 由于物理块号和页表的物理地址都占
12、4个字节,使每页中包含 1024个页表项,所以页表目录和页表的大小也都是 4KB,即一页。在 x86中设置了一个外层页表寄存器( CR3),用于存放页表目录的基址。 5.17 windows Two-Level Page-Table Scheme 5.18 windows Address-Translation Scheme Address-translation scheme for a two-level 32-bit paging architecture (一个两级 32位分页结构的地址转换机制) 5.19 windows Virtual Memory 局部性原理 (principle
13、 of locality):指程序在执行过程中的一个较短时期,所执行的指令地址和指令的操作数地址,分别局限于一定区域。还可以表现为: 时间局部性,即一条指令的一次执行和下次执行,一个数据的一次访问和下次访问都集中在一个较短时期内; 空间局部性,即当前指令和邻近的几条指令,当前访问的数据和邻近的数据都集中在一个较小区域内。 虚拟存储的基本原理 在程序装入时,不必将其全部读入到内存,而只需将当前需要执行的部分页或段读入到内存,就可让程序开始执行。 在程序执行过程中,如果需执行的指令或访问的数据尚未在内存(称为缺页或缺段),则由处理器通知操作系统将相应的页或段调入到内存,然后继续执行程序。 操作系统
14、将内存中暂时不使用的页或段调出保存在外存上,从而腾出空间存放将要装入的程序以及将要调入的页或段 具有请求调入和置换功能,只需程序的一部分在内存就可执行。 5.20 windows Demand Paging 在请求分页系统中的每个页表项如图所示: 状态位 P(存在位):用于指示该页是否已调入内存,供程序访问时参考。 访问字段 A:用于记录本页在一段时间内被访问的次数,或最近已有多长时间未被访问,提供给置换算法选择换出页面时参考。 修改位 M:表示该页在调入内存后是否被修改过。由于内存中的每一页都在外存上保留一份副本,因此,若未被修改,在置换该页时就不需将该页写回到外存上,以减少系统的开销和启动
15、磁盘的次数;若已被修改,则必须将该页重写到外存上,以保证外存中所保留的始终是最新副本。 外存地址 :用于指出该页在外存上的地址,通常是物理块号,供调入该页时使用。 5.21 windows Steps in Handling a Page Fault 5.22 windows Page Replacement 页面置换 在进程运行过程中,如果发生缺页,此时内存中又无空闲块时,为了保证进程能正常运行,就必须从内存中调出一页程序或数据送磁盘的对换区。但将哪个页面调出,则须根据一定的页面置换算法来确定。 . 功能:需要调入页面时,选择内存中哪个物理页面被置换。称为 replacement polic
16、y。 出发点:把未来不再使用的或短期内较少使用的页面调出,通常只能在局部性原理指导下依据过去的统计数据进行预测。 从理论上讲,应将那些以后不再被访问的页面换出,或把那些在较长时间内不会再被访问的页面换出 。 5.23 windows Page Replacement Algorithms First-In-First-Out Page Replacement (FIFO, 先进先出 页面 置换算法) Optimal Page Replacement (OPT, 最佳页面置换算法 ):选 择“未来不再使用的”或“在离当前最远位置上出现的”页面被置换。 Least Recently Used Pa
17、ge Replacement (LRU, 最近最久违用算法 ):选择内存中最久未使用的页面被置换。由于需要记录页面使用时间的先后关系,硬件开销太大。 LRU Approximation Algorithms 近似 LRU算法 (NRU) Additional-Reference-Bits Algorithm附加引用位算法 Second-Chance Algorithm二次机会算法( clock算法) Enhanced Second-Chance Algorithm增强二次机会算法(改进型的 clock算法) 5.24 windows Page Replacement Algorithms Co
18、unting-Base Page Replacement Least Frequently Used Algorithm (LFU最不常用算法 ) : replaces page with smallest count.最小计数的页被置换 Most Frequently Used Algorithm (MFU引用最多算法 ) : Page Buffering Algorithm页面缓冲算法 5.25 windows Optimal Page Replacement 9 Page faults 5.26 windows 帧 (页框 )分配 在采用固定分配策略时,可采用以下几种物理块分配方法: 平
19、均分配算法 按比例分配算法 考虑优先权的分配算法 在请求分页系统中,可采取两种分配策略 固定分配 和可变分配 策略。在进行置换时,也可采取两种策略 全局置换 (global replacement)和 局部置换 (local replacement) 。于是可组合成以下三种策略: 固定分配局部置换策略 可变分配全局置换策略 可变分配局部置换 5.27 windows 页面调入策略 为能使进程运行,必须事先将一部分要执行的程序和数据调入内存。 1. 调入页面的时机 为了将进程运行时所缺的页面调入内存,可采取策略有: 预调页策略 请求调页策略 2.从何处调入页面 在虚拟存储系统中,外存(硬盘)常常
20、被分成两部分; 文件区 (用于存放文件)和 对换区 (用于存放对换页面)。通常,对换区的磁盘 I/O速度比文件区要高。 每当进程发出缺页请求时,系统应从何处将缺页调入内存呢?在UNIX系统中 : 对于 从未运行过的页面 ,都应从硬盘文件区调入; 对于 曾经运行过而又被换出的页面 ,可以从对换区调入; 对于 共享页面 ,该页面可能已由其它进程调入内存,此时就无须再从对换区调入。 5.28 windows Thrashing ( 颠簸或抖动 ) Thrashing( 颠簸 ) a process is busy swapping pages in and out( 一个进程忙于页面的换入换出 )
21、. 5.29 windows Working-Set Model 工作集模型 working-set window(工作集窗口 ) a fixed number of page references Example: 10,000 instruction WSSi (working set of Process Pi) = total number of pages referenced in the most recent (varies in time) if too small will not encompass entire locality. if too large will encompass several localities. if = will encompass entire program. 5.30 windows Working-set model