Presentation on theme: "Virtual Memory Introduction to Operating Systems: Module 9."— Presentation transcript:
Virtual Memory Introduction to Operating Systems: Module 9
Virtual Memory u Background u Demand Paging u Performance of Demand Paging u Page Replacement u Page-Replacement Algorithms u Allocation of Frames u Thrashing u Other Considerations u Demand Segmentation
Background u 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 Need to allow pages to be swapped in and out u Virtual memory can be implemented via: Demand paging Demand segmentation
Demand Paging u Bring a page into memory only when it is needed. Less I/O needed Less memory needed Faster response More users u Page is needed reference to it invalid reference abort not-in-memory bring to memory
Valid-Invalid Bit u With each page table entry a valid– invalid bit is associated (1 in-memory, 0 not-in- memory) u Initially valid–invalid but is set to 0 on all entries. u Example of a page table snapshot. u During address translation, if valid– invalid bit in page table entry is 0 page fault Frame #valid-invalid bit page table
Page Fault u If there is ever a reference to a page, first reference will trap to OS page fault u OS looks at another table to decide: Invalid reference abort. Just not in memory. u Find empty frame u Load page into frame u Reset tables, validation bit = 1 u Restart instruction auto increment/decrement location
What if there is no free frame? u 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. u Same page may be brought into memory several times.
Performance of Demand Paging u Page Fault Rate 0 p 1.0 if p = 0 no page faults if p = 1, every reference is a fault u Average Access Time (AAT) AAT = (1 – p) x memory access + p (page fault overhead + [page out] + page in + restart overhead)
Demand Paging Example u Memory access time (without paging) = 1 sec u Assume 50% of the time the page being replaced has been modified, and must be swapped out u Neglect restart overhead u Load/Store Page Time = 10 msec = 10,000 sec AAT = (1 – p) x 1 + p (15000) 1 – p p (in msec) u Paging, TLB, and Cache each change AAT
Page Replacement u Prevent over-allocation of memory by modifying page-fault service routine to include page replacement. u Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk. u Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory.
Page-Replacement Algorithms u We want lowest page-fault rate u Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string. u In the following examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
First-In-First-Out (FIFO) Algorithm u Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 u 3 frames (3 pages can be in memory at a time per process) u 4 frames u FIFO Replacement – Belady’s Anomaly more frames more page faults page faults page faults 4 43
Optimal Algorithm u Replace page that will not be used for longest period of time. u 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 u Used for measuring how well your algorithm performs page faults 4 5
Least Recently Used (LRU) Algorithm u Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 u Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. When a page needs to be replaced, look at the counters to determine which page is the victim
LRU Algorithm u Stack implementation – keep a stack of page numbers in a doubly-linked list: Page referenced: move it to the top requires 6 pointers to be changed No search for replacement
Clock Algorithm u Use bit With each page associate a bit, initially = 0 When page is referenced bit set to 1 Replace only pages with 0 for reference bit u Second chance Need reference bit (or use bit) A type of clock replacement If page to be replaced (in clock order) has reference bit = 1. then: set reference bit 0 leave page in memory attempt to replace the next page (in clock order), subject to same rules
u Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 u Use bit Each time a page is referenced, the bit is set to 1 u "Clock hand" Points to the frame where replacement algorithm starts Clock Policy
u Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Choosing a victim page If the clock hand points to a frame with use bit = 0, it is chosen as the target of page replacement u Clearing the reference bit If a page is spared, it's use bit is set to zero, and the clock hand advances Clock Policy
u Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 u Variations on the clock algorithm exist Search through the frames to find a page with use=0 and modify=0; if none, exists proceed as above Clock Policy
Allocation of Frames u Each process requires a certain number of pages to execute Some instructions require multiple pages reside in memory Example: IBM 370 – 6 pages to handle SS MOVE instruction: instruction is 6 bytes, might span 2 pages. 2 pages to handle from. 2 pages to handle to. u Two major allocation schemes fixed allocation variable allocation
Fixed Allocation u Each process is allocated frames when it is created; it neither gains nor loses frames during execution Allocate according to the size of process Allocate based on priority criteria (user, process type) u Frames my go unused in memory u If sufficient frames are not available, the process cannot be initiated Wait until frames are released by process termination u Can this scheme handle dynamically allocated memory?
Variable Allocation u During execution, the number of frames allocated to a process varies according to System load Page fault rate Priority u Requires frequent OS intervention, processing overhead
Global vs. Local Replacement Scope u Global replacement – OS selects a replacement frame from the set of all frames One process can take a frame from another u Local replacement – OS selects a replacement frame from the frames allocated to the faulting process
Inverted Page Table u One entry for each real page of memory. u Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page. u Useful for global allocation: once a victim frame is selected, the inverted page table is used to find the process which owns the victim page so that its page table can be updated
Inverted Page Table Architecture
Thrashing u If a process does not have “enough” pages, the page-fault rate is very high. This leads to: low CPU utilization. operating system thinks that it needs to increase the degree of multiprogramming. another process added to the system. u Thrashing a process is busy swapping pages in and out.
Thrashing Diagram u Why does paging work? Locality model Process migrates from one locality to another. Localities may overlap. u Why does thrashing occur? size of locality > total memory size
Working-Set Model u working-set window a fixed number of page references Example: 10,000 instructions u WSS i (working set of Process P i ) = total number of pages referenced in the most recent (varies in time) if too small will not encompass entire locality. if too large will encompass several localities. if = will encompass entire program. u D = WSS i total demand frames u if D > m Thrashing u Policy if D > m, then suspend one of the processes.
Keeping Track of the Working Set u Approximate with interval timer + a reference bit u Example: = 10,000 Timer interrupts after every 5000 time units. Keep in memory 2 bits for each page. Whenever a timer interrupts copy and sets the values of all reference bits to 0. If one of the bits in memory = 1 page in working set. u Why is this not completely accurate? u Might it be desirable to vary ?
Page-Fault Frequency Scheme u Establish “acceptable” page-fault rate. If the actual rate is too low, process loses frames. If the actual rate is too high, process gains frames.
Other Considerations u Prepaging Get pages around the needed page u Page size selection fragmentation table size I/O overhead locality
Program structure & performance u Program structure Array A[1024, 1024] of integer Each row is stored in one page Program 1 for (int j = 0; j < 1024; j++) for (int i = 0; i < 1024; i++) A[i][j] = 0; 1024 x 1024 = ~one million page faults Program 2 for (int i = 0; i < 1024; i++) for (int j = 0; j < 1024; j++) A[i][j] = 0; 1024 page faults