操作系统 内存管理-虚拟内存

虚拟内存:

Virtual memory – separation of user logical memory from physical memory.
1.Only part of the program needs to be in memory for execution
2.Logical address space can therefore be much larger than physical address space
3.Allows address spaces to be shared by several processes
4.Allows for more efficient process creation


 Virtual memory can be implemented via:
1.Demand paging

 Bring a page into memory only when it is needed
1.Less I/O needed
2.Less memory needed
3.Faster response
4.More users
 Page is needed  reference to it
1.invalid reference  abort
2.not-in-memory  bring to memory

页面出错的处理方案:Page Fault

If there is a reference to a page, first reference to that page will trap to operating system: page fault
1. Operating system looks at another table to decide:
    Invalid reference -- abort
     Just not in memory
2. Get empty frame
3. Swap page into frame
4. Reset tables
5. Set validation bit = v
6. Restart the instruction that caused the page fault

 Page Fault Rate 0  p  1.0
1. if p = 0 no page faults
2. if p = 1, every reference is a fault
 Effective Access Time (EAT)
EAT = (1 – p) x memory access+ p (page fault overhead+ swap page out+ swap page in+ restart overhead)

页面置换算法:

基本过程:Basic Page Replacement

1. Find the location of the desired page on disk
2. Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a victim frame
3. Bring the desired page into the (newly) free frame; update the page and frame tables
4. Restart the process

算法1:替换策略-先进先出(FIFO)

算法:总是淘汰最先调入主存的那一页,或者说在主存中驻留时间最长的那一页(常驻的除外)。
理由:最早调入内存的页面,其不再被访问的可能性最大。

特点:实现简单、适合线性访问、对其他情况效率不高

 异常情况:在某些情况下会出现分配给进程的页面数增多,缺页次数反而增加的奇怪现象。

算法2:替换策略-最佳替换(OPT)

算法:调入一页而必须淘汰一个旧页时,所淘汰的页应该是以后不再访问的页或距现在最长时间后再访问的页。
 特点:不是实际可行的算法,可用来作为衡量各种具体算法的标准,具有理论意义。

算法3:替换策略-最近最少使用(LRU)

算法:淘汰的页面是在最近一段时间里较久未被访问的那页。
原理:根据程序局部性原理,那些刚被使用过的页面,可能马上还要被使用,而在较长时间里未被使用的页面,可能不会马上使用到。

算法4:Counting Algorithms

 Keep a counter of the number of references that have been made to each page
 LFU Algorithm: replaces page with smallest count
 MFU Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used

算法5:时钟策略

1.每一个帧关联一个附加位,称为使用位;
2.页第一次调入内存,使用位设为1;
3.当该页被随后访问到时,使用位也被置为1;
4.当置换页发生时,查找使用位为0的一帧;
5.当置换检索过程中,遇到的使用位为1的帧,操作系统将其置为0;

多个进程在内存中运行,但一个进程需要更多页载入,而规定空间内剩余空间不够时:

1.Global replacement
process selects a replacement frame from the set of all frames; one process can take a frame from another
2.Local replacement
each process selects from only its own set of allocated frames


系统颠簸:Thrashing

 If a process does not have “enough” pages, the page-fault rate is very high. This leads to:
1.low CPU utilization
2.operating system thinks that it needs to increase the degree of multiprogramming
3.another process added to the system
 Thrashing : a process is busy swapping pages in and out

思考题:

 Why does demand paging work?
Locality model
1. Process migrates from one locality to another
2. Localities may overlap
 Why does thrashing occur?
 size of locality > total memory size

2.Demand segmentation


虚拟内存的两个特点:
1.进程中所有存储器访问都是逻辑地址,这些逻辑地址在运行时被转换为物理地址;
2.一个进程可以划分为许多块,在执行过程中,这些块不需要连续地位于主存中


Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory
If either process modifies a shared page, only then is the page copied
 COW allows more efficient process creation as only modified pages are copied
 Free pages are allocated from a pool of zeroed-out pages

虚拟内存的两个效果:
 在主存中保留多个进程:
 由于对任何特定的进程都仅仅装入它的某些块,因此就有足够的空间放置更多进程。
 程可以比主存的全部空间还大: 通过分段或分页的虚拟内存,程序员可以获得巨大的主存,大小与磁盘储存器相关。

进在具有层次结构存储器的计算机系统中,采用自动实现部分装入和部分对换功能,为用户提供一个比物理主存容量大得多的,可寻址的一种“主存储器”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值