Wednesday, November 22, 2017

Similar documents
Wednesday, November 4, 2009

Module 9: Virtual Memory

Module 9: Virtual Memory

Operating Systems. Overview Virtual memory part 2. Page replacement algorithms. Lecture 7 Memory management 3: Virtual memory

Virtual Memory COMPSCI 386

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 15: Caching: Demand Paged Virtual Memory

Chapter 9: Virtual-Memory

Virtual Memory. Virtual Memory. Demand Paging. valid-invalid bit. Virtual Memory Larger than Physical Memory

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

Virtual Memory III. Jo, Heeseung

Chapter 9: Virtual Memory

Demand Paging. Valid-Invalid Bit. Steps in Handling a Page Fault. Page Fault. Transfer of a Paged Memory to Contiguous Disk Space

CS370 Operating Systems

All Paging Schemes Depend on Locality. VM Page Replacement. Paging. Demand Paging

Background. Demand Paging. valid-invalid bit. Tevfik Koşar. CSC Operating Systems Spring 2007

Lecture 14 Page Replacement Policies

Page Replacement Algorithms

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

CS370 Operating Systems

Page Replacement. 3/9/07 CSE 30341: Operating Systems Principles

Basic Page Replacement

Principles of Operating Systems

Operating Systems Lecture 6: Memory Management II

Where are we in the course?

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory

Chapter 10: Virtual Memory. Background

Background. Virtual Memory (2/2) Demand Paging Example. First-In-First-Out (FIFO) Algorithm. Page Replacement Algorithms. Performance of Demand Paging

Operating System Concepts

Chapter 4 Memory Management

Last Class. Demand Paged Virtual Memory. Today: Demand Paged Virtual Memory. Demand Paged Virtual Memory

Chapter 10: Virtual Memory. Background. Demand Paging. Valid-Invalid Bit. Virtual Memory That is Larger Than Physical Memory

CS370 Operating Systems

Memory Management. Virtual Memory. By : Kaushik Vaghani. Prepared By : Kaushik Vaghani

Swapping. Operating Systems I. Swapping. Motivation. Paging Implementation. Demand Paging. Active processes use more physical memory than system has

Chapter 8: Virtual Memory. Operating System Concepts Essentials 2 nd Edition

Operating System - Virtual Memory

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

CSC Operating Systems Spring Lecture - XIV Virtual Memory - II. Tevfik Ko!ar. Louisiana State University. March 27 th, 2008.

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory. Demand Paging

Virtual Memory: Page Replacement. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Virtual Memory - Overview. Programmers View. Virtual Physical. Virtual Physical. Program has its own virtual memory space.

Lecture 17. Edited from slides for Operating System Concepts by Silberschatz, Galvin, Gagne

Chapters 9 & 10: Memory Management and Virtual Memory

Optimal Algorithm. Replace page that will not be used for longest period of time Used for measuring how well your algorithm performs

CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement"

Virtual Memory. ICS332 Operating Systems

Chapter 8 Virtual Memory

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

CISC 7310X. C08: Virtual Memory. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/22/2018 CUNY Brooklyn College

CS420: Operating Systems

Memory management, part 2: outline

Lecture#16: VM, thrashing, Replacement, Cache state

Memory Management Outline. Operating Systems. Motivation. Paging Implementation. Accessing Invalid Pages. Performance of Demand Paging

CS 153 Design of Operating Systems Winter 2016

Operating System Principles: Memory Management Swapping, Paging, and Virtual Memory CS 111. Operating Systems Peter Reiher

Chapter 6: Demand Paging

Page 1. Goals for Today" TLB organization" CS162 Operating Systems and Systems Programming Lecture 11. Page Allocation and Replacement"

Memory management, part 2: outline. Operating Systems, 2017, Danny Hendler and Amnon Meisels

Chapter 9: Virtual Memory

ADRIAN PERRIG & TORSTEN HOEFLER Networks and Operating Systems ( ) Chapter 6: Demand Paging

Virtual Memory Management

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

Today s class. Operating System Machine Level. Informationsteknologi. Friday, November 16, 2007 Computer Architecture I - Class 12 1

VIRTUAL MEMORY READING: CHAPTER 9

3/3/2014! Anthony D. Joseph!!CS162! UCB Spring 2014!

Memory Management. Memory Management. G53OPS: Operating Systems. Memory Management Monoprogramming 11/27/2008. Graham Kendall.

CSE 120 Principles of Operating Systems

Chapter 8: Virtual Memory. Operating System Concepts

Chapter 9: Virtual Memory

Chapter 9: Virtual Memory

CS510 Operating System Foundations. Jonathan Walpole

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

ECE7995 Caching and Prefetching Techniques in Computer Systems. Lecture 8: Buffer Cache in Main Memory (I)

Memory Management. Chapter 4 Memory Management. Multiprogramming with Fixed Partitions. Ideally programmers want memory that is.

Chapter 9: Virtual Memory

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS 333 Introduction to Operating Systems. Class 14 Page Replacement. Jonathan Walpole Computer Science Portland State University

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

Virtual Memory. Chapter 8

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Objectives. Background. Virtual-address address Space

OPERATING SYSTEM. Chapter 9: Virtual Memory

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Chapter 9: Virtual Memory

Virtual Memory. Today.! Virtual memory! Page replacement algorithms! Modeling page replacement algorithms

MEMORY MANAGEMENT/1 CS 409, FALL 2013

CSCI 4500/8506 Operating Systems

Virtual Memory Outline

Q. How can we remove, or at least reduce, the relationship between program size and physical memory size?

!! What is virtual memory and when is it useful? !! What is demand paging? !! When should pages in memory be replaced?

First-In-First-Out (FIFO) Algorithm

Virtual Memory. Reading: Silberschatz chapter 10 Reading: Stallings. chapter 8 EEL 358

Chapter 3 - Memory Management

CS 333 Introduction to Operating Systems. Class 14 Page Replacement. Jonathan Walpole Computer Science Portland State University

Lecture 12: Demand Paging

Outline. 1 Paging. 2 Eviction policies. 3 Thrashing 1 / 28

Chapter 8 Virtual Memory

COMP 346 WINTER 2018 MEMORY MANAGEMENT (VIRTUAL MEMORY)

ECE519 Advanced Operating Systems

Last Class: Demand Paged Virtual Memory

Operating Systems. Operating Systems Sina Meraji U of T

Transcription:

Wednesday, November 22, 2017 Topics for today Storage management (Chapter 9) Main memory (3) Variable-partition multiprogramming (4) Paging (5) Virtual memory Page turning algorithms Summary of 5 memory models (3) Variable partition multiprogramming No real Operating System would adopt fixed partitions because it is too inflexible. It is better to let the number and size of the partitions be determined dynamically by the sizes of the programs being run. We thus have an environment similar to the way in which a dynamic memory heap operates. Requests for space of a certain size are made as programs are loaded and space is released as programs terminate. The operating system knows where the user programs are in memory so it keeps track of appropriate BASE and LIMIT values for each one and also where the free space is. The table of BASE and LIMIT values is variable in size and the entries are also variable (compare with Model 2). Free size 10 Free size 8 Free size 12 If we attempt to load a program of size B bytes there are three cases (i) there are one or more free areas of size B bytes or greater. For example, in the diagram we want to load a program of size 6. The operating system selects a partition. Strategies for doing this include: First fit - pick the first partition at least B bytes long (the one of size 10) Best fit pick the smallest partition at least B bytes long (the one of size 8) The program is loaded into the selected partition. Comp 162 Notes Page 1 of 11 November 22, 2017

(ii) (iii) the total free space available is < B bytes. For example, in the diagram we want to load a program of size 31. We have to wait until some current program(s) finish and frees up space The total free space is at least B bytes but there is no single free area of size B or greater. For example, in the diagram we want to load a program of size 20. If we could combine two or more free areas, we could get a free area large enough. The operating system could move programs to create the space but this is very time consuming. We probably have to wait as in case (ii). Problem with memory model 3 - requiring the program to occupy consecutive locations leads to instances of case (iii). Comp 162 Notes Page 2 of 11 November 22, 2017

(4) Paging If we can devise an appropriate address translation mechanism then there is no requirement that a program occupy consecutive locations; we can scatter a program in several pieces all over memory. Thus, we can load a program if the total amount of free space is sufficient, even if it is in disjoint pieces. In order to make address translation efficient, we need to standardize the way in which the program address space (the set of addresses a program can reference) and main memory address space (the actual physical memory) are divided. We will divide main memory into "page frames" of size 2 k bytes (e.g., 512 bytes or 2048 bytes). We divide user programs (transparently to the user) into "pages" also 2 k bytes in size so that any page can go into any page frame. Suppose we have a 4-page program A (pages designated A0, A1, A2, and A3) and a 2-page program B (pages designated B0 and B1). A snap-shot of a main memory containing 7 page frames for user programs might be Frame Contents 0 A1 1 <empty> 2 A0 3 A2 4 B1 5 A3 6 B0 The Operating System would also maintain a Page Table for each program showing where its pages are in main memory. The page table for program A in the above example is Address Translation Page Frame # 0 2 1 0 2 3 3 5 How is a logical address (the address the program thinks it is referencing) translated into a physical address (the address used to access real memory)? If we assume that there are N bits in an address and that the page size is 2 k bytes then a logical (program) address has two parts: Comp 162 Notes Page 3 of 11 November 22, 2017

Page Number (N-k bits) Address-within-Page (k bits) The address-within-page gives us the least significant k bits of the physical address (addresswithin-page frame). The most significant part of the physical address (page frame number) we get by using the page number part of the logical address as an index into the page table. The number we find there is the page frame number. Thus Logical Address Page Number (N-k bits) Address-within-Page (k bits) Page table for program Page # Frame # Page Frame Number (M-k bits) Address-within-Page-frame (k bits) Physical Address This also solves the problem of programs interfering with each other. If the page number gives an index beyond the size of the page table for the program we flag an error. The Operating System maintains a Page Frame Table. When a particular program terminates it marks the appropriate frames as free. A program can be loaded if there is a sufficient number of empty frames. Problem of memory model 4 - a program having more pages than there are empty page frames cannot be loaded even though there is room for part of the program in memory. Comp 162 Notes Page 4 of 11 November 22, 2017

(5) Virtual memory In a Virtual Memory system it is not necessary to have all of a program in memory at once. In theory we only need the page containing the current instruction and the page containing the data item it refers to. If we permit partial programs in main memory then: (i) (ii) The Operating System can hold parts of many programs and have more to choose from when deciding which program to run next programs can be bigger than main memory so programmers are writing programs for a "virtual" memory which the operating system then maps onto the real one. This makes programs more portable, they do not require actual memories of a certain size to run. We need to have more sophisticated Page Tables (for user programs) and Page Frame Table (for the operating system. Consider the following snap-shot of main memory Frame Contents 0 C2 1 B1 2 <empty> 3 D1 4 A0 5 C0 6 A3 The Virtual Memory Page Table for program A might look like Page In memory? Frame Disc Address 0 Y 4 nnnnnn 1 N - nnnnnn 2 N - nnnnnn 3 Y 6 nnnnnn What if a program attempts to access a page that is not in main memory? That is, what happens when we access the page table and we find "N" in the "In memory?" field? This situation is termed a "page fault" (it is not really an error or fault on the programmer's part or the program's part). The Operating System suspends the program until the required page is brought into memory then it is restarted at the instruction that caused the fault. Comp 162 Notes Page 5 of 11 November 22, 2017

Page turning If, when we need to bring a page in, there is an empty page frame then there is no problem. Q: What if the page frames are all full? A: We need an algorithm to decide which page to kick out of memory to make room for the new one. Q: Can we just overwrite the page frame we have chosen? A: Not if its contents have changed since it was brought into memory Q: How can we tell if we have to save the page frame contents back to disc? A: Use a flag ("dirty bit") in the page frame table to indicate if the page is dirty (has been modified) or clean (has not been modified). The Page Frame Table now has more columns than before Frame Contents Dirty? Sticky? Disc Address Usage The Sticky bit enables us to exempt certain frames from being swapped out. Perhaps they contain part of a program that needs to be in memory (real-time control?). The disc address field tells us where to write the page back. The "Usage" field may be needed by one or more of the algorithms that we use to decide which page frame to overwrite. Analogy The algorithm the operating system uses to decide which frame to overwrite is similar to (a) One a supermarket manager might use to determine which of N brands of cereal to stop selling when a new brand comes along. (b) One you might use when your Rolodex TM is full and a new phone number needs to be recorded. Which card do you get rid of? Page-turning algorithms. Here are four possible algorithms for choosing a frame to overwrite: (a) Random - pick a frame at random. This is a low-overhead solution. We don t need to keep any records. We can use some bits from the system clock to give us a random enough value. However, this algorithm may throw out a valued page so its use is not recommended. Comp 162 Notes Page 6 of 11 November 22, 2017

(b) FIFO (First-In-First-Out) pick the page that has been in memory longest. Requires that we timestamp a frame when we bring in a page. Search for the frame with that has been in the memory longest. However, the page chosen by FIFO might turn out to be a page that is heavily used. Consider the supermarket analogy (c) LFU (Least Frequently Used) This requires that we keep a counter on each frame. Reset it to zero when we load in a page and increment it every time we use the frame. Pick the page that has been accessed the smallest number of times. However, LFU discriminates against pages brought in recently. (d) LRU (Least Recently Used) - i.e. idle longest. Like (b) timestamp a frame when we bring in a page. However, we change the timestamp whenever we use the frame. This algorithm seems reasonable and gives good results in practice. A fifth algorithm: Optimal (theoretical interest only) (e) Discard the page whose next reference is furthest in the future (this is why it is theoretical). If the stream of page references is known, this is an algorithm against which the others can be measured. Least Recently Used: implementation One way to implement an LRU algorithm is to have a (conceptual) queue of page frame numbers. When a frame is accessed it is moved to the front of the queue. When we need to pick a frame to overwrite we pick the one at the end of the queue this will be the one least recently used. Example - memory with 4 frames for user programs. The frame references marked with * above are the ones that cause a page fault. The first four of these do not require a call on the page-turning algorithm. Comp 162 Notes Page 7 of 11 November 22, 2017

Time Page * * * * * * * * * * * Referenced 2 3 2 1 2 3 4 1 5 2 6 2 7 6 5 1 6 8 6 5 1 9 Frame 0 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Frame 1-3 3 3 3 3 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1 1 Frame 2 - - - 1 1 1 1 1 1 1 1 1 7 7 7 7 7 8 8 8 8 9 Frame 3 - - - - - - 4 4 4 4 6 6 6 6 6 6 6 6 6 6 6 6 "Queue" 0 1 0 2 0 1 3 2 0 1 3 1 2 3 0 1 3 2 3 0 1 2 0 1 0 2 0 1 3 2 0 1 3 1 2 3 0 1 3 2 3 0 1 1 1 2 0 1 3 2 0 0 3 1 2 3 0 1 1 2 3 0 2 0 1 3 2 2 0 0 1 2 2 0 0 1 2 3 Note that at the point where page 7 is referenced, the Optimal algorithm would discard page 2 because it is never referenced again. Another way to implement LRU is with an array of bits. If we have N frames then we use an N by N bit matrix initially set to all 0s. When frame I is referenced we set row I to all 1s and column I to all 0s. The LRU frame has all zeros in its row (there may be more than one such frame). Summary of the five memory models >1 program in memory? A program is in contiguous area? All of a program must be in memory? Uniprogramming No Yes Yes Partitioned main memory Yes Yes Yes Paging Yes No Yes Virtual memory Yes No No Virtual memory: Working set model We have seen "demand paging" - no page is brought into memory until it is required. An alternative strategy would anticipate the pages that a program will need when it runs next and arrange for these to be pre-loaded into the memory. While it is possible that a page will be loaded that the program will not use, in general this strategy would reduce page faults. The OS will base its guess as to the pages required on a record of the pages the program has been using in the recent past. This is the "working set" of pages for the program. The working set changes relatively slowly over time for most programs. It is defined at the set of pages used by the Comp 162 Notes Page 8 of 11 November 22, 2017

program in the last k memory references. Here is an example for k=4. In our example the set changes much faster than in a real program. time page-reference ws(t,4) 0 6 { 6 } 1 1 { 1 6 } 2 6 { 1 6 } 3 1 { 1 6 } 4 6 { 1 6 } 5 2 { 1 2 6 } 6 6 { 1 2 6 } 7 2 { 2 6 } 8 6 { 2 6 } 9 3 { 2 3 6 } 10 7 { 2 3 6 7 } 11 3 { 3 6 7 } 12 7 { 3 7 } 13 2 { 2 3 7 } 14 7 { 2 3 7 } 15 4 { 2 4 7 } 16 8 { 2 4 7 8 } 17 2 { 2 4 7 8 } 18 8 { 2 4 8 } 19 3 { 2 3 8 } 20 8 { 2 3 8 } 21 3 { 3 8 } 22 6 { 3 6 8 } 23 4 { 3 4 6 8 } Note that if this program were suspended at time t=15, we would assume that it is using page 7 and arrange for that to be loaded ahead of the program running again. In fact the program never used page 7 again. Reading Read end of 9.2 for virtual memory. If you want to read ahead, look at 9.4 and 9.5. Comp 162 Notes Page 9 of 11 November 22, 2017

Review Questions 1. If we have the page table, can we determine the virtual address that mapped to a particular physical address? 2. In what way does a self-modifying program negatively impact virtual memory? 3. Consider the example memory trace on page 8. If the next reference is to page 7, (a) what pages are then in memory (b) what does the queue look like? 4. Think of another situation where a user may have to choose an item to be removed from a finite list in order to make room for a new one. 5. In what ways is model 3 similar to a heap (C implementation)? Any differences? Comp 162 Notes Page 10 of 11 November 22, 2017

Review Answers 1. Yes. Just reverse the process by which the physical address is derived. 2. By making code pages dirty. 3. (a) 5 1 9 7 (b) 3 2 1 0 4. For example, a car navigation system with room for a finite number of addresses or a TV box with room fora finite number of favorite channnels. 5. When a program is loaded, the space request is similar to malloc When a program ends and its space is released, this is similar to free. There is no memory that is neither free nor in use so there is no equivalent to memory leaks. Comp 162 Notes Page 11 of 11 November 22, 2017