Virtual Memory Background Demand Paging Page Replacement Objectives

Virtual Memory • Background • Demand Paging • Page. Replacement

Objectives • To describe the benefits of a virtual memory system • To explain the concepts of demand paging, pagereplacement algorithms, and allocation of page frames • To discuss the principle of the working-set model

Background • Virtual memory - separation of user logical memory from physical memory. - Only part of the program needs to be in memory for execution - Logical address space can therefore be much larger than physical address space - Allows address spaces to be shared by several processes • Virtual memory can be implemented via: - Demand paging - Demand segmentation

Demand Paging • Bring a page into memory only when it is needed - Less I/O needed - Less memory needed - Faster response - More users • Page is needed reference to it - Invalid reference abort - Not-in-memory bring to memory • Lazy swapper -never swaps a page into memory unless page will be needed - Swapper that deals with pages is apager

Page Table When Some Pages Are Not in Main Memory

Page Fault • If there is areference to apage, firstreference to that page will trap to operating system: page fault 1. Operating system looks at another tableto determine whether the reference was a valid or invalid memory access. 2. If the reference is invalid – terminate the process. If it is valid – bring the page 3. Find an empty frame 4. Read the desired page into newly allocated frame 5. Update the page table and Set validation bit =v 6. Restart the instruction that caused thepage fault

Steps in Handling a Page. Fault

What happens if there is nofree frame? • Page replacement - find some page in memory, but not really in use, swap it out -Algorithm -Performance - want an algorithm which will result in minimum number of page faults • Same page may be brought into memory several times

Page Replacement • Basic to demandpaging • When a user process executes and page faultoccurs and there are no free frames inmemory -One choice for OSis to terminate the process -Another choice is to swap out one process -One frame is freed that is not currently being used… called Page Replacement • Page replacement completes separation between logical memory and physical memory - largevirtual memory can be provided on a smaller physical memory

STEPS IN PAGE REPLACEMENT : Find the location of the desired page on the disk. 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. Write the victim page to the disk, change the page and frame tables accordingly. Read the desired page into the newly freed frame, change the page and frame tables. Restart the user process.

Page Replacement Use modify (dirty) bit to reduce overhead ofpage transfers - only modified pages are written to disk

Page Replacement Algorithms • Want lowest page-fault rate • Evaluate algorithm by running it on aparticular string of memory references (reference string) and computing the number of page faultson that string • In all our examples, the reference stringis 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

FIFO page-replacement algorithm • The simplest page-replacement algorithm is a FIFO algorithm. • A FIFO replacement algorithm associates with each page the time when that page was brought into memory. • When a page must be replaced, the oldest page is chosen. • We replace the page at the head of the queue. • When a page is brought into memory, we insert it at the tail of the queue. Operating System Concepts

FIFO page-replacement algorithm Operating System Concepts

The FIFO page –replacement algorithm • The FIFO page –replacement algorithm is easy to understand program. • However, its performance is not always good. • The page replaced may be an initialization module that was used a long time ago and is no longer needed. • On other hand , it could contain a heavily used variable that was initialized early and is in constant use. • Most unexpected result is known as belady’s anomaly: for some page-replacement algorithms, the page-fault rate may increase as the number of allocated frames increases. Operating System Concepts

Optimal page-replacement algorithm • An optimal page-replacement algorithm has the lowest pagefault rate of all algorithms, and will never suffer from belady’s anomaly. • Such an algorithm does exist, and has been called OPT or MIN. • It is simply Replace the page that will not be used for the longest period of time. • Use of the page -replacement algorithm guarantees the lowest possible page fault rate for a fixed number of frames.

Optimal page-replacement algorithm Operating System Concepts

Optimal Page Replacement • The key distinction between the FIFO and OPT algorithms is that the FIFO algorithm uses the time when a page was brought into memory; • the OPT algorithm uses the time when a page is to be used. • If we use the recent past as an approximation of the near future, then we will replace the page that has not been used for the longest period of time. • This approach is the least –recently-used (LRU) algorithm. • LRU replacement associates with each page the time that page’s last use. When a page must be replace, LRU chooses that page that has not been used for the longest period. Operating System Concepts

LRU Page Replacement • LRUreplacement associates with each page the time of that page’s last use • When a page must be replaced, LRUchooses the page that has not been used for the longest period of time

Least Recently Used (LRU) Algorithm Operating System Concepts

LRU • The major problem is how to implement LRU replacement: Counter: whenever a reference to a page is made, the content of the clock register are copied to the time-of-use field in the page table entry for the page. We replace the page with the smallest time value Stack: Whenever a page is referenced, it is • removed from the stack and put onthe top. • In this way, the most recently used page is always at the top of the stack

Examples • Reference String –FIFO Page Replacement

Examples • Reference String –Optimal Page Replacement

Examples • Reference String –LRU Page Replacement
- Slides: 24