【操作系统】Main Memory

来源:互联网 发布:c语言网络编程 编辑:程序博客网 时间:2024/06/05 09:21

chapter 8 Main Memory

1、基本概念

(1)逻辑地址和物理地址。

(2)地址绑定:从逻辑地址到物理地址的映射.可能发生在三个时候:

    ①compile time

    ②load time

    ③execution time

(3)MMU:把虚拟地址映射成物理地址的硬件设备(用户程序只关心逻辑地址,不会直接和物理地址打交道)。

(4)动态重定位

(5)动态装入

(6)动态链接

 

2、交换技术

(1)backing store

备份存储。问题:碎片(fragmentation)。

 

3、连续分配

(1)多分区分配:固定分区和动态分区。

(2)动态分配的策略:

    ①first-fit:先来的先装。

    ②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.

    ④next fit:类似首次适应,每次分区时,总是从上次查找结束的地方开始,只要找到一 个足够大的空白区,就把它划分后分配出去。

(3)碎片

    ①内碎片:wasted memory within a single process memory space.

    ②外碎片:can reduce the number of runnable processes。使用紧缩(compaction)和拼接(defragmentation)处理外碎片。

补充:单一连续分配没有外碎片,但是有内碎片,适用于单用户。固定分区分配没有外碎片,但是不能实现多个程序共享主存。动态分配:有外碎片,需要动态重定位寄存器的支持。

书上的multiple partition指的是固定分区的分配。


4、分页

物理内存被分成了大小固定的frame(物理块、页框)。采用分页,逻辑地址可以不连续。

(1)physical memory分成frame(物理块)。

(2)logical memory分成page(页)。

(3)页表:逻辑映射成物理。

分页的方式:cpu产生的逻辑地址分成两部分:页号和页偏移。页号用来在页表中查找基地址,这个基地址拼上页偏移得到物理地址(注:这个基地址就是frame的编号。如果一个frame是4byte,那么物理地址=f*4+d)。

(4)分页可能有内碎片,但是没有外碎片。

(5)PPT的计算题:

4k=1000H,2362H/1000H=2H,2是p,后面的是d。254拼上362得到254362H。同理1565H会得到p=1,102拼上565得到102565H(也就是说102还要乘上页大小1000H)。

(6)页表放在主存里。

(7)TLB(associative memory)

TLB解决了两次访问内存的问题。这里有一个概念叫做"有效访问时间"(EAT)。

EAT=(内存访问时间+TLB访问时间)*命中率+(2*内存访问时间+TLB访问时间)*(1-命中率)

(8)页表的结构

①分级页表(hierarchical page tables)

a、linux是分了4级,windows分了2级。

②反向页表

a、用pid来遍历。

b、特点是降低了需要的存储空间,但是时间开销大。

③哈希页表

a、逻辑地址的p通过hash函数映射到hash table

b、工作方式:逻辑地址被hash到hash table对应的entry,每个entry里有三个field:第一个是virtual page number,第二个是page frame的值,第三个是指向下一个entry的指针。如果hash值和virtual page number匹配,取出field 2与offset形成物理地址,否则通过指针移动到下一个entry。

c、有此派生出的方案有clustered page table,适用于地址空间分布稀疏的情况。

④例题

某计算机采用二级页表的分页存储管理方式,按字节编址,页大小为2^10 字节,页表项大小为2字节,逻辑地址结构为:

页目录号 页号 页内偏移量

逻辑地址空间大小为2^16 页,则表示整个逻辑地址空间的页目录表中包含表项的个数至少是____128

解:

2^10/2=2^9,得到第二级有多少个页,2^16/2^9=2^7

 

6、分段(segmentation)

没细讲。

分段的动机是在用户层面方便内存管理。用户可以通过一个二元组(段号,偏移)来指定一个逻辑地址。这个地址可以通过段表映射成物理地址,段表有两项:limit和base,base是基地址,limit是从基地址开始最大能偏移的长度。

 

7、段页式存储

书上没讲。自求多福。

。。。话说可以看后面的补充题。

 

———————————————————————————————————————习题:

1. Given memory partitions of 100K, 500K, 200K, 300K, and 600K (in order), how would each of First-Fit, Best-fit, and Worst-fit algorithms place processes of 212K, 417K, 112K, and 426K (in order)? Which algorithm makes the most efficient use of memory?

解答:

 

2.Compare the main memory organization schemes of contiguous-memory allocation, pure segmentation, and pure paging with respect to the following issues:

    a. external fragmentation

    b. internal fragmentation

    c. ability to share code across processes

解答:

 

3.Consider a paging system with the page table stored in memory.

    a. If a memory reference takes 200 nanoseconds, how long does a paged memory reference take?

    b. If we add associative registers, and 75 percent of all page-table references are found in the associative registers, what is the effective memory reference time? (Assume that finding a page-table entry in the associative registers takes zero time, if the entry is there.)

 

4.

 

5. Considering the segment table, what are the physical addresses for the following logical addresses?

    Segment       Base  Length

0         219      600

1         2300    14

2         90        100

3         1327     580

4         1952     96

What are the physical addresses for the following logical addresses?

a. 0,430

b. 1,10

c. 2,500

d. 3,400

e. 4,112