Paging and Page Replacement Algorithms

Similar documents
CS 550 Operating Systems Spring Memory Management: Page Replacement

Chapter 4 Memory Management

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

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

Week 2: Tiina Niklander

Operating Systems, Fall

Chapter 4 Memory Management. Memory Management

Perform page replacement. (Fig 8.8 [Stal05])

Virtual Memory Design and Implementation

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

Memory management, part 2: outline

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

Chapter 4: Memory Management. Part 1: Mechanisms for Managing Memory

Page replacement algorithms OS

Memory Management Prof. James L. Frankel Harvard University

CS510 Operating System Foundations. Jonathan Walpole

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

CS450/550 Operating Systems

Operating Systems Lecture 6: Memory Management II

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

Virtual Memory Design and Implementation

Memory: Paging System

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Virtual Memory Design and Implementation

Clock page algorithm. Least recently used (LRU) NFU algorithm. Aging (NFU + forgetting) Working set. Process behavior

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Virtual Memory III. Jo, Heeseung

CS370 Operating Systems

Chapter 8: Virtual Memory. Operating System Concepts

Move back and forth between memory and disk. Memory Hierarchy. Two Classes. Don t

Memory Management Ch. 3

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

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

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

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Basic Memory Management

Virtual Memory Management

OPERATING SYSTEM. Chapter 9: Virtual Memory

Address spaces and memory management

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

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

Operating System Concepts

Chapter 10: Virtual Memory. Background

Virtual Memory Outline

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

Chapter 9: Virtual Memory

CS307: Operating Systems

Motivation. Memory Management. Memory Management. Computer Hardware Review

Chapter 6: Demand Paging

Chapter 9: Virtual Memory

Chapter 8 Virtual Memory

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

Paging algorithms. CS 241 February 10, Copyright : University of Illinois CS 241 Staff 1

Operating System Concepts 9 th Edition

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

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

Chapter 10: Virtual Memory

Operating Systems. Operating Systems Sina Meraji U of T

Lecture 12: Demand Paging

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

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

CS370 Operating Systems

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

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

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

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

Modeling Page Replacement: Stack Algorithms. Design Issues for Paging Systems

Where are we in the course?

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

Chapter 9: Virtual Memory

Chapter 9: Virtual Memory

CIS Operating Systems Memory Management Cache Replacement & Review. Professor Qiang Zeng Fall 2017

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

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

Page Replacement Chap 21, 22. Dongkun Shin, SKKU

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Chapter 3: Important Concepts (3/29/2015)

Swapping. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture 14 Page Replacement Policies

Chapter 9: Virtual Memory

Past: Making physical memory pretty

Global Replacement Algorithms (1) Ken Wong Washington University. Global Replacement Algorithms (2) Replacement Example (1)

VIRTUAL MEMORY READING: CHAPTER 9

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

CIS Operating Systems Memory Management Page Replacement. Professor Qiang Zeng Spring 2018

CS420: Operating Systems

Lecture 14: Cache & Virtual Memory

Swapping. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 153 Design of Operating Systems Winter 2016

Chapter 9: Virtual-Memory

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

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

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

Last Class: Demand Paged Virtual Memory

First-In-First-Out (FIFO) Algorithm

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

CS370 Operating Systems

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

Transcription:

Paging and Page Replacement Algorithms Section 3.4 Tanenbaum s book Kartik Gopalan

OS Involvement with Page Table Management Four times when OS deals with page-tables 1. Process creation create page table 2. Upon context switch Load MMU context for a new process (e.g. load CR3 register with base address of page table) TLB may be flushed 3. Page fault time determine the virtual address causing fault (read CR2 register for faulting address) swap target page out, bring needed page in 4. Process termination time release page table and other pages

Page Fault Page Fault is a hardware exception It occurs when a process accesses a virtual page 1. that is currently not resident in physical memory OR 2. for which it doesn t have the correct access permissions OR 4. which hasn t yet been allocated by the operating system Virtual Pages unmapped unmapped mapped unmapped mapped unmapped mapped mapped Translation for mapped pages are handled by MMU Accessing unmapped or protected pages causes page fault Physical Page

Page-fault handling 1. Process accesses unmapped/protected virtual address 2. MMU hardware raises trap Because it cannot find a valid page-table entry 3. Process is paused Program counter and Registers saved 4. OS runs page-fault handler (PFH) PFH examines the virtual address which caused the fault Is this a write to an unallocated page? PFH allocates a new physical page Is this an already allocated page? PFH reads the page from the swap device (page-in) PFH updates the page-table entry. Is this a disallowed access? E.g. writing to a read-only page? PFH terminates the process 5. Process resumes from where it left off (unless terminated by PFH). Program counter and Registers restored OS may periodically page-out victim page to disk to create space 4

Locking Pages in Memory Virtual memory and I/O occasionally interact Problem Process P1 issues call for read from device into buffer While waiting for I/O, another processes P2 starts up P2 has a page fault Buffer for the P1 may be chosen as victim to be paged out, resulting in a DMA error. Need to specify some pages as locked or pinned in memory These pages are exempted from being victim pages

Page Replacement Resident pages must be occasionally evicted from memory to make room for new pages Pages that have not been modified can be simply discarded i.e. overwritten with new content Modified pages must be saved before reuse unmodified pages are simply overwritten Ideally, we d like to evict pages that are least needed. Goal of page replacement algorithm minimize the number of page faults E.g. if we evict an often used page, we may need to bring back in soon resulting in more page faults.

Page Replacement Algorithms q Optimal Page Replacement Least Recently Used (LRU) q Not Recently Used (NRU) q FIFO q Second Chance q Clock Not Frequently Used (NFU) Aging Working Set WSClock

Optimal Page Replacement Algorithm (OPR) Replace the page that s NOT needed for the longest time in the future Page access timeline 1 2 3 4 1 5 2 4 Assume we have 3 frames in physical memory Eviction Decisions Frame 0 t0 t1 t2 t3 t4 t5 t6 t7 1 1 1 1 1 5 5 5 Frame 1 Frame 2 2 2 2 2 2 2 2 3 4 4 4 4 4 8

Optimal Page Replacement Algorithm (OPR) Why is it optimal? Here, Optimal means that no other algorithm can give fewer page faults than OPR. OPR is optimal but unrealizable Because we cannot predict all future memory accesses But OPR can serve as a useful baseline to measure the effectiveness of other algorithms How do we know its optimal? Try proof by contradiction What if you can find another algorithm which does not pick the page needed farthest point in the future and can still yield fewer page faults than OPT? Other algorithms try to approximate OPR Estimate the pages not needed for a long time by recording the sequence of pages used in the past.

Least Recently Used (LRU) Assume pages used recently will be used again soon throw out page that has not been used for longest time in the past Ideally: Must keep a linked list of pages most recently used at front, least at rear update this list every memory reference!! Not practical. Aging: Alternatively keep counter in each page table entry Counter tracks the number of references to a page Choose page with lowest value counter Periodically zero the counter Note: This only approximates LRU Implementation details in Tanenbaum Section 3.4.7

(Recall) Page Table Entry (PTE) q Referenced bit: Whether the page was accessed since last time the bit was reset. q Modified bit: Also called Dirty bit. Whether the page was written to, since the last time the bit was reset. q Present/Absent bit: Whether the PTE contains a valid page frame #. Used for marking swapped/unallocated pages. 11

Not Recently Used Page Replacement Algorithm NRU periodically scans all PTEs After each scan, pages are classified as 1. not referenced, not modified (0,0) 2. not referenced, modified (0,1) 3. referenced, not modified (1,0) 4. referenced, modified (1,1) When selecting a victim page remove page from the lowest numbered non-empty class Also reset all referenced bits Implies that pages are moved from 3 1 and from 4 2 Before the next NRU scan, some pages may be referenced or modified again Implies some pages may move back from 1 >3, 1 >4, 2 >4, 3 >4 Advantage: Simple to implement Disadvantage: Doesn t distinguish among pages within the same class

FIFO Page Replacement Algorithm Maintain a linked list of all pages in order they came into memory Page at beginning of list replaced Disadvantage page in memory for the longest time may be often used

Second Chance Page Replacement Algorithm FIFO List New FIFO list: if fault occurs at time 20, A has R bit set Pages are sorted in FIFO order Basic idea: Select the oldest page that has not been recently used. How it works: When selecting a victim page: 1. Look at the oldest page in the list. 2. If R bit of the page is set, then set R=0 and move the page to the front of the FIFO list; go back to step 1 3. Select the oldest page with R=0 as the victim page. 4. If no page with R=0 (unlikely), then select the oldest page (FIFO)

The Clock Page Replacement Algorithm ; Put new page in its position Logically the same as Second Chance, except that it doesn t keep moving pages around the linked list. Hence it is more memory-efficient.

The Working Set Page Replacement Algorithm (1) K Working Set: The set of all pages that a process is currently using. In theory, if the entire working set is in memory, then there ll be no page faults. In practice, predicting the exact working set is impossible. Assume, the working set is the set of pages used by the K most recent memory references For typical processes w(k,t) the size of the working set at time t becomes constant over time

The Working Set Page Replacement Algorithm (2) Keep track of the working set of a process When its time to evict a page, choose one which is not in the working set of the process. If no page with (R==0) and (age > τ) evict the page with R==0 and smallest time

Internal versus External Fragmentation Internal Fragmentation Occurs when part of memory allocated to a process remains unused. E.g. a process may be allocated a 4KB page, but it uses only 1-byte in the page. External fragmentation Occurs when none of the available free memory fragments is big enough to satisfy a new memory allocation request. E.g. a process may request 16KB contiguous physical memory allocation, but all available free memory is of size less than 16KB. Allocated Free 8KB 8KB 4KB 4KB Can a virtual memory paging system have Internal fragmentation? Yes External fragmentation? No. Why?

Periodic Eviction Policy Need for a background process, paging daemon periodically checks memory pressure and evicts pages from memory When to evict? Based on Low and High watermarks. Two thresholds of memory usage When memory usage is above High watermark, say 90%, indicating high memory pressure Start evicting pages till memory usage falls below Low watermark, say 80% Why two thresholds? To enable hysteresis So that the system state doesn t fluctuate rapidly around a single threshold.

Thrashing Despite good designs, system may still thrash when some processes need more memory but no processes need less Thrashing When the working set of all processes does not fit in the main memory. Hence, paging daemon has to constantly page-out pages to disk and bring them back in almost immediately. Solution : Reduce the degree of multi-programming swap some less important processes to disk And free up all the pages they hold number of processes competing for memory

Shared Memory between multiple processes Virtual Address space 1 Virtual Address space 2 PTE1 PTE2 Physical memory PTE = Page Table Entry PTEs for virtual pages in different processes map to common physical page frames in DRAM.

Copy-on-write Right after fork() Child process has identical memory content as parent. But copying is inefficient, especially if the child calls exec Copy-on-write Delays copying memory till either child or parent actually write to a page. Pages are shared as read-only (RO) between the parent and child Writes cause hardware to trap to OS OS intercepts write faults, copies page, and marks them read/write (RW) P R M Readonly protection bits page frame number

Memory-mapped Files Normally, files are accessed with system calls Open, read, write, close Memory mapping (using mmap) allows a program to access a file with load/store operations Virtual Address space Foo.txt