Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?

Similar presentations


Presentation on theme: "Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?"— Presentation transcript:

1 Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?

2 Thrashing If a process does not have “enough” pages, the page-fault rate is very high. This leads to: low CPU utilization OS scheduler thinks that it needs to increase the degree of multiprogramming  new processes are added  even less pages for each process … Thrashing  a process is busy swapping pages in and out  Spending more time paging than executing.

3 Transfer of a Paged Memory to Contiguous Disk Space
Demand Paging Demand paging: pages are only loaded into memory when they are demanded during execution Less I/O needed Less memory needed Higher degree of multiprogramming Faster response Pager (lazy swapper) never swaps a page into memory unless that page will be needed. An extreme case: Pure demand paging starts a process with no pages in memory … Transfer of a Paged Memory to Contiguous Disk Space

4 Page Replacement Algorithms
Page fault forces choice which page must be removed make room for incoming page Modified page must first be saved unmodified just over written Better not to choose an often used page will probably need to be brought back in soon 页面淘汰算法的好坏直接决定了系统的性能。

5 Page Replacement Algorithms
Random page replacement Optimal page replacement algorithm Not recently used page replacement First-In, First-Out page replacement Second chance page replacement Clock page replacement Least recently used page replacement Working set page replacement WSClock page replacement Random page replacement Choose a page randomly 随机 最优 最近未使用 FIFO 第二次机会 时钟 最近最少使用 工作集 工作集时钟 每次页面失效时,尽管可以随机选择一个页面淘汰,但是选择不常使用的页面会使系统性能提升,反之 ,如果一个经常使用的页面被淘汰,很有可能很快又被调入,引起不必要的开销。

6 Optimal Page Replacement Algorithm
Replace page needed at the farthest point in future Optimal but unrealizable Estimate by … logging page use on previous runs of process although this is impractical

7 Optimal Example 12 references, 7 faults

8 Not Recently Used Page Replacement Algorithm(NRU)
Each page has Reference bit, Modified bit bits are set when page is referenced, modified Pages are classified not referenced, not modified not referenced, modified referenced, not modified referenced, modified NRU removes page at random from lowest numbered non empty class

9 FIFO Page Replacement Algorithm
Maintain a linked list of all pages in order they came into memory Page at beginning of list replaced Disadvantage page in memory the longest may be often used

10 FIFO 12 references, 9 faults

11 Belady's Anomaly (for FIFO)
Same reference string as with 3 frames (9 page faults). 12 references, 10 faults Belady’s Anomaly for FIFO: (Sometimes) as the number of page frames increase, so does the fault rate.

12 Second Chance Algorithm
Operation of second chance. (a) Pages sorted in FIFO order. (b) Page list if a page fault occurs at time 20 and A has its R bit set. The numbers above the pages are their load times.

13 Second Chance Example 12 references, 9 faults

14 The Clock Page Replacement Algorithm

15 Least Recently Used (LRU)
Assume pages used recently will used again soon throw out page that has been unused for longest time Must keep a linked list of pages most recently used at front, least at rear update this list every memory reference !! Alternatively keep counter in each page table entry choose page with lowest value counter periodically zero the counter

16 LRU Page Replacement Algorithm
LRU using a matrix when pages are referenced in the order 0, 1, 2, 3, 2, 1, 0, 3, 2, 3.

17 Simulating LRU in Software
NFU Aging The aging algorithm simulates LRU in software. Shown are six pages for five clock ticks. The five clock ticks are represented by (a) to (e).

18 LRU and Anomalies Anomalies cannot occur. 12 references, 8 faults

19 Working Set Page Replacement (1)
一个进程当前正在使用的页面的集合,称为工作集。如果在内存中装入的是整个工作集,进程的运行在进入下一运行阶段之前,则不会产生很多页面失效。 工作集页面置换算法:在单纯的分页系统里,刚启动进程时,在内存中没有页面。在CPU试图取第一条指令时就会产生一次页面失效,使操作系统装入含有第一条指令的页面。其他由访问全局数据和堆栈引起的页面失效通常会紧接着发生。一段时间后,进程需要的大部分页面都已经在内存了,进程开始在较少页面失效的情况下运行。这个策略称为请求式分页,因为页面是在需要时调入,而不是预先装入。 程序的局部性:即在进程运行的任何阶段,它都指访问较少的一部分页面。 工作集是最近k次内存访问所用过页面的集合。 The working set is the set of pages used by the k most recent memory references. The function w(k, t) is the size of the working set at time t.

20 Working Set Page Replacement (2)
基于工作集的页面置换算法 The working set algorithm.

21 The WSClock Page Replacement Algorithm (1)
When the hand comes all the way around to its starting point there are two cases to consider: At least one write has been scheduled. No writes have been scheduled. 工作集时钟页面置换算法:当页面发生失效后,在确定被淘汰的页面之前,基本工作集算法需要扫描整个工作集内页面。 工作集时钟算法:基于时钟算法,并且收集了工作集信息。 与时钟算法一样,每次页面失效时,首先检查指针指向的页面。如果R位被置为1,该页面在当前时钟滴答中被使用过,那么该页面就不适合被淘汰。然后把该页面的R位置为0,指针指向下一个页面,并重复该算法。该时间序列之后的状态可以见下图。

22 The WSClock Page Replacement Algorithm (2)
Operation of the WSClock algorithm. (a) and (b) give an example of what happens when R = 1.

23 The WSClock Page Replacement Algorithm (3)
Operation of the WSClock algorithm. (c) and (d) give an example of R = 0.

24 Summary of Page Replacement Algorithms
Page replacement algorithms discussed in the text.

25 Local versus Global Allocation Policies (1)
Figure Local versus global page replacement. (a) Original configuration. (b) Local page replacement. (c) Global page replacement.

26 Local versus Global Allocation Policies (2)
Page fault rate as a function of the number of page frames assigned.

27 Modeling Page Replacement Algorithms Belady's Anomaly
FIFO with 3 page frames FIFO with 4 page frames P's show which page references show page faults

28 Stack Algorithms State of memory array, M, after each item in reference string is processed

29 Original configuration Local page replacement Global page replacement
Design Issues for Paging Systems Local versus Global Allocation Policies (1) Original configuration Local page replacement Global page replacement

30 Local versus Global Allocation Policies (2)
Page fault rate as a function of the number of page frames assigned

31 Load Control Despite good designs, system may still thrash When
some processes need more memory but no processes need less Solution : Reduce number of processes competing for memory swap one or more to disk, divide up pages they held reconsider degree of multiprogramming

32 Page Size (1) Small page size Advantages less internal fragmentation
better fit for various data structures, code sections less unused program in memory Disadvantages programs need many pages, larger page tables

33 Page Size (2) Overhead due to page table and internal fragmentation
Where s = average process size in bytes p = page size in bytes e = page entry page table space internal fragmentation 从数学上分析的结论,最优页面大小的公式。 Optimized when

34 Separate Instruction and Data Spaces
大多数计算机只有一个地址空间,既存放程序也存放数据,如果地址空间足够大还不会有什么问题。然而通常地址空间太小了,这就使得程序员对地址空间的使用出现困难。 为指令和数据设置分离的地址空间。I空间和D空间两种地址空间都可以分页,相互独立。他们分别有自己的页表,完成虚拟页面到物理页帧的映射。 One address space Separate I and D spaces

35 Two processes sharing same program sharing its page table
Shared Pages 页面共享:避免同时在内存中有相同的页面。 Two processes sharing same program sharing its page table

36 Implementation Issues Operating System Involvement with Paging
Four times when OS involved with paging Process creation determine program size create page table Process execution MMU reset for new process TLB flushed Page fault time determine virtual address causing fault swap target page out, needed page in Process termination time release page table, pages

37 Page Fault Handling (1) Hardware traps to kernel
General registers saved OS determines which virtual page needed OS checks validity of address, seeks page frame If selected frame is dirty, write it to disk

38 Page Fault Handling (2) OS brings schedules new page in from disk
Page tables updated Faulting instruction backed up to when it began Faulting process scheduled Registers restored Program continues

39 An instruction causing a page fault
Instruction Backup An instruction causing a page fault

40 Locking Pages in Memory
Virtual memory and I/O occasionally interact Proc issues call for read from device into buffer while waiting for I/O, another processes starts up has a page fault buffer for the first proc may be chosen to be paged out Need to specify some pages locked exempted from being target pages

41 Backing Store (a) Paging to static swap area
(b) Backing up pages dynamically

42 Separation of Policy and Mechanism
Page fault handling with an external pager

43 作业 P 139 页 12, 22, 24,26, 27, 28


Download ppt "Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?"

Similar presentations


Ads by Google