Virtual Memory Basic Concepts Demand Paging

Slides:



Advertisements
Similar presentations
Virtual Memory (Chapter 4.3)
Advertisements

Module 10: Virtual Memory
Chapter 10: Virtual Memory
Page Replacement Algorithms (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
Virtual Memory Management G. Anuradha Ref:- Galvin.
Virtual Memory Operating System Concepts chapter 9 CS 355
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 38 Frame Allocation Read.
03/31/2004CSCI 315 Operating Systems Design1 Allocation of Frames & Thrashing (Virtual Memory)
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 ©2009 Operating System Concepts – 8 th Edition, Lecture 15: Background Information for the VMWare ESX Memory Management.
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.
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 9: Virtual Memory Background Demand Paging.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Virtual-Memory Management
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.
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.
#include pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
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.
Saurav Karmakar. Chapter 9: Virtual Memory  Background  Demand Paging  Copy-on-Write  Page Replacement  Allocation of Frames  Thrashing  Memory-Mapped.
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.
CSC 360, Instructor: Kui Wu Memory Management II: Virtual Memory.
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.
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.
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.
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Chapter 9: Virtual Memory
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
Virtual Memory.
Presentation transcript:

Virtual Memory Basic Concepts Demand Paging Page Replacement Algorithms Working-Set Model GMU – CS 571

Last week in Memory Management… Assumed all of a process is brought into memory in order to run it. Contiguous allocation Paging Segmentation What if we remove this assumption? What do we gain/lose? GMU – CS 571

Process address Space GMU – CS 571

Virtual Memory Separation of user logical memory from physical memory Only part of the program needs to be in memory for execution. The logical address space can be much larger than the physical address space. Virtual memory can be implemented via: Demand paging Demand segmentation GMU – CS 571

Virtual Memory and Physical Memory GMU – CS 571

Demand Paging When time to run a process, bring only a few necessary pages into memory. While running the process, bring a page into memory only when it is needed (lazy swapping) Less I/O needed Less memory needed Faster response Support more processes/users Page is needed If memory resident  use the reference to page from page table If not in memory  Page fault trap GMU – CS 571

Valid-Invalid Bit How do we know if a page is in main memory or not? With each page table entry, a valid–invalid bit is associated (1  legal/in-memory, 0  not-in-memory) During address translation, if valid–invalid bit in page table entry is 0  page fault trap 1  Frame # valid-invalid bit page table GMU – CS 571

Use of the Page Table GMU – CS 571

Handling a Page Fault If there is a reference to a page which is not in memory, this reference will result in a trap  page fault Typically, the page table entry will contain the address of the page on disk. Major steps Locate empty frame Initiate disk I/O Move page (from disk) to the empty frame Update page table; set validation bit to 1. GMU – CS 571

Steps in Handling a Page Fault GMU – CS 571

Impact of Page Faults Each page fault affects the system performance negatively The process experiencing the page fault will not be able to continue until the missing page is brought to the main memory The process will be blocked (moved to the waiting state) so its data (registers, etc.) must be saved. Dealing with the page fault involves disk I/O Increased demand to the disk drive Increased waiting time for process experiencing the page fault GMU – CS 571

Performance of Demand Paging Page Fault Rate p (0  p  1.0) if p = 0, no page faults if p = 1, every reference is a page fault Effective Access Time with Demand Paging = (1 – p) * (effective memory access time) + p * (page fault overhead) Example Effective memory access time = 100 nanoseconds page fault overhead = 25 milliseconds p = 0.001 Effective Access Time with Demand Paging = 25 microseconds GMU – CS 571

Page Replacement As we increase the degree of multi-programming, over-allocation of memory becomes a problem. What if we are unable to find a free frame at the time of the page fault? One solution: Swap out a process (writing it back to disk), free all its frames and reduce the level of multiprogramming May be good under some conditions Another option: free a memory frame already in use. GMU – CS 571

Basic Page Replacement Find the location of the desired page on disk. Locate a free frame: - If there is no free frame, use a page replacement algorithm to select a victim frame. - Write the victim page back to the disk; update the page and frame tables accordingly. Read the desired page into the free frame. Update the page and frame tables. Put the process (that experienced the page fault) back to the ready queue. GMU – CS 571

Page Replacement GMU – CS 571

Page Replacement Observe: If there are no free frames, two page transfers needed at each page fault! We can use a modify (dirty) bit to reduce overhead of page transfers – only modified pages are written back to disk. Page replacement completes the separation between the logical memory and the physical memory – large virtual memory can be provided on a smaller physical memory. GMU – CS 571

Page Replacement Algorithms When page replacement is required, we must select the frames that are to be replaced. Primary Objective: Use the algorithm with lowest page-fault rate. Efficiency Cost Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string. We can generate reference strings artificially or we can use specific traces. GMU – CS 571

First-In-First-Out (FIFO) Algorithm Simplest page replacement algorithm. FIFO replacement algorithm chooses the “oldest” page in the memory. Implementation: FIFO queue holds identifiers of all the pages in memory. We replace the page at the head of the queue. When a page is brought into memory, it is inserted at the tail of the queue. GMU – CS 571

FIFO Page Replacement Consider what happens with the reference string above (where each number corresponds to a different page) Each of the first three references generate a page fault and are brought into memory Total faults = 3 GMU – CS 571

FIFO Page Replacement 2 – Page fault – replace 7 0 – no fault – in memory already Total faults = 4 GMU – CS 571

FIFO Page Replacement 3 – Page fault – replace 0 0 – Page fault - must be brought back in to replace the 1 Total faults = 6 GMU – CS 571

FIFO Page Replacement 4 – Page fault – replace 2 2 – Page fault - must be brought back in to replace the 3 3 – Page fault - must be brought back in to replace the 0 Total faults = 9 GMU – CS 571

FIFO Page Replacement 0 – Page fault – replace 4 3 – in memory Total faults = 10 GMU – CS 571

FIFO Page Replacement 1 – Page fault – replace 2 0 – in memory Total faults = 12 GMU – CS 571

FIFO Page Replacement 1 – in memory 7 – Page fault – replace 0 Total faults = 15 GMU – CS 571

FIFO Page Replacement 7 – Page fault 0 – Page fault 1 – Page fault 1 1 2 7 – Page fault 0 – Page fault 1 – Page fault 2 – Page fault Total faults = 4 GMU – CS 571

FIFO Page Replacement 0 – in memory 3 – Page fault 4 – Page fault 7 7 7 7 3 3 4 1 1 1 1 2 2 2 0 – in memory 3 – Page fault 4 – Page fault Total faults = 6 GMU – CS 571

FIFO Page Replacement 2 – in memory 3 – in memory 0 – Page fault 7 7 7 7 3 3 3 4 4 1 1 1 1 2 2 2 2 2 – in memory 3 – in memory 0 – Page fault Total faults = 7 GMU – CS 571

FIFO Page Replacement 2 – in memory 1 – Page fault 2 – Page fault 7 7 7 7 3 3 3 3 2 4 4 4 4 1 1 1 1 2 2 2 2 1 1 2 – in memory 1 – Page fault 2 – Page fault 0 – in memory Total faults = 9 GMU – CS 571

FIFO Page Replacement 1 – in memory 7 – Page fault 0 – In memory 3 3 3 3 2 2 4 4 4 4 7 1 1 1 1 2 2 2 2 1 1 1 1 – in memory 7 – Page fault 0 – In memory Total faults = 10 GMU – CS 571

Page Faults Versus The Number of Frames Usually, for a given reference string the number of page faults decreases as we increase the number of frames. GMU – CS 571

FIFO Page Replacement But the “oldest” page may contain a heavily used variable. Will need to bring back that page in near future! Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3-frame case results in 9 page faults 4-frame case results in 10 page faults Program runs potentially slower with more memory! Belady’s Anomaly more frames  more page faults for some reference strings! GMU – CS 571

FIFO Page Replacement 1 2 3 4 1 2 5 1 2 3 4 5 1 1 1 1 4 4 faults 2 2 2 1 2 3 4 1 2 5 1 2 3 4 5 1 1 1 1 4 4 faults 2 2 2 3 3 1 1 1 4 2 2 2 4 faults 3 3 GMU – CS 571

FIFO Page Replacement 1 2 3 4 1 2 5 1 2 3 4 5 1 1 1 1 4 5 2 3 4 5 faults 2 2 2 3 3 1 1 1 4 4 4 5 2 2 2 1 1 1 7 faults 3 3 3 2 2 GMU – CS 571

FIFO Page Replacement 1 2 3 4 1 2 5 1 2 3 4 5 1 1 1 1 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 8 faults 2 2 2 3 3 1 1 1 4 4 4 5 5 2 2 2 1 1 1 3 8 faults 3 3 3 2 2 2 GMU – CS 571

FIFO Page Replacement 1 2 3 4 1 2 5 1 2 3 4 5 1 1 1 1 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 4 1 2 3 4 5 2 3 10 faults 2 2 2 3 3 1 1 1 4 4 4 5 5 5 2 2 2 1 1 1 3 3 9 faults 3 3 3 2 2 2 4 GMU – CS 571

Optimal Page Replacement Is there an algorithm that yields the minimal page fault rate for any reference string and a fixed number of frames? Replace the page that will not be used for the longest period of time  Algorithm OPT (MIN) Can be used as a yardstick for the performance of other algorithms What is the best we can do for the reference string we have been looking at? GMU – CS 571

Optimal Page Replacement GMU – CS 571

Optimal Page Replacement GMU – CS 571

Optimal Page Replacement For this reference string, 9 faults is the best we can do. GMU – CS 571

Least-Recently-Used (LRU) Algorithm Idea: Use the recent past as an approximation of the near future. Replace the page that has not been used for the longest period of time  Least-Recently-Used (LRU) algorithm GMU – CS 571

Least-Recently-Used (LRU) Algorithm GMU – CS 571

Least-Recently-Used (LRU) Algorithm GMU – CS 571

Least-Recently-Used (LRU) Algorithm GMU – CS 571

LRU and Belady’s Anomaly Neither OPT nor LRU suffers from Belady’s anomaly. In general, LRU gives good (low) page-fault rates. Run-time overhead of the exact LRU implementations is typically unacceptable. Counters (work like timestamps) Stack – when use a page, take it from the inside of the stack (if there), and push it on the top. LRU always on the bottom of the stack GMU – CS 571

LRU Approximations Real systems often employ LRU approximation algorithms, making use of the “reference” bits Ex: Additional-Reference-Bits Algorithm Each page keeps 8 bits initialized to 00000000 If a page gets touched (used), its leftmost bit gets changed to 1 At some interval, the bits get shifted right Page with the lowest value at some point in time is ‘LRU’ 00000000 – page has not been touched for 8 intervals 11111111 – page has been touched every interval for the last 8 GMU – CS 571

LRU Approximations Real systems often employ LRU approximation algorithms, making use of the “reference” bits Ex: Second-Chance Algorithm (Clock Algorithm) FIFO with the addition of an extra reference bit When a page is used, its reference bit is set to 1 When looking for a new page, use FIFO but skip pages whose reference bit = 1 If a page gets ‘skipped’, its reference bit is set to 0 and it is put at the end of the queue. Can be enhanced by adding consideration of the modification bit GMU – CS 571

Second Chance Page Replacement GMU – CS 571

Page-Buffering Algorithm In addition to a specific page-replacement algorithm, other procedures are also used. Systems commonly keep a pool of free frames. When a page fault occurs, the desired page is read into a free frame first, allowing the process to restart as soon as possible. The victim page is written out to the disk later, and its frame is added to the free-frame pool. Other enhancements are also possible. GMU – CS 571

Allocation of Frames How do we allocate the fixed number of free memory among the various processes? In addition to performance considerations, each process needs a minimum number of pages, determined by the instruction-set architecture. Since a page fault causes an instruction to restart, consider an architecture that contains the instruction ADD M1, M2, M3 (with three operands). Consider also the case where indirect memory references are allowed. GMU – CS 571

Global vs. Local Allocation Page replacement algorithms can be implemented broadly in two ways. Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another. Under global allocation algorithms, the page-fault rate of a given process depends also on the paging behavior of other processes. Local replacement – each process selects from only its own set of allocated frames. Less used pages of memory are not made available to a process that may need them. GMU – CS 571

Memory Allocation for Local Replacement Equal allocation – If we have n processes and m frames, give each process m/n frames. Proportional allocation – Allocate according to the size of process. Priority of processes? GMU – CS 571

CPU Utilization versus the Degree of Multiprogramming GMU – CS 571

Thrashing High-paging activity: The system is spending more time paging than executing. How can this happen? OS observes low CPU utilization and increases the degree of multiprogramming. Global page-replacement algorithm is used, it takes away frames belonging to other processes But these processes need those pages, they also cause page faults. Many processes join the waiting queue for the paging device, CPU utilization further decreases. OS introduces new processes, further increasing the paging activity. GMU – CS 571

Thrashing To avoid thrashing, we must provide every process in memory as many frames as it needs to run without an excessive number of page faults. Programs do not reference their address spaces uniformly/randomly A locality is a set of pages that are actively used together. According to the locality model, as a process executes, it moves from locality to locality. A program is generally composed of several different localities, which may overlap. GMU – CS 571

Locality in a Memory-Reference Pattern GMU – CS 571

Working-Set Model Introduced by Peter Denning A model based on locality principle The parameter , defines the working set window The set of pages in the most recent  page references of process Pi constitutes the working set GMU – CS 571

Working-Set Model The accuracy of the working set depends on the selection of : if  is too small, it will not encompass the entire locality. if  is too large, it will encompass several localities. if  =   will encompass the entire program. D =  WSSi  total demand of frames if D > the number of frames in memory  Thrashing The O.S. will monitor the working set of each process and perform the frame allocation accordingly. It may suspend processes if needed. Difficulty is how to keep track of the moving working-set window. GMU – CS 571

Working Sets and Page Fault Rates GMU – CS 571

Controlling Page-Fault Rate Maintain “acceptable” page-fault rate. If actual rate too low, process loses frame. If actual rate too high, process gains frame. GMU – CS 571