1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.

Slides:



Advertisements
Similar presentations
9.4 Page Replacement What if there is no free frame?
Advertisements

Page Replacement Algorithms
Virtual Memory (Chapter 4.3)
Chapter 3.3 : OS Policies for Virtual Memory
Virtual Memory 3 Fred Kuhns
Module 10: Virtual Memory
Page-replacement policies On some standard algorithms for the management of resident virtual memory pages.
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Virtual Memory II Chapter 8.
Scribe for 7 th April 2014 Page Replacement Algorithms Payal Priyadarshini 11CS30023.
Chapter 8 Virtual Memory
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 38 Frame Allocation Read.
Chapter 101 Cleaning Policy When should a modified page be written out to disk?  Demand cleaning write page out only when its frame has been selected.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
1 Thursday, July 06, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Virtual Memory Chapter 8.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Virtual Memory Chapter 8.
1 Virtual Memory Chapter 8. 2 Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
1 Lecture 9: Virtual Memory Operating System I Spring 2007.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Virtual Memory Chapter 8.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
OS Spring’04 Virtual Memory: Page Replacement Operating Systems Spring 2004.
Virtual Memory Basic Concepts Demand Paging
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Virtual Memory.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
1 Virtual Memory Chapter 8. 2 Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Copyright ©: Lawrence Angrave, Vikram Adve, Caccamo 1 Virtual Memory.
Operating Systems CMPSC 473 Virtual Memory Management (3) November – Lecture 20 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
ICS Virtual Memory 8.1 Principles of Virtual Memory 8.2 Implementations of Virtual Memory –Paging –Segmentation –Paging With Segmentation –Paging.
Virtual Memory The memory space of a process is normally divided into blocks that are either pages or segments. Virtual memory management takes.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory.
Page Buffering, I. Pages to be replaced are kept in main memory for a while to guard against poorly performing replacement algorithms such as FIFO Two.
10.1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Virtual Memory Chapter 8.
OPERATING SYSTEM CONCEPTS AND PRACTISE
ITEC 202 Operating Systems
Day 23 Virtual Memory.
Day 24 Virtual Memory.
Chapter 8 Virtual Memory
Lecture 10: Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Practical Session 8, Memory Management 2
Operating Systems CMPSC 473
Module 9: Virtual Memory
COMP755 Advanced Operating Systems
Practical Session 9, Memory Management continues
Presentation transcript:

1 Page Replacement Algorithms

2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when a page fault occurs Issues:  Placement strategies  Place pages anywhere – no placement policy required  Replacement strategies  What to do when there exist more jobs than can fit in memory  Load control strategies  Determining how many jobs can be in memory at one time Operating System User Program 1 User Program 2 User Program n... Memory

3 Page Replacement Algorithms Concept Typically  i VAS i >> Physical Memory With demand paging, physical memory fills quickly When a process faults & memory is full, some page must be swapped out  Handling a page fault now requires 2 disk accesses not 1!  Though writes are more efficient than reads (why?) Which page should be replaced? Local replacement — Replace a page of the faulting process Global replacement — Possibly replace the page of another process

4 Page Replacement Algorithms Evaluation methodology Record a trace of the pages accessed by a process  Example: (Virtual) address trace... (3,0), (1,9), (4,1), (2,1), (5,3), (2,0), (1,9), (2,4), (3,1), (4,8)  generates page trace 3, 1, 4, 2, 5, 2, 1, 2, 3, 4 (represented as c, a, d, b, e, b, a, b, c, d) Hardware can tell OS when a new page is loaded into the TLB  Set a used bit in the page table entry  Increment or shift a register Simulate the behavior of a page replacement algorithm on the trace and record the number of page faults generated fewer faults better performance

5 Optimal Page Replacement Clairvoyant replacement Replace the page that won’t be needed for the longest time in the future cadbebabc d Faults Page Frames a b c d Requests Time Time page needed next

6 Optimal Page Replacement Clairvoyant replacement Replace the page that won’t be needed for the longest time in the future cadbebabc d aaaaaaaaa daaaaaaaaa d bbbbbbbbb b ccccccccc c Faults Page Frames ddddeeeee e a b c d Requests Time a = 7 b = 6 c = 9 d = 10 Time page needed next a = 15 b = 11 c = 13 d = 14

7 Local Page Replacement FIFO replacement Simple to implement  A single pointer suffices Performance with 4 page frames: cadbebabc d Faults Page Frames a b c d Requests Time Physical Memory Frame List

8 Local Page Replacement FIFO replacement Simple to implement  A single pointer suffices Performance with 4 page frames: cadbebabc d aaaaeeeee daaaaeeeee d bbbbbbaaa a cccccccbb b Faults Page Frames ddddddddc c a b c d Requests Time Physical Memory Frame List

9 Least Recently Used Page Replacement Use the recent past as a predictor of the near future Replace the page that hasn’t been referenced for the longest time cadbebabc d Faults Page Frames a b c d Requests Time Time page last used

10 Least Recently Used Page Replacement Use the recent past as a predictor of the near future Replace the page that hasn’t been referenced for the longest time cadbebabc d aaaaaaaaa a bbbbbbbbb b cccceeeee dcccceeeee d Faults Page Frames ddddddddccddddddddcc a b c d Requests Time a = 2 b = 4 c = 1 d = 3 Time page last used a = 7 b = 8 e = 5 d = 3 a = 7 b = 8 e = 5 c = 9

11 Least Recently Used Page Replacement Implementation Maintain a “stack” of recently used pages cadbebabc d aaaaaaaaa a bbbbbbbbb b cccceeeee dcccceeeee d Faults Page Frames ddddddddc c a b c d Requests Time c c a c a d c a d b a d b e a d e b d e b a d e a b e a b c a b c d LRU page stack Page to replace c de

12 Least Recently Used Page Replacement Implementation Maintain a “stack” of recently used pages cadbebabc d aaaaaaaaa a bbbbbbbbb b cccceeeee dcccceeeee d Faults Page Frames ddddddddc c a b c d Requests Time c c a c a d c a d b a d b e a d e b d e b a d e a b e a b c a b c d LRU page stack Page to replace c de

13 What is the goal of a page replacement algorithm?  A. Make life easier for OS implementer  B. Reduce the number of page faults  C. Reduce the penalty for page faults when they occur  D. Minimize CPU time of algorithm

14 Approximate LRU Page Replacement The Clock algorithm Maintain a circular list of pages resident in memory  Use a clock (or used/referenced) bit to track how often a page is accessed  The bit is set whenever a page is referenced Clock hand sweeps over pages looking for one with used bit = 0  Replace pages that haven’t been referenced for one complete revolution of the clock func Clock_Replacement begin while ( victim page not found ) do if( used bit for current page = 0 ) then replace current page else reset used bit end if advance clock pointer end while end Clock_Replacement resident bit used bit frame number 01 Page 7: 1 50 Page 1: 130 Page 4: 1 41 Page 0: 111 Page 3: 1

15 d c b a c Clock Page Replacement Example Faults Page Frames a b c d 0 Requests Time Page table entries for resident pages: 1 d c b a a 2 d c b a d 3 d c b a b 4 e 5 b 6 a 7 b 8 c 9 d e b c d e b c d e b a d e b a d e b a c d b a c a b c d

16 d c b a c Clock Page Replacement Example Faults Page Frames a b c d 0 Requests Time Page table entries for resident pages: 1 d c b a a 2 d c b a d 3 d c b a b 4 d c b e e 5 d c b e b 6 d a b e a 7 d a b e b 8 c a b e c 9 c a b d d e b c d e b c d e b a d e b a d e b a c d b a c a b c d

17 Optimizing Approximate LRU Replacement The Second Chance algorithm There is a significant cost to replacing “dirty” pages Modify the Clock algorithm to allow dirty pages to always survive one sweep of the clock hand  Use both the dirty bit and the used bit to drive replacement resident bit used bit dirty bit 01 Page 7: 1 50 Page 1: 130 Page 4: 1 41 Page 0: 191 Page 3: Before clock sweep After clock sweep useddirty useddirty replace page Second Chance Algorithm

18 d c b a c The Second Chance Algorithm Example Faults Page Frames a b c d 0 Requests Time Page table entries for resident pages: 1 d c b a awaw 2 d c b a d 3 d c b a bwbw 4 b 6 awaw 7 b a*a* b*b* e d a*a* b*b* e d a b*b* e d a b*b* e c a*a* d e c 10 a b c d a b c d e 5 c 9 d

19 d c b a c The Second Chance Algorithm Example Faults Page Frames a b c d 0 Requests Time Page table entries for resident pages: 1 d c b a awaw 2 d c b a d 3 d c b a bwbw 4 d e b a b 6 d e b a awaw 7 d e b a b a*a* b*b* e d a b e d a b e d a b e c a*a* d e c 10 a b c d a b c d d e b a e 5 c e b a c 9 c e d a d 10

20 The Problem With Local Page Replacement How much memory do we allocate to a process? Faults Page Frames a b c abcdabcda bcd Faults Page Frames a b c Requests Time –

21 The Problem With Local Page Replacement How much memory do we allocate to a process? Faults Page Frames a b c abcdabcda bcd aaadddccc bbbaaadddccc bbb bbbbaaadd dcc cccccbbba aad Faults Page Frames a b c Requests Time – aaaaaaaaa aaa bbbbbbbbb bbb ccccccccc ccc dddddd ddd

22 Page Replacement Algorithms Performance Local page replacement  LRU — Ages pages based on when they were last used  FIFO — Ages pages based on when they’re brought into memory Towards global page replacement... with variable number of page frames allocated to processes The principle of locality  90% of the execution of a program is sequential  Most iterative constructs consist of a relatively small number of instructions  When processing large data structures, the dominant cost is sequential processing on individual structure elements  Temporal vs. physical locality

23 Optimal Page Replacement For processes with a variable number of frames VMIN — Replace a page that is not referenced in the next  accesses Example:  = 4 ccdbcecea d Faults Pages in Memory Page a Page b Page c Page d Requests Time Page e- t = 0 t = -1

24 Optimal Page Replacement For processes with a variable number of frames VMIN — Replace a page that is not referenced in the next  accesses Example:  = 4 ccdbcecea d F - ---F F - - Faults Pages in Memory F Page a Page b Page c Page d Requests Time -----F- -Page e- t = 0 t = -1

25 Explicitly Using Locality The working set model of page replacement Assume recently referenced pages are likely to be referenced again soon…... and only keep those pages recently referenced in memory (called the working set)  Thus pages may be removed even when no page fault occurs  The number of frames allocated to a process will vary over time A process is allowed to execute only if its working set fits into memory  The working set model performs implicit load control

26 Working Set Page Replacement Implementation Keep track of the last  references  The pages referenced during the last  memory accesses are the working set   is called the window size Example: Working set computation,  = 4 references: ccdbcecea d Faults Pages in Memory Page a Page b Page c Page d Requests Time Page e t = 0 t = -1 t = -2

27 Working Set Page Replacement Implementation Keep track of the last  references  The pages referenced during the last  memory accesses are the working set   is called the window size Example: Working set computation,  = 4 references:  What if  is too small? too large? ccdbcecea d -----F ---F - - F Faults Pages in Memory F Page a Page b Page c Page d Requests Time ----F Page e t = 0 t = -1 t = -2

28 Page-Fault-Frequency Page Replacement An alternate working set computation Explicitly attempt to minimize page faults  When page fault frequency is high — increase working set  When page fault frequency is low — decrease working set Algorithm: Keep track of the rate at which faults occur When a fault occurs, compute the time since the last page fault Record the time, t last, of the last page fault If the time between page faults is “large” then reduce the working set If t current – t last > , then remove from memory all pages not referenced in [t last, t current ] If the time between page faults is “small” then increase working set If t current – t last ≤ , then add faulting page to the working set

29 Page-Fault-Frequency Page Replacement Example, window size = 2 If t current – t last > 2, remove pages not referenced in [t last, t current ] from the working set If t current – t last ≤ 2, just add faulting page to the working set t cur – t last ccdbcecea d Faults Pages in Memory Page a Page b Page c Page d Requests Time Page e

30 Page-Fault-Frequency Page Replacement Example, window size = 2 If t current – t last > 2, remove pages not referenced in [t last, t current ] from the working set If t current – t last ≤ 2, just add faulting page to the working set 3 t cur – t last 231 ccdbcecea d -----F ---F- - F Faults Pages in Memory - F Page a Page b Page c Page d Requests Time --F Page e 1

31 Load Control Fundamental tradeoff High multiprogramming level Issues  What criterion should be used to determine when to increase or decrease the MPL?  Which task should be swapped out if the MPL must be reduced? Low paging overhead  MPL min = 1 process minimum number of frames required for a process to execute number of page frames  MPL max =

32 Load Control How not to do it: Base load control on CPU utilization  Assume memory is nearly full  A chain of page faults occur  A queue of processes forms at the paging device  CPU utilization falls Operating system increases MPL  New processes fault, taking memory away from existing processes CPU utilization goes to 0, the OS increases the MPL further... System is thrashing — spending all of its time paging I/O Device... Paging Device Paging Device CPU

33 Better criteria for load control: Adjust MPL so that:  mean time between page faults (MTBF) = page fault service time (PFST)   WS i = size of memory 1.0 CPU Utilization Multiprogramming Level Thrashing can be ameliorated by local page replacement Load Control Thrashing N max N I/O-BALANCE MTBF PFST 1.0

34 Load Control Thrashing When the multiprogramming level should be decreased, which process should be swapped out? Suspended suspended queue ready queue semaphore/condition queues Waiting Running Ready ? Paging Disk Physical Memory  Lowest priority process?  Smallest process?  Largest process?  Oldest process?  Faulting process?