Chapter 9: Virtual Memory

Slides:



Advertisements
Similar presentations
Virtual Memory (Chapter 4.3)
Advertisements

Module 10: Virtual Memory
Chapter 10: Virtual Memory
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Chapter 9: Virtual Memory
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Virtual Memory Management G. Anuradha Ref:- Galvin.
9.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory OSC: Chapter 9. Demand Paging Copy-on-Write Page Replacement.
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.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Gordon College Stephen Brinton
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
03/26/2010CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying an earlier.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
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.
Chapter 10: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 10: Virtual Memory.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual Memory.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
CSS430 Virtual Memory Textbook Ch9
Virtual Memory.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
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.
Part 8: Virtual Memory. Silberschatz, Galvin and Gagne ©2005 Virtual vs. Physical Address Space Each process has its own virtual address space, which.
Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
CS212: OPERATING SYSTEM Lecture 6: Virtual-Memory Management 1 Computer Science Department.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Background Virtual memory –
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples Operating.
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.
9.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts Chapter 9: Virtual-Memory Management Background Demand Paging Page Replacement Allocation.
Virtual Memory. Background Virtual memory is a technique that allows execution of processes that may not be completely in the physical memory. Virtual.
Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 9: Virtual-Memory Management.
Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department.
CS307 Operating Systems Virtual Memory Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory.
Lecture 19 Virtual Memory Demand Paging. Background Virtual memory – separation of user logical memory from physical memory. –Only part of the program.
Virtual Memory The address used by a programmer will be called a virtual address or logical address. An address in main memory is called a physical address.
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 9: Virtual-Memory Management 9.1 Background.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
10.1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual-Memory Management.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
Chapter 9: Virtual Memory
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 9: Virtual Memory – Part I
Chapter 9: Virtual Memory
Chapter 9: Virtual Memory
Operating Systems Virtual Memory Alok Kumar Jagadev.
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Chapter 9: Virtual Memory
Chapter 6 Virtual Memory
Operating Systems CMPSC 473
Module 9: Virtual Memory
Chapter 9: Virtual Memory CSS503 Systems Programming
Chapter 9: Virtual Memory
Virtual Memory.
Chapter 8 & 9 Main Memory and Virtual Memory
Presentation transcript:

Chapter 9: Virtual Memory As each process requests memory, the memory manager must satisfy or deny the request If free memory is not available, the request is denied If the physical memory of the system is not sufficient, overlays must be used to allow a process to overlay its allocated memory The memory thus limits the number and size of ready processes How can we more efficiently use our limited physical memory? Virtual address – separation of user logical memory address from physical memory address Virtual memory – separation of user logical memory from physical memory We can extend the concept of “swapping” - instead of swapping the entire memory space of a process in or out, we swap part of the memory space in or out on demand Logical address space can therefore be much larger than physical address space CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Demand Paging Only part of the program needs to be in memory for execution. Need to allow pages to be swapped in and out Parts of a program (rare features, error handling routines, large static data structures, etc.) may never be needed in a particular run Virtual memory can be implemented via: Demand paging (swapper replaced with pager) Demand segmentation Bring a page into memory (via pager) only when it is needed Less I/O needed (initially) Less memory needed Faster response More users/processes When memory is accessed, one of the following occurs: Page is memory resident  satisfy reference Page is not memory resident  bring page into memory (I/O) Page is invalid  abort Page fault CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Valid-Invalid Bit With each page table entry a valid–invalid bit is associated (1  in-memory, 0  not-in-memory) Initially valid–invalid but is set to 0 on all entries Example of a page table snapshot: During address translation, if valid–invalid bit in page table entry is 0  page fault 1  Frame # valid-invalid bit page table CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Page Fault If there is ever a reference to a page, first reference will trap to OS  page fault OS looks at another table to decide: Just not currently in memory Processing a page fault: If invalid, abort process Find (or make) an empty frame Swap page into frame (I/O) Reset tables, validation bit = 1. Restart instruction Systems which start executing a process with no frames initially in memory use the pure demand pageing scheme The hardware to support demand paging is the same as the hardware for paged memory (Page table) and swapping (backing store) CEG 433/633 - Operating Systems I

What happens if there is no free frame? When a page fault occur the OS must find a frame for the new page If no frames are free, a victim must be selected for eviction page replacement – find some page in memory, but not currently in use, page (swap) it out this requires a decision, thus we need an algorithm performance goal: minimum number of page faults (effectively the same as increasing the hit ratio) If the victim page has been modified (dirty), it must first be stored to disk (Is RAM a cache?) Each page must be brought into memory at least once try to avoid paging the same page several times…. Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory CEG 433/633 - Operating Systems I

Performance of Demand Paging Page Fault Rate 0  p  1.0 if p = 0 no page faults if p = 1, every reference is a fault Effective Access Time (EAT) EAT = (1 – p) x memory access + p ( page fault overhead + [swap page out (if dirty)] + swap page in + restart overhead ) Goal: Reduce PFR to decrease EAT CEG 433/633 - Operating Systems I

Optimal Page-Replacement Algorithm Page-replacement algorithms are evaluated by running them on a particular string of memory references (a reference string) and computing the number of page faults on that string Optimal Algorithm: remove the page which will be referenced farthest in the future not used for longest period of time requires an oracle use as a metric to judge how well other algorithms perform Example: Consider the following references on a system w/4 frames 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 2 3 4 6 page faults 5 CEG 433/633 - Operating Systems I

First-In-First-Out (FIFO) Algorithm Keep a list, remove oldest page. Example: reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Note: More frames does not imply less page faults! FIFO replacement suffers Belady’s Anomaly 3 frames 4 frames 1 2 3 4 5 9 page faults 1 2 3 5 4 10 page faults CEG 433/633 - Operating Systems I

Least Recently Used (LRU) Algorithm 1 2 3 5 4 The victim is the page not used for the longest time Example: reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Problem: How do we implement this? Updating a list on every memory reference is expensive Solutions: Counter implementation Add a hardware counter; when the page is referenced copy the counter value into the page table entry When a page needs to be changed, look at the counters to determine which are to change Software/Stack implementation – keep a stack of page numbers in a double link form, update on page table lookup: Page referenced: move it to the top, change 6 pointers No search for replacement CEG 433/633 - Operating Systems I

LRU Approximation Algorithms Reference bit With each page associate a bit, initially cleared to 0 When page is referenced bit set to 1 Select as a victim a page whose reference bit = 0 Clear bits periodically FIFO - second chance algorithm If the victim has been referenced lately (R bit set) clear the bit and put it on the tail of the FIFO, clear reference bits periodically Slightly better, but still suffers from Belady’s Anomaly LRU w/aging Requires a n-bit register for each entry The MSB is the reference bit Right shift periodically (~100ms or so) Frames with the largest value are (approximately) the most frequently referenced CEG 433/633 - Operating Systems I

Not-Recently-Used (NFU) Algorithm AKA: enhanced second chance LFU approximation algorithm R bit is set when page is referenced (memory read) M bit is set when page is modified (memory write) Clear all R bits on each clock interrupt Each page is a member of one of four classes Class 0: R’ M’ Class 1: R’ M Class 2: R M’ Class 3: R, M Select (randomly) a page in the lowest class Easy to implement “Ok” performance (used in Macintosh OS) Paging Daemon: Many OSes invoke a paging daemon when memory is full to select “victims” to writeback data in preparation for normal eviction CEG 433/633 - Operating Systems I

Processing a Page Fault Hardware trap to kernel (save PC on stack or special register). Enter monitor mode, call assembly routine to save registers and call OS. OS “discovers” page fault and tries to find which page is needed (from hardware register or by examining the instruction @ the saved PC). Check to see if page is valid and allowed access. If not, abort process. Otherwise, find a free frame. If necessary, select a victim. If victim is dirty, schedule I/O, mark frame as busy, and switch context until done. Look up disk address for page and request transfer to clean frame. Switch context and wait until disk interrupt for finished I/O arrives. Update tables and mark frame as normal (not busy). Back up faulting instruction to original state and reset PC. Schedule process for execution (take off wait queue) and return to the assembly routine that invoked the OS. Restore registers and return to user mode as if no fault had occurred. CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Allocation of Frames How do we decide how many frames each process gets? Working set: The subset of its pages that a process requires to be in memory resident at a given point in its execution A system that uses memory segmentation with paging may require a minimum of 3 pages per process (text, data, stack) Some processes require far more All processes “want” their working set to be fully resident Major allocation schemes equal fixed allocation: frames divided evenly among processes proportional fixed allocation: partition weighted by process size priority allocation: proportion is based on a non-size priority Major replacement policies Global replacement: process selects a replacement frame from the set of all frames; one process can take a frame from another Local replacement: each process selects from only its own set of allocated frames CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Thrashing If a process does not have enough frames to hold its current working set, 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 (Argh!) Thrashing a process is thrashing when it spends more time paging than executing w/ local replacement algorithms, a process may thrash even though memory is available w/ global replacement algorithms, the entire system may thrash Less thrashing in general, but is it fair? CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Thrashing Diagram Why does paging work? Locality model Process migrates from one locality to another. Localities may overlap. Why does thrashing occur?  size of locality > total memory size What should we do? suspend one or more processes! CEG 433/633 - Operating Systems I

Page-Fault Frequency Scheme Establish “acceptable” page-fault frequency (PFF) If PFF falls below minimum, remove a frame from processes If PFF rate too high, allocate a new frame to process If no free frames are available, suspend process CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Program Structure How should we arrange memory references to large arrays? Is the array stored in row-major or column-major order? Example: Array A[1024, 1024] of type integer Page size = 1K Each row is stored in one page System has one frame Program 1 for i := 1 to 1024 do for j := 1 to 1024 do A[i,j] := 0; 1024 page faults Program 2 for j := 1 to 1024 do for i := 1 to 1024 do A[i,j] := 0; 1024 x 1024 page faults CEG 433/633 - Operating Systems I

CEG 433/633 - Operating Systems I Demand Segmentation Used when insufficient hardware to implement demand paging OS/2 allocates memory in segments, which it keeps track of through segment descriptors Segment descriptor contains a valid bit to indicate whether the segment is currently in memory. If segment is in main memory, access continues, If not in memory, segment fault Hybrid Scheme: Segmentation with demand paging CEG 433/633 - Operating Systems I