Lecture 24 Reminders: Homework 5 due Wednesday. Case study outline with references due following Wednesday. Memory management project posted. Questions? Monday, March 14 CS 470 Operating Systems - Lecture 24 1 Outline Review: Demand paging Storage management Page fault processing Page replacement Monday, March 14 CS 470 Operating Systems - Lecture 24 2 Dynamic, Partial, Non-Contiguous Organization Recall: Virtual memory storage organization provide support for the bolded design choices: single vs. multiple processes complete vs. partial allocation fixed-size vs. variable-size allocation contiguous vs. fragmented allocation static vs. dynamic allocation of partitions Monday, March 14 CS 470 Operating Systems - Lecture 24 3 Review: Demand Paging Virtual memory (VM) commonly is implemented using a demand paging technique. The idea is fairly simple, only bring a logical page of a process into memory when it is used. As with complete allocation organizations, a process's logical address space is loaded onto a backing store (also called swap space) in a contiguous manner to make loading into memory easier. Backing store usually is a disk. Monday, March 14 CS 470 Operating Systems - Lecture 24 4 Review: Demand Paging The PT is modified to have a valid/invalid bit in each entry to indicate whether the page is in memory. If the entry is valid, it contains the physical frame number as usual. If the entry is invalid, it contains the disk address on the backing store that holds the page. Monday, March 14 CS 470 Operating Systems - Lecture 24 5 Review: Address Translation Address translation must now handle the case where the logical page is not in memory: Page number p is obtained from logical address If TLB hit, access memory If TLB miss, access PT If PT entry is valid, access memory If PT entry is invalid, trap to OS and process goes to the Wait Queue. This is called a page fault. Monday, March 14 CS 470 Operating Systems - Lecture 24 6 Review: Address Translation Page fault processing consists of Issuing a disk transfer request to the backing store Loading the requested page into a free frame in physical memory Updating the page table entry with valid bit and frame number OS issues an Event completion interrupt and process goes to the Ready Queue to wait for CPU. Then it attempts the same access that caused the page fault. Monday, March 14 CS 470 Operating Systems - Lecture 24 7 Review: Address Translation log. addr. phys. addr. f d CPU p d f d TLB p# f# p f TLB hit i/v valid PT entry f# main memory p TLB miss update page table I/O completion interrupt Monday, March 14 i/v m/f invalid PT entry OS trap page fault page table backing store CS 470 Operating Systems - Lecture 24 transfer page from disk to memory 8 Storage Management One advantage of VM is that more processes can be run at any given time. (I.e., it increases multi-programming.) E.g., suppose processes each have 10 logical pages and there are 40 physical frames. With real (i.e., static and complete) memory allocation, only 4 processes can be loaded. If each process uses an average of 5 logical pages, then can load 8 processes. Monday, March 14 CS 470 Operating Systems - Lecture 24 9 Storage Management However, there is a chance that all 8 processes will try to access all 10 of their pages at the same time. Now need 80 physical frames, but only have 40. What should happen when there is a page fault and no free frames? Monday, March 14 CS 470 Operating Systems - Lecture 24 10 Page Replacement How is this done? Add to page fault handler Write page back to swap space Invalidate PT (and TLB, if any) entry Note that now there are two disk transfers, when there are no free frames. Really only need to write back a page if it has been modified. Add a dirty bit to each PT entry to indicate if a page has been changed. Only write back the modified frames. Monday, March 14 CS 470 Operating Systems - Lecture 24 11 Page Replacement The page fault algorithm becomes 1. Find location of page p on swap disk 2. Find a free frame: a. If there is a free frame, use it b. If there is no free frame, use a page replacement algorithm to select a victim frame c. If the victim frame is dirty, write to disk d. Change the PT of victim process accordingly 3. Read page p from swap disk into newly freed frame; change PT of this process accordingly 4. Restart user process Monday, March 14 CS 470 Operating Systems - Lecture 24 12 Replacement Algorithms There are many types of replacement algorithms. Criteria for choosing includes: Low page fault rate Efficient in choosing victim frame As with CPU scheduling, evaluate by simulating on scenario data and comparing. For VM page replacement, data is a string of logical page references. Also need to know number of physical frames available. Monday, March 14 CS 470 Operating Systems - Lecture 24 13 Replacement Algorithms Generally, we expect that more physical frames leads to fewer page faults. E.g., if have only one physical frame, then nearly every memory reference causes a fault. Vice versa, if have as many physical frames as logical pages, then only the first use of each page causes a fault. For most of the examples, will use 3 physical frames. Monday, March 14 CS 470 Operating Systems - Lecture 24 14 FIFO Replacement As usual, simplest algorithm is FIFO (first in, first out). Associate a time with each frame. Victim frame is the oldest one. Can be implemented using a basic queue. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 3 3 3 2 2 2 1 1 1 1 0 0 0 3 3 * * * * * * * * * * Easy to understand, but doesn't always give th good performance. E.g., 5 fault (page 3). Monday, March 14 CS 470 Operating Systems - Lecture 24 15 Belady's Anomaly Consider following reference string with 3 physical frames that results in 9 faults: 1 2 3 4 1 2 5 1 1 1 4 4 4 2 2 2 1 3 3 3 1 2 3 4 5 5 5 1 1 3 3 2 2 2 4 5 Do it again with 4 frames; results in 10 faults! 1 2 3 4 1 1 1 1 2 2 2 3 3 1 2 5 1 2 3 4 5 4 Monday, March 14 CS 470 Operating Systems - Lecture 24 16 OPT Replacement OPT is the optimal algorithm - replace the page that will be used farthest into the future. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 7 7 7 2 2 2 0 0 0 0 4 1 1 3 3 Monday, March 14 CS 470 Operating Systems - Lecture 24 17 OPT Replacement OPT is provably optimal for a finite reference string. Of course, generally do not know the entire reference string, but it is good to know a theoretical minimum so we can say things like "at worst within 12% of optimal" or "4.7% of optimal on average". OPT is unimplementable in real systems, so need to approximate. FIFO is not good. Monday, March 14 CS 470 Operating Systems - Lecture 24 18 LRU Replacement LRU chooses the "least recently used" page as the victim page. The theory is to use recent past usage as a predictor of new future usage. It replaces the page that has not been used for the longest time. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 7 7 7 2 2 4 0 0 0 0 0 1 1 3 3 Monday, March 14 CS 470 Operating Systems - Lecture 24 19 LRU Replacement The main problem for LRU is how to implement it. There are a couple of feasible implementations Counters: use CPU time counter and store it into the PT on each reference. Replace the lowest numbered entry. Need to search PT. Need to deal with context switches and counter rollover "Stack": keep a "stack" of page references. When a page is referenced, move it to the top of stack. Victim is bottom of stack. Since want to remove from middle, usually use doubly-linked list. Monday, March 14 CS 470 Operating Systems - Lecture 24 20