您的位置:首页 > 其它

Chapter 9 Memory Management

2017-01-01 11:11 148 查看
Program must be brought into memory and placed within a process for it to be executed
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). 

重定位
Logical address

Generated by the 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.

地址重定位:把程序装入与其地址空间不一致的物理空间,所引起的一系列地址变换过程
静态地址重定位

装入一个作业时,把作业中的指令地址全部转换为绝对地址

动态地址重定位

在程序执行过程中,在CPU访问内存之前,将要访问的程序或数据地址转换成内存地址

Overlays
Keep in memory only those instructions and data that are needed at any given time

Swapping
A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution

存储管理方式
连续分配方式:为一个程序分配一段连续的内存空间

单一连续区管理方式
多分区管理方式,是一种可用于多道程序的较简单的存储管理方式

固定分区方式

作业装入之前,内存就被划分成若干个固定大小的连续分区
一旦划分完成,在系统允许期间不再重新划分,即分区的个数不可变 -----> 静态分区

分区大小相等

适用于多个相同程序的并发执行,缺乏灵活性

分区大小不等

多个小分区,适量的中等分区,少量的大分区

优点

易于实现
开销小

缺点

分区大小固定

内碎片

分区总数固定

限制并发执行的进程数目

可变分区方式

寻找某个空闲分区,其大小需大于或等于程序的要求。若是大于要求,则将该分区分割成两个分区,其中一个分区为要求的大小并标记为“占用”,而另一个分区为余下部分并标记为“空闲”。分区的先后次序通常是从内存低端到高端。
分区释放算法:需要将相邻的空闲分区合并成一个空闲分区。(这时要解决的问题是:合并条件的判断)
First-fit

目的在于减少查找时间
特点

分配和释放的时间性能较好,较大的空闲分区可以被保留在内存高端。
随着低端分区不断划分而产生较多小分区,每次分配时查找时间开销会增大。
在系统不断地分配和回收中,必定会出现一些不连续的小的空闲区,称为外碎片。虽然可能所有碎片的总和超过某一个作业的要求,但是由于不连续而无法分配。

Best-fit

为提高查找效率,空闲分区表(空闲区链)中的空闲分区要按从小到大进行排序,自表头开始查找到第一个满足要求的自由分区分配
特点

从个别来看,外碎片较小,但从整体来看,会形成较多无法利用的碎片
较大的空闲分区可以被保留

Worst-fit
可变式分区的碎片问题

在系统不断地分配和回收中,必定会出现一些不连续的小的空闲区,称为外碎片。虽然可能所有碎片的总和超过某一个作业的要求,但是由于不连续而无法分配。

解决碎片的方法是拼接(或称紧凑),即向一个方向(例如向低地址端)移动已分配的作业,使那些零散的小空闲区在另一方向连成一片。
分区的拼接技术,一方面要求能够对作业进行动态重定位,另一方面系统在拼接时要耗费较多的时间。

Fragmentation
External fragmentation
Internal fragmentation
Reduce external fragmentation by compaction

Compaction is possible only if relocation is dynamic, and is done at execution time

Paging:避开了连续性的要求,允许进程的物理地址空间不连续
Logical 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.
Set up a page table to translate logical to physical addresses. 
Internal fragmentation
Address translation scheme

Address generated by CPU is divided into:
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 sent to the memory unit.
For given logical address space 2^m and page size 2^n

程序经过编译链接后形成逻辑地址,对某一特定机器其地址结构是一定的。若给定逻辑地址A,页面大小L,则页号P和页内地址W

P = A / L
W = A mod L

Implementation of page table
Page table is kept in main memory.
Page-table
base register (PTBR) points to the page table.
Page-table length register
(PRLR)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 registers or translation look-aside buffers (TLBs). 

Effective access time = (t+δ)α + (2t+δ)(1 - α)

Where assuming that memory cycle time is t ms, hit ratio is α and accociate lookup time is δ

基本的地址变换机构:将用户程序中的页号变换成内存中的物理块号
按页的大小分离出页号和位移量,放入有效地址寄存器中
将页号与页表长度进行比较,如果页号大于页表长度,越界中断;
以页号为索引查找页表:将页表始址与页号和页表项长度的乘积相加,便得到该表项在页表中的位置,于是可从中得到该页的物理块号;
将该物理块号装入物理地址寄存器的高址部分;
将有效地址寄存器中的位移量直接复制到物理地址寄存器的低位部分,从而形成内存地址。

页式地址变换
虚地址以16/8/2进制给出

将虚地址转换成二进制的数
按页的大小分离出页号和位移量
将位移量直接复制到内存地址寄存器的低位部分
以页号查页表,得到对应页装入内存的块号,并将块号转换成二进制数填入地址寄存器的高位部分,从而形成内存地址

虚地址以十进制数给出

页号=虚地址%页大小
位移量=虚地址 mod 页大小
以页号查页表,得到对应页装入内存的块号
内存地址=块号×页大小+位移量

两种方式其实是等价的

Thinking...(

Memory protection

Two-level page-table scheme

Multi-level paging and performance
Since each level is stored as a separate table in memory...
n-level page needs (n+1) times accessing memory

Hashed page table
Inverted page table
Shared code must appear in the same location in the logical address space of all processes

纯分页的特点
没有外碎片,每个内碎片不超过页大小
一个程序不必连续存放
程序全部装入内存

Segmentation
Logical address consists of a two tuple<segment-number, offset>
Segment table: maps two-dimensional physical addresses; each table entry has

base

contains the starting physical address where the segments reside in memory.

limit

specifies the length of the segment.

Segment-table base register (STBR): points to the segment table’s location in memory.
Segment-table length register (STLR): indicates number of segments used by a program;

segment number s is legal if s < STLR.

分段的特点
程序通过分段划分为多个模块,如代码段、数据段、共享段

可以分别编写和编译
可以针对不同类型的段采取不同的保护
可以按段为单位来进行共享,包括通过动态链接进行代码共享

特点

没有内碎片,外碎片可以通过内存紧缩来消除
便于改变进程占用空间的大小
进程全部装入内存

分页和分段的比较

 分 页
分 段
[align=left]目的[/align]
[align=left]为了提高内存的利用率;[/align]
[align=left]为了能更好地满足用户的需要。[/align]
[align=left]页/段单位划分[/align]
[align=left]页是信息的物理单位,页的大小是固定的,而且由系统确定。[/align]
[align=left]段是信息的逻辑单位,它含有一组意义相对完整的信息。段的长度是不固定的,取决于用户所编写的程序,并由编译程序来划分。[/align]
[align=left]作业地址空间[/align]
[align=left]单一的线性地址空间[/align]
[align=left]二维的,标识一个地址需给出段名和段内地址。[/align]
内存分配
[align=left]以页为单位离散分配,无外碎片,所以也无紧缩问题[/align]
[align=left]以段为单位离散分配,类同可变分区,会产生许多分散的小自由分区——外碎片,造成主存利用率低,需采用紧缩解决碎片问题,但紧缩需花费机时[/align]
Segmentation with paging
既具有分页系统能有效地提高内存利用率的优点,又具有分段系统能很好地满足用户需要的长处,是一种有效的存储管理方式
基本原理

将整个主存划分成大小相等的物理块(页框)
把用户程序按程序的逻辑关系分为若干个段,并为每个段赋予一个段名
把每个段划分成若干页,以页框为单位离散分配。
地址结构由段号、段内页号和页内地址三部分组成

需同时配置段表和页表
由于将段中的页进行离散的分配,段表中的内容不再是段的内存始址和段长,而是页表始址和页表长度

在段页式系统中,为了获取一条指令或数据,需三次访问内存。

第一次是访问内存中的段表,从中取得页表始址
第二次是访问内存中的页表,从中取得物理块号,并将该块号与页内地址一起形成物理地址
第三次才是真正从第二次访问所得到的物理地址中,取得指令或数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  操作系统