4.4 Page replacement algorithms

Slides:



Advertisements
Similar presentations
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Advertisements

Chapter 4 Memory Management Basic memory management Swapping
CS Introduction to Operating Systems
9.4 Page Replacement What if there is no free frame?
Page Replacement Algorithms
Virtual Memory (Chapter 4.3)
Chapter 101 The LRU Policy Replaces the page that has not been referenced for the longest time in the past By the principle of locality, this would be.
Chapter 3.3 : OS Policies for Virtual Memory
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Virtual Memory 3 Fred Kuhns
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.
Memory Management.
Page Replacement Algorithms
Virtual Memory Why? The need of memory more than the available physical memory. Process 3 Physical Memory Process 2 Process 1 Process 4.
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Virtual Memory. 2 What is virtual memory? Each process has illusion of large address space –2 32 for 32-bit addressing However, physical memory is much.
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
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.
Virtual Memory Introduction to Operating Systems: Module 9.
1 Memory Management Managing memory hierarchies. 2 Memory Management Ideally programmers want memory that is –large –fast –non volatile –transparent Memory.
Virtual Memory Today Virtual memory Page replacement algorithms
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management – 4 Page Replacement Algorithms CS 342 – Operating Systems.
Paging Algorithms Vivek Pai / Kai Li Princeton University.
Introduction to Systems Programming Lecture 7
Memory Management Virtual Memory Page replacement algorithms
CS 333 Introduction to Operating Systems Class 14 – Page Replacement Jonathan Walpole Computer Science Portland State University.
Virtual Memory Management B.Ramamurthy. Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Page Replacement Algorithms
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
CSCI2413 Lecture 6 Operating Systems Memory Management 2 phones off (please)
Demand Paging Virtual memory = indirect addressing + demand paging –Without demand paging, indirect addressing by itself is valuable because it reduces.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement Jonathan Walpole Computer Science Portland State University.
CSC 322 Operating Systems Concepts Lecture - 15: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Virtual Memory.
O RERATıNG S YSTEM LESSON 10 MEMORY MANAGEMENT II 1.
Memory Management Page replacement algorithms, segmentation Tanenbaum, ch. 3 p Silberschatz, ch. 8, 9 p
Page Replacement Algorithms Memory Management. Optimal Page Replacement ▪The label for each page in memory is labeled with the number of instructions.
1 First-in, First-out Remove the oldest page Determine the age based on the loading time, not on the time being referenced Old pages may be heavily used.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Paging.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
#include pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Lecture 11 Page 1 CS 111 Online Virtual Memory A generalization of what demand paging allows A form of memory where the system provides a useful abstraction.
Demand Paging Reference Reference on UNIX memory management
Virtual Memory Questions answered in this lecture: How to run process when not enough physical memory? When should a page be moved from disk to 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.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.
Virtual Memory. 2 Last Week Memory Management Increase degree of multiprogramming –Entire process needs to fit into memory Dynamic Linking and Loading.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
PAGE REPLACEMNT ALGORITHMS FUNDAMENTAL OF ALGORITHMS.
COS 318: Operating Systems Virtual Memory Paging.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Virtual Memory What if program is bigger than available memory?
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
Chapter 9: Virtual Memory – Part I
Demand Paging Reference Reference on UNIX memory management
Demand Paging Reference Reference on UNIX memory management
Chapter 9: Virtual-Memory Management
Demand Paged Virtual Memory
VM Page Replacement Hank Levy.
There’s not always room for one more. Brian Bershad
Practical Session 8, Memory Management 2
Operating Systems CMPSC 473
Lecture 9: Caching and Demand-Paged Virtual Memory
Chapter 9: Virtual Memory CSS503 Systems Programming
There’s not always room for one more. Brian Bershad
Practical Session 9, Memory Management continues
Presentation transcript:

4.4 Page replacement algorithms

Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Optimal page replacement Page fault occurs Scan all pages currently in memory Determine which page won’t be needed (referenced) until furthest in the future Replace that page Not really possible. (But useful as a benchmark.) Depends on code as well as data.

Algorithms we will discuss: Optimal NRU FIFO Second chance Clock LRU NFU Aging Working set WSClock

NRU (not recently used) Bits set by hardware after every memory reference Cleared only by software (OS) R bits – set when page is referenced (read or write) M bits – set when page is modified (written) Periodically (after k clock interrupts), R bits are cleared

NRU page categories Not referenced, not modified Not referenced, modified Occurs when #4’s R bit is cleared during clock interrupt Referenced, not modified Referenced, modified NRU algorithm: remove random page from lowest numbered non empty class

NRU algorithm evaluation Simple Efficient Not optimal but adequate

FIFO page replacement Queue pages as they are requested. Remove page at head (front) of queue. Oldest page is removed first + simple/efficient - might remove a heavily used page

Second chance page replacement Inspect R bit of oldest page If R==0 then page is old & unused Replace it Else Clear R bit Move page to from head to tail of FIFO Treating it as a newly loaded page Try another page

Second chance page replacement load time page

Clock page replacement Circular list instead of queue Clock hand points to oldest page If (R==0) then Page is unused Replace it Else Clear R Advance clock hand

Clock page replacement

LRU (least recently used) page replacement Page recently used is likely to be used in the near future; page not used in ages is not likely to be used in the near future. Algorithm: “age” the pages Maintain a queue of pages in memory. Recently used at front; oldest at rear. Every time a page is referenced, it is removed from the queue and placed at the front of the queue. This is slow!

LRU in hardware implementation #1: 64 bit counter, C, incremented after every instruction Each page also has a 64 bit counter When a page is referenced, C is copied to its counter. Page with lowest counter is oldest.

LRU in hardware implementation #2: Given n page frames, let M be a nxn matrix of bits initially all 0. Reference to page frame k occurs. Set all bits in row k of M to 1. Set all bits in column k of M to 0. Row with lowest binary value is least recently used.

LRU in hardware: implementation #2 example oldest

NFU (Not Frequently Used) Hardware doesn’t often support LRU Software counter associated w/ each page initially set to 0. At each clock interrupt: Add R bit (either 0 or 1) to the counter for each page. Page with lowest counter is NFU

NFU problem It never forgets! So pages that were frequently referenced (during initialization for example) but are no longer needed appear to be FU. Solution (called “aging”): Shift all counters to right 1 bit before R bit is added in. Then R bit is added to MSb (leftmost bit) instead of LSb (rightmost bit). Page w/ lowest value is chosen for removal.

NFU w/ aging Shift to right MSb = R 00010000

Differences between LRU and NFU LRU updated after every instruction so it’s resolution is very fine. NFU is coarse (updated after n instructions execute between clock interrupts). A given page referenced by n-1 instruction is given equal weight to a page referenced by only 1 instruction (between clock interrupts). n/2 references to a given page at the beginning of the interval are given equal weight with n/2 references to another page at the end of the interval.

Working set page replacement algorithm Demand paging = start up processes with 0 pages and only load what’s needed. Locality of reference = during any phase of execution, the process references only a relatively small fraction of its pages. Working set = set of pages that a process is currently using. Thrashing = causing a page fault every few instructions.

Working sets Working set model = make sure a page is in memory before the process needs it. a.k.a. prepaging w.s. = set of pages used in the k most recent memory references.

Working set algorithm Uses current virtual time = amount of CPU time a process has actually used since it started. T is a threshold on CVT R and M bits as before; clock interrupt

Working set algorithm age = current virtual time – time of last use  (greatest age/least virtual time) and choose that one if no better candidate exists. If no suitable candidate exists, pick one at random.

WSClock page replacement Previous WS algorithm requires entire page table be scanned at each page fault. WSClock: Simple, efficient, widely used. Uses circular list of page frames.

WSClock page replacement At each page fault… Loop once through page table: Examine PTE pointed to by clock hand. If r bit == 1 then clear r bit; advance clock hand; goto loop else If age>t If page is clean then use this page! Else write dirty page to disk; advance clock hand; goto loop If write scheduled, wait for completion and used that page. Else pick a victim at random.

WSClock page replacement clear r bit and advance clock hand. WSClock page replacement Replace old and advance.

Summary of page replacement algorithms