770 likes | 938 Views
Part 3 Storage Management. Main Memory Management Virtual Memory. Chapter 9: Memory Management. 9.1 Background ( 背景) 9.2 Swapping ( 交换) 9.3 Contiguous Allocation ( 连续分配) 9.4 Paging ( 分页) 9.5 Segmentation ( 分段) 9.6 Segmentation with Paging ( 段页式). 9.1 Background.
E N D
Part 3 Storage Management Main Memory Management Virtual Memory
Chapter 9: Memory Management • 9.1 Background (背景) • 9.2 Swapping (交换) • 9.3 Contiguous Allocation (连续分配) • 9.4 Paging (分页) • 9.5 Segmentation (分段) • 9.6 Segmentation with Paging (段页式) Operating System Concepts
9.1 Background • Program must be brought into memory and placed within a process for it to be run.程序必需以一个进程的方式,装入内存才能被执行 • Input queue – collection of processes on the disk that are waiting to be brought into memory to run the program.输入队列— 磁盘上等待进入内存并执行的进程的集合 • User programs go through several steps before being run. 用户程序在执行之前必需经历很多步骤 Operating System Concepts
9.1.1 Binding of Instructions and Data to Memory Address binding of instructions and data to memory addresses can happen at three different stages. 指令和数据结合到内存地址可以在三个不同的阶段发生。 • Compile 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. 如果存储位置在编译时不知道,则必须生成可重定位代码 • Execution 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). 如果进程在执行时可以在内存中移动,则地址绑定要延迟到运行时。需要硬件对地址映射的支持,例如基址和限长寄存器) Operating System Concepts
Multistep Processing of a User Program Operating System Concepts
9.1.2 Logical vs. Physical Address Space • The concept of a logical address space that is bound to a separate physicaladdress space is central to proper memory management. 逻辑地址空间的概念同物理地址空间相关联,它是内存管理的中心。 • Logical address – generated by the CPU; also referred to as virtual address. (逻辑地址—由CPU产生;也叫做虚拟地址。) • Physical address – address seen by the memory unit. (物理地址—内存设备所读入的地址) • Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme. 逻辑和物理地址在编译时期和装入时期的地址绑定策略是相同的,而在执行时间的地址绑定策略是不同的。 Operating System Concepts
Memory-Management Unit (MMU) • Hardware device that maps virtual to physical address.硬件把虚拟地址映射到物理地址 • In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory.在MMU策略中,基地址寄存器中的值在其送入内存的时候被加入到由一个用户进程所产生的每个地址中。 • The user program deals with logical addresses; it never sees the real physical addresses. 用户程序所对应到的是逻辑地址,物理地址对它从来都不可见。 Operating System Concepts
Dynamic relocation using a relocation register Operating System Concepts
9.1.3 Dynamic Loading • Routine is not loaded until it is called 例程在被调用之前并不事先装入到内存中。 • Better memory-space utilization; unused routine is never loaded. 更好的内存空间利用率;没有被使用的例程不被载入。 • Useful when large amounts of code are needed to handle infrequently occurring cases. 当需要大量的代码来处理不经常发生的事情时是非常有用的。 • No special support from the operating system is required implemented through program design. 不需要操作系统的特别支持,通过程序设计实现。 Operating System Concepts
9.1.4 Dynamic Linking • Linking postponed until execution time. 链接被推迟到执行时期 • Small piece of code, stub, used to locate the appropriate memory-resident library routine. 小的代码片 - 存根,用来定位合适的保留在内存中的库程序。 • Stub replaces itself with the address of the routine, and executes the routine. 存根用例程地址来替换自己,以及执行例程。 • Operating system needed to check if routine is in processes’ memory address. 操作系统需要检查例程是否在进程的内存空间 • Dynamic linking is particularly useful for libraries. Operating System Concepts
9.1.5 Overlays • Keep in memory only those instructions and data that are needed at any given time.只是在内存中保留那些在特定时间所需要的指令和数据。 • Needed when process is larger than amount of memory allocated to it.当进程比所分配的内存大时,覆盖是必需的 • Implemented by user, no special support needed from operating system, programming design of overlay structure is complex 由用户执行,不需要操作系统的特别支持,覆盖结构的程序设计很复杂。 Operating System Concepts
Overlays for a Two-Pass Assembler Operating System Concepts
9.2 Swapping • A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution. 一个进程可以暂时被交换到内存外的一个备份区,随后可以被换回内存继续执行。 • Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images. 备份区—是一个固定的足够大的可以容纳所有用户内存映像的拷贝;对于可以让这些内存映像直接存取。 Operating System Concepts
Swapping (Cont.) • Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed. 滚入,滚出—交换由于基于优先级的算法而不同,低优先级的进程被换出,这样高优先级的进程可以被装入和执行。 • Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped.交换时间的主要部分是传输时间,总的传输时间直接同交换的内存的数量成比例。 • Modified versions of swapping are found on many systems, i.e., UNIX, Linux, and Windows 在许多系统如:UNIX,Windows中,可以找到一些被修正过的交换措施。 Operating System Concepts
Schematic View of Swapping(交换示意图) Operating System Concepts
9.3 Contiguous Allocation(连续分派) • Main memory usually into two partitions: 主存通常被分为两部分 • Resident operating system, usually held in low memory with interrupt vector. (为操作系统保留的部分,通常用中断矢量保存在内存低端。) • User processes then held in high memory. (用户进程保存在内存高端。) Operating System Concepts
Contiguous Allocation(CONT.1) • Single-partition allocation(单独分区分配) • Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data. (基址寄存器策略用来保护用户进程(同其他进程和改变的操作系统代码和数据分开。) • Relocation register contains value of smallest physical address; limit register contains range of logical addresses – each logical address must be less than the limit register. (基址寄存器包含最小物理地址的值;限长寄存器包含逻辑地址的范围,每个逻辑地址必需比限长寄存器的值小。) Operating System Concepts
Hardware Support for Relocation and Limit Registers Operating System Concepts
Contiguous Allocation (Cont.2) OS OS OS OS process 5 process 5 process 5 process 5 process 9 process 9 process 8 process 10 process 2 process 2 process 2 process 2 • Multiple-partition allocation (多分区分配) • Hole – block of available memory; holes of various size are scattered throughout memory. (分区—可用的内存块,不同大小的分区分布在整个内存中。) • When a process arrives, it is allocated memory from a hole large enough to accommodate it. (当一个进程到来的时候,它将从一个足够容纳它分区中分配内存。) • Operating system maintains information about: (操作系统包含以下信息)a) allocated partitions (已分配的分区) b) free partitions (hole) (空闲的分区) Operating System Concepts
固定分区(大小相同) 固定分区(多种大小) Operating System Concepts
优点:易于实现,开销小。 • 缺点: • 内碎片造成浪费 • 分区总数固定,限制了并发执行的程序数目。 • 可以和覆盖、交换技术配合使用。 • 采用的数据结构:分区表--记录分区的大小和使用情况 Operating System Concepts
9.3.2 Dynamic Storage-Allocation Problem(动态存储分派问题) How to satisfy a request of size n from a list of free holes. (怎样从一个空的分区序列中满足一个申请需要。) • First-fit (首先适应): Allocate the first hole that is big enough. (分配最先找到的合适的分区。) • Best-fit (最佳适应) : Allocate the smallest hole that is big enough; must search entire list, unless ordered by size. Produces the smallest leftover hole. (搜索整个序列,找到适合条件的最小的分区进行分配。) • Worst-fit (最差适应) : Allocate the largest hole; must also search entire list. Produces the largest leftover hole.搜索整个序列,寻找最大的分区进行分配。) First-fit and best-fit better than worst-fit in terms of speed and storage utilization. (在速度和存储的利用上,首先适应和最佳适应要比最差适应好。) Operating System Concepts
9.3.3 Fragmentation(碎片) • External Fragmentation (外碎片) – total memory space exists to satisfy a request, but it is not contiguous. (整个内存空间用来满足一个请求,但它不是连续的。) • Internal Fragmentation (内碎片)– allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. (分配的内存可能比申请的内存大一点,这两者之间的差别是内部不被使用的簇) • Reduce external fragmentation by compaction (通过压缩来减少外碎片) • Shuffle memory contents to place all free memory together in one large block. (把一些小的空闲内存结合成一个大的块。) • Compaction is possible only if relocation is dynamic, and is done at execution time.只有可以动态重定位的时候,才有可能进行压缩,压缩在执行时期进行) • I/O problem (I/O问题) • Latch job in memory while it is involved in I/O. (当I/O的时候,把工作锁定在内存中。) • Do I/O only into OS buffers. (只对操作系统的缓冲区进行I/O。) Operating System Concepts
9.4 Paging(分页) • Physical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available. (进程的物理地址空间可能是不连续的,如果有可用的物理内存,它将分给进程。) • 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. (把逻辑内存也分为固定大小的块,叫做页。) • Keep track of all free frames. (保留一个页的记录。) • To run a program of size n pages, need to find n free frames and load program. (运行一个有N页大小的程序,需要找到N个空的页架读入程序。) • Set up a page table to translate logical to physical addresses. (建立一个页表,把逻辑地址转换为物理地址。) • Internal fragmentation. (内碎片。) Operating System Concepts
Address Translation Scheme(地址变换方案) • Address generated by CPU is divided into (CPU产生的地址被分为) : • Page number(p) (页号) – used as an index into a pagetable 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 sent to the memory unit. (同基址相结合,用来确定送入内存设备的物理内存地址。) Operating System Concepts
Address Translation Architecture (地址变换机构) Operating System Concepts
优点: • 没有外碎片,每个内碎片不超过页大小。 • 一个程序不必连续存放。 • 便于改变程序占用空间的大小。即随着程序运行而动态生成的数据增多,地址空间可相应增长。 • 缺点:程序全部装入内存。 Operating System Concepts
Paging Example Operating System Concepts
Paging Example Operating System Concepts
Free Frames Before allocation After allocation Operating System Concepts
9.4.2 Implementation of Page Table(页表实现) • Page table is kept in main memory. (主存中的页表) • Page-tablebase register (PTBR) points to the page table. (页表基址寄存器指向页表) • Page-table length register (PTLR) indicates size of the page table. (页表限长寄存器表明页表的长度) • In this scheme every data/instruction access requires two memory accesses. One for the page table and one for the data/instruction. (在这个机制中,每一次的数据/指令存取需要两次内存存取,一次是存取页表,一次是存取数据) • The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers(TLBs)(通过一个联想寄存器,可以解决两次存取的问题) Operating System Concepts
Associative Memory Page # Frame # • Associative memory – parallel search(联想寄存器—并行查找) Address translation (A´, A´´) (地址转换) • If A´ is in associative register, get frame # out. (如果A’在联想寄存器中,把页框#取出来。) • Otherwise get frame # from page table in memory (否则从内存中的页表中取出页框#。) Operating System Concepts
Paging Hardware With TLB Operating System Concepts
Effective Access Time • Associative Lookup = time unit(联想寄存器的查找需要时间) • Assume memory cycle time is 1 microsecond (假设内存一次存取要1微秒) • Hit ratio – percentage of times that a page number is found in the associative registers; ration related to number of associative registers. (命中率—在联想寄存器中找到页号的比率,比率与联想寄存器的大小有关。) • Hit ratio = • Effective Access Time (EAT)(有效存取时间) EAT = (1 + ) + (2 + )(1 – ) = 2 + – Operating System Concepts
Effective Access Time • Example: • Hit ratio = 80% • Take 20ns to access TLB • Take 100ns to access page table or memory • Then EAT = (20+100)x0.8 + (20+100+100)x0.2 = 140ns • If Hit ration=98% EAT = (20+100)x0.98 + (20+100+100)x0.0.02 = 122ns Operating System Concepts
9.4.3 Memory Protection(存储保护) • Memory protection implemented by associating protection bit with each frame.(内存的保护由与每个页框相连的保护位来执行。) • Valid-invalid bit attached to each entry in the page table (有效-无效位附在页表的每个表项中) : • “valid” indicates that the associated page is in the process’ logical address space, and is thus a legal page. (“有效”表明相关的页在进程的逻辑地址空间,以及是一个合法的页。) • “invalid” indicates that the page is not in the process’ logical address space. (“无效”表明页不在进程的逻辑地址空间中。) Operating System Concepts
Valid (v) or Invalid (i) Bit In A Page Table Operating System Concepts
页的大小 • 通常是:几KB到几十KB。 • 小->内碎片小;大->页表短,管理开销小,交换时对外存I/O效率高。 • 和目前计算机的物理内存大小有关:4MB到256MB,不太大。 • 页表长度寄存器(PTLR):指出页表的大小 Operating System Concepts
9.4.4 Page Table Structure • Hierarchical Paging 分级页表 • Hashed Page Tables 哈希页表 • Inverted Page Tables 反向页表 Operating System Concepts
9.4.4.1 Hierarchical Page Tables • Break up the logical address space into multiple page tables. 分解逻辑地址空间到多个页表 • A simple technique is a two-level page table. 一个简单的技术是两级页表 Operating System Concepts
Two-Level Paging Example page number page offset p2 pi d 10 12 10 • A logical address (on 32-bit machine with 4K page size) is divided into (一个逻辑地址被分为) : • a page number consisting of 20 bits. (一个20位的页号) • a page offset consisting of 12 bits. (一个12位的偏移) • 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 pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table. Operating System Concepts
Two-Level Page-Table Scheme Operating System Concepts
Address-Translation Scheme • Address-translation scheme for a two-level 32-bit paging architecture (一个两级32位分页结构的地址转换机制) Operating System Concepts
9.4.4.2 Hashed Page Tables • Common in address spaces > 32 bits. 公用地址空间大于32位 • The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location. 虚拟页的数值被散列到一个页表中,这个页表包含散列到相同地址的一根链指针。 • Virtual page numbers are compared in this chain searching for a match. If a match is found, the corresponding physical frame is extracted. 通过这个链搜索匹配虚页数值,如果匹配成功,取出相应的物理页框。 Operating System Concepts
Hashed Page Table Frame number Page number Operating System Concepts
9.4.4.3 Inverted Page Table • One entry for each real page of memory. (一个内存中页的表项。) • Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page. 表项包含真正内存地址的页的虚拟地址,它包括拥有这个页的进程的信息。 • Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs. 减少内存需要储存每个页表,但是当访问一个页时,寻找页表需要增加时间。 • Use hash table to limit the search to one — or at most a few — page-table entries.(使用哈希表来减少搜索。) Operating System Concepts