1 First-in, First-out Remove the oldest page Determine the age based on the loading time, not on the time being referenced Old pages may be heavily used.

Slides:



Advertisements
Similar presentations
Chapter 4 Memory Management Basic memory management Swapping
Advertisements

9.4 Page Replacement What if there is no free frame?
Page Replacement Algorithms
4.4 Page replacement algorithms
Page Replacement Algorithms
Virtual Memory (Chapter 4.3)
Chapter 3.3 : OS Policies for Virtual Memory
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Virtual Memory 3 Fred Kuhns
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Page Replacement Algorithms
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Chapter 9: Virtual Memory
Virtual Memory Management G. Anuradha Ref:- Galvin.
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
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.
Virtual Memory Today Virtual memory Page replacement algorithms
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management – 4 Page Replacement Algorithms CS 342 – Operating Systems.
Introduction to Systems Programming Lecture 7
Memory Management Virtual Memory Page replacement algorithms
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement Jonathan Walpole Computer Science Portland State University.
Virtual Memory Management B.Ramamurthy. Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
OS Spring’04 Virtual Memory: Page Replacement Operating Systems Spring 2004.
Page Replacement Algorithms
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Demand Paging Virtual memory = indirect addressing + demand paging –Without demand paging, indirect addressing by itself is valuable because it reduces.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
CS 333 Introduction to Operating Systems Class 14 – Page Replacement Jonathan Walpole Computer Science Portland State University.
CSC 322 Operating Systems Concepts Lecture - 15: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Virtual Memory.
O RERATıNG S YSTEM LESSON 10 MEMORY MANAGEMENT II 1.
Memory Management Page replacement algorithms, segmentation Tanenbaum, ch. 3 p Silberschatz, ch. 8, 9 p
Page Replacement Algorithms Memory Management. Optimal Page Replacement ▪The label for each page in memory is labeled with the number of instructions.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Paging.
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.
CSC 360, Instructor: Kui Wu Memory Management II: Virtual Memory.
Demand Paging Reference Reference on UNIX memory management
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.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
Page Replacement FIFO, LIFO, LRU, NUR, Second chance
COS 318: Operating Systems Virtual Memory Paging.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Virtual Memory What if program is bigger than available memory?
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
Chapter 9: Virtual Memory – Part I
Demand Paging Reference Reference on UNIX memory management
Operating Systems Virtual Memory Alok Kumar Jagadev.
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Demand Paging Reference Reference on UNIX memory management
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Practical Session 8, Memory Management 2
Page Replacement FIFO, LIFO, LRU, NUR, Second chance
Operating Systems CMPSC 473
Module 9: Virtual Memory
Practical Session 9, Memory Management continues
Virtual Memory.
Presentation transcript:

1 First-in, First-out Remove the oldest page Determine the age based on the loading time, not on the time being referenced Old pages may be heavily used Process table, scheduler Pure FIFO is rarely used Improve: second chance algorithm

2 Second Chance Algorithm Idea: Looking for an old page not recently referenced Algorithm: Inspect the R bit before removing old pages 0: throw it away 1: clear R bit, move the page to the tail A … 018 Page loaded first Most recently loaded page A … 1820 A is treated like a newly loaded page

3 Clock Page Algorithm Moving pages around in second chance algorithm is inefficient Keep all page frames on a circular list A hand points to the oldest one A variation of second chance algorithm A B C D E F H G

4 Least Recently Used (LRU) Observation: Pages heavily used in the last few instructions will probably be heavily used again in the next few instructions. Remove the page unused for the longest time Maintaining a list of pages? Most recently used page at the front Least recently used page at the rear Update the list on every memory reference Very costly!

5 LRU: Hardware Solution A global hardware counter automatically incremented after each instruction A 64-bit local counter per entry in page table Local counter  global counter when page is referenced When a page fault occurs, examine all the local counters. The page with lowest value is the least recently used one

6 Software LRU NFU – Not Frequently Used Each page has a software counter initially zero Add R bit to the counter every clock interrupt, then clear the R bit The page with least counter value is removed Never forget anything, hurt recently used pages A page was heavily used before but no longer be referenced. This page cannot be removed due to the high value counter. Improvement: Aging Counters are shifted right 1 bit before the R bit is added in The R bit is added to the leftmost

7 Example The page with lowest counter value is removed PageR bitCounter Before clock tick 1 PageR bitCounter After clock tick

8 Aging Vs. LRU In aging, no information about which page was referenced last (first) during the interval between clock ticks. Clock interval is 10 seconds. One page is referenced at the 3 rd second; while another at the 7 th second. Aging does not know which page is referenced first. Limited history kept by aging 8-bit counter only keeps the history of 8 clock ticks. Referenced 9 ticks ago or 1000 ticks ago: no difference

9 Working Set & Thrashing Working set: the set of pages currently used by a process The entire working set in main memory  not many page faults until process moves to next phase Only part of the working set in main memory  many page faults, slow Like page table, each process has its own unique working set Trashing: a program causing page faults every few instructions

10 Working Set Model: Avoid Trashing Demand paging Pages are loaded on demand Lots of page faults when process starts Working set model: bring it in memory before running a process Keep track of working sets Pre-paging: before run a process, allocate its working set

11 The Function of Working Set Working set (WS): at time t, the set of pages used by the k most recent memory references The set changes slowly in time, i.e., the content of working set is not sensitive to k E.g., a loop referring 10 pages has 1,000 instructions for each iteration Working set has the same 10 pages from k=1,000 to k=1,000,000 if the loop executes 1000 times. k w(k, t)

12 Maintaining WS: A Simple Way Store page numbers in a shift register of length k, and with every memory reference, we do Shift the register left one position, and Insert the most recently referenced page number on the right The set of k page numbers in the register is the working set. p1p2…pk the eldest page p2p3…p(k+1) Page (k+1) is referenced The most recent page

13 Approximating Working Set Maintaining the shift register is expensive Alternative: using the execution time (  ) instead of references E.g., the set of pages used during the last  =100 msec of execution time. Usually  > the interval of clock interrupt

14 WS Page Replacement Algorithm Basic idea: find a page not in WS and evict it The pages in WS are most likely to be used next On every page fault, the page table is scanned to look for a suitable page to evict Each entry contains (at least) two items: the time the page was last used and R (referenced) bit. The hardware sets the R each time a page is used. Software clears the R bit periodically.

Algorithm Details

16 WSClock Algorithm The basic working set page replacement scans the entire page table per page fault, costly! Improvement: clock algorithm + working set information A circular list of pages. Initially empty. When a page is loaded, it is added to the list. Pages form a ring. Widely used Based on: time of last use, R and M bits

17 Algorithm Details A page not in working set Clean (no modification)  use it Dirty  schedule a write back to disk and keep searching It is OS who is responsible for the write back! If the hand comes to its staring point again Some write-backs  find a clean page (it must be not in working set) No write-back  just grab a clean page or current page

Example New page Current virtual time Time of last use R bit(M bit not shown) M=1  =200 M=0

19 Summary AlgorithmComment OptimalNot implementable, good as benchmark NRUVery crude FIFOMight throw out important pages Second chanceBig improvement over FIFO ClockRealistic LRUExcellent, but difficult to implement exactly NFUFairly crude approximation to LRU AgingEfficient algorithm approximates LRU well Working setSomewhat expensive to implement WSClockGood efficient algorithm

20 Maintaining WS: A Simple Way Store page numbers in a shift register of length k, and with every memory reference, we do Shift the register left one position, and Insert the most recently referenced page number on the right The set of k page numbers in the register is the working set. p1p2…pk the eldest page p2p3…p(k+1) Page (k+1) is referenced The most recent page

21 Approximating Working Set Maintaining the shift register is expensive Alternative: using the execution time (  ) instead of references E.g., the set of pages used during the last  =100 msec of execution time. Usually  > the interval of clock interrupt

22 WS Page Replacement Algorithm Basic idea: find a page not in WS and evict it The pages in WS are most likely to be used next Pre-requisites Each entry contains (at least) two items: the time the page was last used and R (referenced) bit. The hardware sets the R each time a page is used. Software clears the R bit after every clock interrupt.

23 Algorithm Details

24 WSClock Algorithm The basic working set page replacement scans the entire page table per page fault, costly! Improvement: clock algorithm + working set information A circular list of pages. Initially empty. When a page is loaded, it is added to the list. Pages form a ring. Widely used Based on: time of last use, R and M bits

25 Algorithm Details When page fault occurs, check the page pointed by the clock-hand If it is clean and not in working set, Get it! If it is dirty, schedule a write back to disk and move the hand one page ahead It is OS who is responsible for the write back, and OS will clear the M bit once that page is written back. If the hand comes to its staring point again Some write-backs  find a clean page (it must be not in working set) No write-back  just grab a clean page or current page

26 Example New page Current virtual time Time of last use R bit(M bit not shown) M=1  =200 M=0

27 Summary AlgorithmComment OptimalNot implementable, good as benchmark NRUVery crude FIFOMight throw out important pages Second chanceBig improvement over FIFO ClockRealistic LRUExcellent, but difficult to implement exactly NFUFairly crude approximation to LRU AgingEfficient algorithm approximates LRU well Working setSomewhat expensive to implement WSClockGood efficient algorithm

28 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

29 Motivation For the preceding algorithms like LRU, WSClock Based on the empirical studies E.g., more page frames are better. No theoretical analysis Given the number of page frames, how many page faults are expected? To reduce the page fault occurrence to certain level, how many page frames are required?

30 More Page Frames  Fewer Page Faults? Belady’s anomaly  Not always the case. 5 virtual pages and 3/4 page frames Page reference string: FIFO is used Youngest Oldest PPPPPPPPP9 page faults Youngest Oldest PPPPPPPPPP10 page faults

31 Model of Paging System Three elements for the system Page reference string Page replacement algorithm #page frames (size of main memory): m M: the array keeping track of the state of memory. |M|=n, the number of virtual pages |top part|=m, recording the pages in main memory Empty entry  free page frame |bottom part|=n-m, storing the pages have been referenced but have been paged out.

32 How M Works? Initially empty When a page is referenced, If it is not in memory (i.e., not in the top part of M), page fault occurs. If empty entry in the top part exists, load it, or A page is moved from the top part to the bottom part. Pages in both parts may be separately re- arranged.

33 An Example With LRU Algorithm When referenced, move to the top entry in M. All pages above the just referenced page move down one position Keep the most recently referenced page at top Reference string Page faultsPPPPPPPPPPP Top part Bottom part

34 Stack Algorithms The algorithms that have the property: M(m, r)  M(m+1, r), where m is the number of page frames and r is an index into the reference string. M(m,r) is a set of pages in the top part of M for index r. m is the size of top part. E.g., M(4,1) If increase main memory size by one page frame and re-execute the process, at every point during the execution, all the pages that were present in memory in the first run are also present in the second run Stack algorithms do not suffer from Belady’s anomaly LRU is but FIFO is not.

35 An Observation Reference string P Youngest Oldest PPPPPPPPP9 page faults Youngest Oldest PPPPPPPPPP10 page faults LRU FIFO M(4,r)  M(5,r)

36 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

37 Allocating Memory for Processes Age A010 A17 A25 A34 A46 A53 B09 B14 B26 B32 B45 B56 B612 C13 C25 C36 Local lowest Global lowest A0 A1 A2 A3 A6 A5 B0 B1 B2 B3 B4 B5 B6 C1 C2 C3 Local page replacement Global page replacement A0 A1 A2 A3 A6 A5 B0 B1 B2 A6 B4 B5 B6 C1 C2 C3 Process A has a page fault…

38 Local/Global Page Replacement Local page replacement: every process with a fixed fraction of memory Thrashing even if plenty of free page frames are available. Waste memory if working set shrinks Global page replacement: dynamically allocate page frames for each process Generally works better. But how to determine how many page frames to assign to each process?

39 Assign Page Frames to Processes Allocate each process an equal share 1000 frames and 10 processes. Each process gets 100 frames. Assign 100 frames to a 10-page process! Dynamically adjust #frames Based on program size Allocate based on page fault frequency (PFF) Page faults/sec #frames Assigned A B

40 Page Size Arguments for small page size Internal fragmentation: on average, half of the final page for a process is empty and wasted p: page size, thus p/2 memory wasted for each segment The size of process could be smaller than one page Arguments for large page size Small pages  large page table and overhead s: size of process, e: size of page entry. The over head of page table entries is: s*e/p Total overhead = s*e / p + p / 2 Solution: p = sqrt(2 * s* e)

41 Shared Pages for Programs Processes sharing the same program should share the pages Avoid two copies of the same page Keep the share pages when a process swaps out or terminates. Identify the share pages Costly if search all the page tables.

42 Cleaning Dirty Pages Writing back dirty pages when free pages are needed is costly! Processes are blocked and have to wait Overhead of process switching Keep enough clean pages for paging Background paging daemon: evicting pages Keep page content, can be reused