操作系统(8):内存管理

背景

  • CPU只能,访问到寄存器和内存
  • CPU存在L1,L2,L3的cache,L4实际上指的是主存

Menmory Hierarchy

在这里插入图片描述

Base&Limit Reg

对每个进程,有一对Base和Limit寄存器,确定其逻辑地址空间的起始点和上限长度(而不是结束地址)。

不同形式的地址

  • symbolic addr:程序中用符号表示的地址
  • Relocatable addr:编译器将符号地址绑定到可重定位地址,他们是针对某个基准的相对地址
  • Absolute addr:一般来说, B a s e ≤ A A ≤ B a s e + l i m i t Base\leq AA\leq Base+limit BaseAABase+limit

指令和数据的绑定

集中绑定方式:

  • Compile time(编译时刻): If memory location known a priori, absolute code can be generated; must recompile code if starting location changes (是一种非常落后的,基本上不可取的绑定方式)
  • Load time(装入时刻): 编译时不知道地址空间,编译器必须生成relocatable code,在加载时确定物理地址
  • Execution time(执行时刻): 地址绑定被推迟到运行时(runtime),从而使得程序能够从一个内存段转移到另一个内存段,这能够提供很大的灵活性,但需要硬件提供地址映射的支持。 (e.g., base and limitregisters)。,大部分现代操作系统采取这种形式。

逻辑地址空间和物理地址空间

The concept of a logical address space that is bound to a separate physical address space is central to proper memory management

  • Logical address :CPU产生,也被称为虚地址(virtual addr)
  • Physical address:内存管理单元可见和使用的地址
  • 逻辑地址和物理地址对编译时绑定加载时绑定的地址绑定模式来说完全相同。
  • 运行时绑定的绑定模式中,逻辑地址可能会被映射到不同的物理地址,所以二者不尽相同

MMU(内存管理单元)

例子:在这里插入图片描述

  • 是位于CPU和物理内存中间的硬件
  • 提供从虚拟内存到物理内存的映射
  • 简单的MMU映射将虚拟地址加上Base寄存器从而生成物理地址
  • 它使得用户程序永远只处理逻辑地址空间,而无法接触到物理内存

动态链接(dynamic linking)

和静态链接(static linking)对应,需要操作系统的支持

  • 链接操作被延迟到程序运行时(execution time)。其中
    • 被称为stub(存根)的小段代码被用于locate the appropriate memory-resident library routine
    • 存根会检查程序是否在内存中,否则加载,它会用程序地址替换自己并开始执行程序。
  • 动态链接对编写库十分有用:
    • 节省主存空间
    • 节省静态存储(.exe映像)的空间
    • 不需要重新链接库
  • 在windows中是.dll,在linux中是.so(shared object)。

动态加载(dynamic loading)

  • 程序只有在调用时才加载routine
  • 可以借此获得更好的内存空间利用率(未使用的routine不会被加载),但没有获得操作系统的支持,因为实现难度较高

交换

标准交换

以磁盘作为备份存储,系统维护一个所有可运行进程的ready queue,交换的代价高,所以知道用户真正使用的内存可以减少交换次数和时间,具有动态内存需求的进程需要通过系统调用通知OS内存需求变化情况。

连续相邻内存的分配(Contiguous Memory Allocation)

主存一半被分为两部分

  • 寄主操作系统通常占据低地址的内存,带有中断向量??
  • 用户进程在高地址内存
    ……………………

硬件地址保护的简单模式
在这里插入图片描述

multiple-partition allocation

  • 内存中有hole:一块可用的连续地址
  • 进程到来的时候,在能装下自己的hole中分配地址
  • 操作系统维持已分配的分区(allocated partition)空闲分区(free partition),也即hole的信息
    在这里插入图片描述

动态内存分配的策略

  • 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

显然worst-fit在时间和利用率上至少有一点不如前

分段

逻辑地址空间由一组端构成,每个段有名称和长度,地址制定了段名称和段内偏移。是<段号,偏移>

分页(paging)与页表(Page Table)

  • 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 8,192 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

地址翻译模式

地址被分为页号(高位)和页偏移(低位)。页号用于在页表中索引物理地址,页偏移不变,和页表中物理地址高位(frame)组合成物理地址
在这里插入图片描述

硬件支持

在这里插入图片描述

硬件实现

页表存于主存

页表由一对reg指示:PageTable Base Register(PTBR)和PageTable Length Register(PTLR)。但是这样的机制需要访问2次主存(第一次查页表),所以用TLB(Translation Look-aside Buffers)做缓冲
在这里插入图片描述
硬件机制将输入的页号并行比较,输出匹配的帧号(Frame),如无匹配则是无效输出。不匹配时,再从页表查询,并且根据一定机制进行TLB的替换。

Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely identifies each process to provide address-space protection for that process

TLB的效率估计

令TLB查询时间为 ε \varepsilon ε个内存时间单位(远小于1),令命中率为 α \alpha α,则
Effective Access Time(EAT)=

页表中的内存保护

使用每个页表项使用valid bit标记

  • “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

共享页(Shared Pages)

Shared code

  • One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems).
  • Shared code must appear in same location in the logical address space of all processes

Private code and data

  • Each process keeps a separate copy of the code and data
  • The pages for the private code and data can appear anywhere in the logical address space

分级页表(Hierarchical Page Tables)

在这里插入图片描述
在这里插入图片描述
但显然,每多一级就多一次memory-access

哈希页表(Hashed Page Tables)

常用于地址空间大于32bit的内存系统

虚页表号被哈希函数映射到一个页表,并且使用separate chaining+链表处理重复。
在这里插入图片描述

倒排页表(Inverted Page Table)

在这里插入图片描述

整个OS只有1个页表

分段(Segmentation)

将内存分段,每段代表用户程序的一部分,如:
在这里插入图片描述
在这里插入图片描述
逻辑地址需要维护:<段号,段内偏移>,从用户视图角度出发

段表项有2项,base表示段起始地址,limit表示这个段的长度,首先比较limit和offset,合理后再链接base和offset形成物理地址:

还有一定的保护措施:
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值