a process may be swapped in and out of main memory such that it occupies different regions

Similar documents
COMP 346 WINTER 2018 MEMORY MANAGEMENT (VIRTUAL MEMORY)

Virtual Memory. Chapter 8

Chapter 8 Virtual Memory

Memory Management Virtual Memory

Chapter 8 Virtual Memory

MEMORY MANAGEMENT/1 CS 409, FALL 2013

ECE519 Advanced Operating Systems

Chapter 8 Virtual Memory

Role of OS in virtual memory management

Virtual Memory. Chapter 8

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

Process size is independent of the main memory present in the system.

Virtual Memory. control structures and hardware support

Operating Systems CSE 410, Spring Virtual Memory. Stephen Wagner Michigan State University

Chapter 8. Virtual Memory

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Virtual to physical address translation

Week 2: Tiina Niklander

CPE300: Digital System Architecture and Design

Operating Systems, Fall

Operating Systems: Internals and Design Principles. Chapter 7 Memory Management Seventh Edition William Stallings

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

Virtual Memory Outline

Perform page replacement. (Fig 8.8 [Stal05])

Memory Management Virtual Memory

Virtual or Logical. Logical Addr. MMU (Memory Mgt. Unit) Physical. Addr. 1. (50 ns access)

Operating Systems. Operating Systems Sina Meraji U of T

Operating Systems Lecture 6: Memory Management II

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

Chapter 8: Virtual Memory. Operating System Concepts

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

First-In-First-Out (FIFO) Algorithm

CS6401- Operating System UNIT-III STORAGE MANAGEMENT

1. Background. 2. Demand Paging

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

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

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

Readings and References. Virtual Memory. Virtual Memory. Virtual Memory VPN. Reading. CSE Computer Systems December 5, 2001.

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

OPERATING SYSTEM. Chapter 9: Virtual Memory

Chapter 9: Virtual Memory

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

Principles of Operating Systems

Paging Policies, Load control, Page Fault Handling, Case studies January WT 2008/09

Address spaces and memory management

Memory Hierarchy. Goal: Fast, unlimited storage at a reasonable cost per bit.

Page Size Page Size Design Issues

Virtual Memory COMPSCI 386

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

Chapter 4 Memory Management

Operating System Support

The Memory System. Components of the Memory System. Problems with the Memory System. A Solution

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

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

Memory and multiprogramming

Memory Management Cache Base and Limit Registers base limit Binding of Instructions and Data to Memory Compile time absolute code Load time

CS370 Operating Systems

Chapter 3 - Memory Management

Operating System Concepts

Operating Systems Memory Management. Mathieu Delalandre University of Tours, Tours city, France

Operating Systems, Fall Lecture 5 1

Virtual Memory. Patterson & Hennessey Chapter 5 ELEC 5200/6200 1

Page Replacement Algorithms

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Virtual Memory Management

Chapter 9: Virtual Memory

CS307: Operating Systems

Registers Cache Main memory Magnetic disk Magnetic tape

Lecture 12: Demand Paging

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

Chapter 9: Virtual-Memory

Memory Management. To improve CPU utilization in a multiprogramming environment we need multiple programs in main memory at the same time.

Chapter 9: Virtual Memory

Objectives and Functions Convenience. William Stallings Computer Organization and Architecture 7 th Edition. Efficiency

Memory: Page Table Structure. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Processes and Tasks What comprises the state of a running program (a process or task)?

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Operating Systems, Fall Lecture 5 1. Overhead due to page table and internal fragmentation. Tbl 8.2 [Stal05] 4.

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

Virtual Memory: From Address Translation to Demand Paging

Operating Systems Design Exam 2 Review: Spring 2011

CS 416: Opera-ng Systems Design March 23, 2012

Lecture 2: Memory Systems

Operating System - Virtual Memory

Chapter 8. Operating System Support. Yonsei University

Virtual Memory: From Address Translation to Demand Paging

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

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

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

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

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

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

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

The levels of a memory hierarchy. Main. Memory. 500 By 1MB 4GB 500GB 0.25 ns 1ns 20ns 5ms

Chapter 9: Virtual Memory

Chapter 8: Memory-Management Strategies

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Operating Systems (2INC0) 2017/18

Virtual Memory III. Jo, Heeseung

Transcription:

Virtual Memory

Characteristics of Paging and Segmentation A process may be broken up into pieces (pages or segments) that do not need to be located contiguously in main memory Memory references are dynamically translated into physical addresses at run time a process may be swapped in and out of main memory such that it occupies different regions These characteristics lead to a breakthrough: it is not necessary for all pages/segments of a process to be in main memory during execution Execution can proceed as long as the instruction and data needed next are in main memory

Process Execution, I. The term piece here refers to either page or segment The OS brings into main memory only a few pieces of the program (including its starting point) Each page/segment table entry has a present bit that is set only if the corresponding piece is in main memory The resident set is the portion of the process that is in main memory Execution proceeds smoothly as long as all memory references are within the resident set

Process Execution, II. An interrupt (memory fault) is generated when the memory reference is on a piece not present in main memory OS issues a disk I/O read request to bring into main memory the piece containing the reference OS places the process in a blocked state another process is dispatched to run while the disk I/O takes place an interrupt is issued when the disk I/O completes this causes the OS to place the affected process in the Ready state

Advantages of Partial Loading More processes can be maintained in main memory We only load some of the pieces of each process, so there is room for more processes with more processes in main memory, it is more likely that a process will be in the Ready state at any given time, improving processor utilization A process can now execute even if it is larger than the main memory size No need of overlays OS will automatically load the process in pieces

Virtual Memory Physical memory (main memory or real memory) is the memory referenced by a physical address located on DRAM The memory referenced by a logical address is called virtual memory is maintained on secondary memory (ex: disk) Programmer perceives much larger memory than real memory pieces are moved into main memory only when needed for better performance, the file system is often bypassed and virtual memory is stored in a special area of the disk called the swap space

Possibility of Thrashing To accommodate as many processes as possible, only a few pieces of each process are maintained in main memory But main memory may be full: when the OS brings one piece in, it must swap one piece out The OS must not swap out a piece of a process just before that piece is needed If it does this too often this leads to thrashing: the processor spends most of its time swapping pieces rather than executing user instructions

Principle of Locality Principle of locality states that program and data references within a process tend to cluster Hence, only a few pieces of a process will be needed over a short period of time Use principle of locality to make intelligent guesses about which pieces will be needed in the near future to avoid thrashing

Support Needed for Virtual Memory Memory management hardware must support paging and/or segmentation OS must be able to manage the movement of pages and/or segments between secondary memory and main memory We will first discuss the hardware aspects; then the algorithms used by the OS

Paging, I. Each page table entry contains a present bit to indicate whether the page is in main memory or not. if it is in main memory, the entry contains the frame number of the corresponding page in main memory if it is not in main memory, the entry may contain the address of that page on disk or the page number may be used to index another table (often in the PCB) to obtain the address of that page on disk Typically, each process has its own page table

Paging, II. A modified bit indicates if the page has been altered since it was last loaded into main memory If no change has been made, the page does not have to be written to the disk when it needs to be swapped out Other control bits may be present if protection or sharing is managed at the page level a read-only/read-write bit protection level bit: kernel page or user page (more bits are used when the processor supports more than 2 protection levels)

Address Translation in a Paging System Translate virtual (logical) address into real (physical) address using page table Virtual address: (page #, offset) Real address: (frame #, offset) Page # is used to index the page table and look up the corresponding frame # Frame # combined with the offset produces the real address

Address Translation in a Paging System

Page Table Structure Length of page tables depends on process size they must be in main memory instead of registers a single register holds the starting physical address of the page table of the currently running process Thus each virtual memory reference causes at least two physical memory accesses one to fetch the page table entry and one to fetch data To overcome this problem a special high-speed cache called the TLB - Translation Lookaside Buffer is used contains page table entries that have been recently used This scheme improves performance because of the principle of locality

Translation Lookaside Buffer TLB works similar to main memory cache Given a logical address, the processor examines the TLB If page table entry is present (a hit), the frame # is retrieved and the real (physical) address is formed If page table entry is not found in the TLB (a miss), the page # is used to index the process page table if present bit is set then the corresponding frame is accessed if not, a page fault is issued to bring in the referenced page into main memory The TLB is updated to include the new page table entry

Use of a Translation Lookaside Buffer

TLB: Further comments Each TLB entry consists of a page # and the corresponding page table entry TLB simultaneously interrogates all entries to find a match on page number The TLB must be flushed each time a new process enters the Running state The CPU uses two levels of cache on each virtual memory reference first the TLB: to convert the logical address to the physical address once the physical address is formed, the CPU then looks in the cache for the referenced word

Page Tables and Virtual Memory Most computer systems support a very large virtual address space 32 to 64 bits are used for logical addresses if (only) 32 bits are used with 4KB pages, a page table may have 2 20 entries The entire page table may take up too much main memory. Hence, page tables are often also stored in virtual memory and subjected to paging when a process is running, part of its page table must be in main memory (including the page table entry of the currently executing page)

Multilevel Page Tables Since a page table will generally require several pages to be stored. One solution is to organize page tables into a multilevel hierarchy when 2 levels are used (ex: 386, Pentium), the page number is split into two numbers p1 and p2 First level: root page table (outer page table or page directory) Second level: process page table (user page table) The root page table entries point to pages of the process page table p1 indexes the outer page table and p2 indexes the resulting page in the process page table Page directory is always kept in main memory, but part of the process page table may be swapped out.

Two-level Page Tables

Two-level Page Tables page of process

The Page Size Issue, I. Page size is defined by hardware; always a power of 2 for more efficient logical to physical address translation. But exactly which size to use is a difficult question: small page size is good to minimize internal fragmentation large page size is good since for a small page size, more pages are required per process More pages per process means larger page tables. large page size is good since disks are designed to efficiently transfer large blocks of data larger page sizes means more of a process in main memory; after a certain point, this increases the TLB hit ratio

The Page Size Issue, II. Page sizes from 1KB to 4KB are most commonly used But the issue is non trivial. Hence some processors are now supporting multiple page sizes. Ex: Pentium supports 2 sizes: 4KB or 4MB R4000 supports 7 sizes: 4KB to 16MB See discussion in text, p. 348 Ex.: Larger sizes for program instructions Smaller sizes for thread stacks

Sharing Pages If we share the same code among different users, it is sufficient to keep only one copy in main memory Shared code must be reentrant so that 2 or more processes can execute the same code If we use paging, each sharing process s page table will have entries pointing to the same frames: only one copy is in main memory But each user needs to have its own private data pages

Sharing Pages: A Text Editor

Segmentation Typically, each process has its own segment table Similarly to paging, each segment table entry contains a present bit and a modified bit If the segment is in main memory, the entry contains the starting (base) address and the length of that segment Other control bits may be present if protection and sharing is managed at the segment level Logical to physical address translation is similar to paging except that the offset is added to the starting address (instead of being appended)

Address Translation in a Segmentation System Physical address

Segmentation: Comments In each segment table entry we have both the starting address and length of the segment the segment can thus dynamically grow or shrink as needed address validity easily checked with the length field but variable length segments introduce external fragmentation and are more difficult to swap in and out It is natural to provide protection and sharing at the segment level since segments are visible to the programmer (pages are not) Useful protection bits in segment table entry: read-only/read-write bit supervisor/user bit

Sharing in Segmentation Systems Segments are shared when entries in the segment tables of two different processes point to the same physical locations only one copy is kept in main memory But each user would still need to have its own private data segment

Sharing of Segments: Text Editor

Combined Segmentation and Paging To combine their advantages some processors and OS page the segments Several combinations exists. Here is a simple one Each process has: one segment table several page tables: one page table per segment The virtual address consists of: a segment number: used to index the segment table whose entry gives the starting address of the page table for that segment a page number: used to index that page table to obtain the corresponding frame number an offset: used to locate the word within the frame

Address Translation in a combined Segmentation/Paging System

Simple Combined Segmentation and Paging The Segment Base is the physical address of the page table of that segment Present and modified bits are present only in page table entry Protection and sharing info most naturally resides in segment table entry example, a read-only/read-write bit, a kernel/user bit...

Operating System Software Memory management software depends on whether the hardware supports paging or segmentation or both Pure segmentation systems are rare. Segments are usually paged -- memory management issues are then those of paging We shall thus concentrate on issues associated with paging To achieve good performance we need a low page fault rate

OS Policies for Virtual Memory Fetch policy Demand paging Prepaging Placement policy Replacement policy Basic algorithms Optimal Least recently used(lru) First in, First out (FIFO) Clock Page buffering Resident set management Resident set size Fixed Variable Replacement scope Global Local Cleaning policy Demand Precleaning Load control Degree of multiprogramming

Fetch Policy Determines when a page should be brought into main memory. Two common policies: Demand paging only brings pages into main memory when a reference is made to a location on the page many page faults when process first started but should decrease as more pages are brought in Prepaging brings in more pages than demanded; hopefully the extra pages brought in will soon be referenced it is more efficient to bring in pages that reside contiguously on the disk all at once efficiency not definitely established: the extra pages brought in may not be referenced

Placement Policy Determines where in real memory a process piece resides For pure segmentation systems: best-fit, first-fit, next fit are possible choices (a real issue) For paging (and paged segmentation): the chosen frame location is irrelevant since all memory frames are equivalent (not an issue)

Replacement Policy, I. Deals with the selection of a page in main memory to be replaced when a new page is brought in This occurs whenever main memory is full (no free frame available) The page that is selected for replacement should be the one that is least likely to be referenced in the near future The principle of locality enables prediction of future referencing behavior based on past behavior

Replacement Policy, II. Not all pages in main memory can be selected for replacement Some frames cannot be paged out (locked) like kernel, key control structures, I/O buffers The OS might decide that the set of pages considered for replacement should be: limited to those of the process that has caused the page fault the set of all pages in unlocked frames of memory

Optimal policy Replacement Algorithms selects for replacement the page for which the time to the next reference is the longest produces the fewest number of page faults impossible to implement (need to know the future) but serves as a standard to compare with the other algorithms we shall study Least recently used (LRU) First-in, first-out (FIFO) Clock

LRU Policy Replaces the page that has not been referenced for the longest time by the principle of locality, this page is least likely to be referenced in the near future performs nearly as well as the optimal policy Example: A process of 5 pages with an OS that fixes the resident set size to 3

Note on counting page faults When the main memory is empty, each new page we bring in is a result of a page fault For the purpose of comparing the different algorithms, we are not counting these initial page faults because the number of these is the same for all algorithms But, in contrast to what is shown in the figures, these initial references are really producing page faults

Implementation of the LRU Policy Each page could be tagged (in the page table entry) with the time of its last reference Requires tagging at each memory reference The LRU page is the one with the smallest time value (needs to be searched at each page fault) This would require expensive hardware and a great deal of overhead Therefore, other algorithms are used instead

FIFO Policy Treats page frames allocated to a process as a circular buffer; pages removed in round-robin style When the buffer is full, the page that has been in memory the longest is replaced. Hence, first-in, first-out A page fetched into memory a long time ago may now have fallen out of use But a frequently used page is often the oldest, so it will be repeatedly paged out by FIFO Simple to implement requires only a pointer that circles through the page frames of the process

Comparison of FIFO with LRU LRU recognizes that pages 2 and 5 are referenced more frequently than others but FIFO does not FIFO performs relatively poorly

Clock Policy The set of frames that are candidates for replacement (local or global scope) is considered as a circular buffer When a page is replaced, a pointer is set to point to the next frame in buffer A use bit for each frame is set to 1 whenever a page is first loaded into the frame the page is referenced When it is time to replace a page, the first frame encountered with the use bit set to 0 is replaced during the search for replacement, each use bit set to 1 is changed to 0

Clock Policy: An Example

Comparison of Clock with FIFO and LRU Asterisk indicates that the corresponding use bit is set to 1 Clock protects frequently referenced pages by setting the use bit to 1 at each reference

Clock policy: Comments If all frames have a use bit of 1, then the pointer will make one full circle setting all use bits to 0. It stops at the starting position because it can now replace the page in that frame. Similar to FIFO, except frames with use bit set to 1 are passed over Numerical experiments tend to show that performance of Clock is close to that of LRU Many OS es use variations of the clock algorithm. Example: Solaris

Reference www.cs.ucf.edu/~lboloni/teaching/.../08- VirtualMemory.ppt