Implementation of page-replacement algorithms and Belady’s anomaly

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
Chapter 101 The LRU Policy Replaces the page that has not been referenced for the longest time in the past By the principle of locality, this would be.
Page-replacement policies On some standard algorithms for the management of resident virtual memory pages.
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 4 Memory Management Page Replacement 补充:什么叫页面抖动?
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
Virtual Memory Management G. Anuradha Ref:- Galvin.
Caching and Virtual Memory. Main Points Cache concept – Hardware vs. software caches When caches work and when they don’t – Spatial/temporal locality.
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 Introduction to Operating Systems: Module 9.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
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.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Maninder Kaur VIRTUAL MEMORY 24-Nov
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Virtual Memory.
Caching and Virtual Memory. Main Points Cache concept – Hardware vs. software caches When caches work and when they don’t – Spatial/temporal locality.
Memory Management Techniques
Page Replacement Algorithms Memory Management. Optimal Page Replacement ▪The label for each page in memory is labeled with the number of instructions.
Operating Systems CMPSC 473 Virtual Memory Management (3) November – Lecture 20 Instructor: Bhuvan Urgaonkar.
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.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Virtual Memory The memory space of a process is normally divided into blocks that are either pages or segments. Virtual memory management takes.
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.
Memory Management & Virtual Memory © Dr. Aiman Hanna Department of Computer Science Concordia University Montreal, Canada.
CS307 Operating Systems Virtual Memory Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
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.
NETW3005 Virtual Memory. Reading For this lecture, you should have read Chapter 9 (Sections 1-7). NETW3005 (Operating Systems) Lecture 08 - Virtual Memory2.
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 ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
1 Contents Memory types & memory hierarchy Virtual memory (VM) Page replacement algorithms in case of VM.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
COS 318: Operating Systems Virtual Memory Paging.
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
Chapter 9: Virtual Memory – Part II
Chapter 2 Memory and process management
Chapter 9: Virtual Memory – Part I
Memory Management 6/20/ :27 PM
Demand Paging Reference Reference on UNIX memory management
CS 286: Memory Paging and Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Demand Paging Reference Reference on UNIX memory management
Chapter 9: Virtual-Memory Management
Memory Management & Virtual Memory
Lecture 39 Syed Mansoor Sarwar
5: Virtual Memory Background Demand Paging
Virtual Memory Management
Contents Memory types & memory hierarchy Virtual memory (VM)
Practical Session 8, Memory Management 2
Operating Systems CMPSC 473
Lecture 9: Caching and Demand-Paged Virtual Memory
Department of Computer Science
Module 9: Virtual Memory
COMP755 Advanced Operating Systems
Chapter 9: Virtual Memory CSS503 Systems Programming
Practical Session 9, Memory Management continues
CSE 542: Operating Systems
Module IV Memory Organization.
Presentation transcript:

Implementation of page-replacement algorithms and Belady’s anomaly CS 314 Operating Systems Implementation of page-replacement algorithms and Belady’s anomaly Department of Computer Science Southern Illinois University Edwardsville Fall, 2018 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu Belady/000

CS 314 Operating Systems Today, we will discuss (as your check list):  (Memory) page-replacement algorithms (for virtual memory (CS286))  Advantages (and disadvantages) in the major page-replacement algorithms  The paradox regarding the page-replacement algorithms (“more memory” can end up with a slower computer )  A type of programming errors you can make: “memory leaks”  A technique to cope with “memory leaks” – “garbage collection” (it can be your hero, at the same time a trouble maker to you)  Locality in memory accesses – something you should know for designing your software Belady/001

CS 314 Operating Systems Memory Management: Page-Replacement Algorithms Virtual memory (from CS-286) Disk Virtual Memory Physical Memory Address Space Memory Address Space CPU 00000000(16) FFFFFFFFFF(16) Logical Memory Address Space 64MB Page Main Memory Belady/001

CS 314 Operating Systems Memory Management: Page-Replacement Algorithms INITIAL-PF: 5 HITS: 3 FIFO Algorithm CAPACITY-PF: 4 HIT-RATE: 3/12 Accessed Memory Pages 1 2 3 4 1 3 2 3 3 4 4 1 4 1 4 1 4 2 4 2 3 1 2 1 1 2 2 1 3 Time INITIAL-PF: CAPACITY-PF: HITS: HIT-RATE: 5 2 LRU Algorithm 5 2/12 Accessed Memory Pages 1 2 3 1 4 1 2 3 4 1 3 2 3 3 4 4 4 2 2 1 3 2 1 2 1 1 1 3 2 1 1 1 4 Belady/002

CS 314 Operating Systems Locality in Memory Accesses  Spatial locality If a particular memory address is referenced at a particular time, then it is likely that nearby memory addresses will be referenced in the near future.  Temporal locality If at one point a particular memory address is referenced, then it is likely that the same address will be referenced again in the near future. Belady/003

CS 314 Operating Systems Locality in Memory Accesses  Spatial locality If a particular memory address is referenced at a particular time, then it is likely that nearby memory addresses will be referenced in the near future.  Temporal locality If at one point a particular memory address is referenced, then it is likely that the same address will be referenced again in the near future. Belady/004

CS 314 Operating Systems Locality in Memory Accesses Memory Working Set The segments of the memory space a process currently needs to execute Itself. = the size of the memory a process needs for executing itself w/out causing serious page faults Belady/005

CS 314 Operating Systems The trade-off problem between FIFO and LRU Problem in FIFO Does not consider how recently each page was used (= ignores “locality in memory reference”) Problem in LRU Implementation is expensive (= considers locality in memory reference, but too much to do) Belady/006

CS 314 Operating Systems Implementation of (LRU) Algorithm To implement LRU, what kind of information is needed?  Information about reference order N pages Physical Memory         Which page was most recently referenced? Which page was second most recently referenced? Which page was third most recently referenced? We have to keep track of the order of all N pages  Amount of information needed Given N pages log2N bits required for each entry (a total of N log2N bits)  Amount of operations needed (algorithm complexity) log2N bits Sorting N pages in their access order (N) Worst case: for each memory access Belady/007

CS 314 Operating Systems Not Recently Used (NRU) Algorithm Problem in FIFO Problem in LRU Does not consider how recently each page was used (= ignores “locality in memory reference”) Implementation is expensive (= considers locality in memory reference, but too much to do) NRU page-replacement algorithm  Consider how recently each page was used (advantage in LRU)  Reasonable implementation cost (advantage in FIFO) Belady/008

CS 314 Operating Systems Not Recently Used (NRU) Algorithm Each physical page is attached with 2 bits (“R bit” and “M bit”) (instead of log2N bits for each physical page) When a page is loaded with a new page - Set both R and M bits “0” When a page is read after the page was initially loaded (assuming that the first use of this page was “read”) - Set R bit “1” Any time a page is updated (written) - Set M bit “1” Belady/009

   CS 314 Operating Systems Not Recently Used (NRU) Algorithm When a new page is requested, while all pages are currently used, select a page to replaced in the following order: R Bit M Bit 1 (R = 0, M = 0) (R = 1, M = 0) (R = 0, M = 1) (R = 1, M = 1)    If there are multiple pages in a group, select one using other selection methods Belady/010

CS 314 Operating Systems Second-Chance Algorithm Pages are enqueued and dequed only if they are first referenced and removed from the memory Same as FIFO, with one exception We discussed that FIFO can be implemented by a queue     FIFO Queue Next page # to be replaced A page# newly referenced     Check the top page’s “used” attributed 1-bit “used” attribute if 0, replace this page if 1, set “used” attribute “0” and move it to the end of the queue “Used” attribute Set 0 when a page is loaded to a physical-memory page Set 1 when a page is used after loaded to a physical-memory page Belady/011

the performance of them CS 314 Operating Systems We want to quantify the performance of them Belady’s Anomaly Performance metric for page-replacement algorithms (Total # of page faults) “Page fault-rate” = (Total # of memory references ) For some page replacement algorithms, the page-fault rate can increase after you install more physical memory - More physical memory More physical memory blocks - Page size remains the same Questions How is it possible? How can we prevent that from happening? Belady/012

CS 314 Operating Systems          Belady’s Anomaly PF Rate = Page-reference String: 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4 Physical memory with 3 pages FIFO page-replacement algorithm 1 2 3 4 1 3 2 3 3 4 4 1 4 1 4 1 2 4 3 2 4 1 1 2 1 2 2 1 3          All 3 pages initially available PF Rate = # of Page-Faults # of total page references = 9 = 0.75 12 Belady/013

CS 314 Operating Systems           Belady’s Anomaly Page-reference String: 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4 Physical memory with 3 pages 4 pages (+1 page) FIFO page-replacement algorithm 1 2 3 1 4 1 2 3 4  1  2 1  3 1 2  2 3 1 2 3 1 4 4 2 3 3 4 4 1 3 3 2 1 1 3 2 2 1 4 1 2       PF Rate = # of Page-Faults # of total page references 10 = = 0.83 12 Belady/014

CS 314 Operating Systems           Belady’s Anomaly Page-reference String: 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4 Physical memory with 3 pages LRU page-replacement algorithm 1 2 3 4 1  2 1  3 2 3 3 4 4 1 4 1 2 2 1 3 2 1 2 1 1 3 1 4         PF Rate = # of Page-Faults # of total page references 10 = 12 = 0.83 Belady/015

CS 314 Operating Systems      Belady’s Anomaly PF Rate = = Page-reference String: 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4 Physical memory with 3 pages 4 pages (+1 page) LRU page-replacement algorithm 1 2 3 1 4 1 2 3 4 1 2 3  2 3 1 2 3 1 1  3 4 3 1 3 4 1 4 1  2  1 4 3 2 1  4 3 2 PF Rate = # of Page-Faults # of total page references = 12 8 = 0.67 Belady/016

CS 314 Operating Systems                  Belady’s Anomaly FIFO Page-Replacement Algorithm 1 2 3 4 MRU LRU  1  2 1  3 1 2  2 3  1 3  4 1  4 1 4 1 2 4 1  3 2 4  3 2 4 1 2 3 4 MRU LRU  1 2 3  2 3 1 2 3 1 4 1 2 3 4 1  3 2 4 2  3 3 4  4 1  2  1 3 2 1  Belady/017

CS 314 Operating Systems               Belady’s Anomaly LRU Page-Replacement Algorithm 1 2 3 1 4 1 2 3 4 1 2 MRU LRU 3 1 2  2 3  1 3  4 1  4 1 4 1 2 1  3 2 1  3 2 4   1 2 3 1 4 1 2 3 4  MRU LRU 1 2 3  2 3 1 2 3 1 4 1  3 4 1 3 3 4 1 2 4 1  3 2  1 3 2 4 1  Any page-replacement algorithm that satisfies this property “stack algorithm” Belady/018

CS 314 Operating Systems Belady’s Anomaly For some page-replacing algorithms, increasing the physical memory capacity does not necessarily mean page-fault rate will be improved. The phenomenon is known as “Belady’s Anomaly” Those page-replacement algorithms that suffer from “Belady’s Anomaly” are “Non-Stack Algorithms” The stack page-replacement algorithms satisfy the following property - Assume M(k, r) indicates a set of pages in the physical memory that consists of k blocks at r-th reference - Then, M(k, r)  M((k+1), r) Any page in k physical-memory pages is always a part of (k+1) physical-memory pages Belady/019

the operating system can successfully delete CS 314 Operating Systems Memory Leaks the operating system can successfully delete “leaked memory space” /* the MAIN ----------------------- */ void main(void) { int * local_pointer_01; struct Struct_Definition { int my_integer_01; int my_integer_02; char my_text[256]; }; struct * Struct_Definition my_struct_01; ..... local_pointer_01 = new (int); my_struct_01 = new (struct Struct_Definition); return_code = my_function (void); delete local_pointer_01; delete my_struct_01; } int my_function(void) { int * test_integer; ..... test_integer = new (int); delete test_integer; return(1); } If you forgot “delete”, the memory space allocated (“new”) for any pointer in this program will be released by OS when this module (“my_function” is completed. If you forgot “delete”, the memory space allocated (“new”) for any pointer in this program will be released by OS Belady/018

CS 314 Operating Systems Memory Leaks Memory spaces left unused by an operating system You’ve created a memory block 11 pData = malloc( size ); 100 /* request a dynamic memory block */ 101 pData = malloc(size1); 102     300 pData = malloc(size2); 301 900 free (pData); 1 int write_to_a_file (char *fileName, int size) 2 { 3 FILE *fp; 4 char *pData; 5 6 /* request a dynamic memory block */ 7 pData = malloc( size ); 8 if (pData == NULL ) 9 { 10 /* memory block request fails */ 11 return (1); 12 } 13 14 fp = fopen (fileName, "wb" ); 15 if (fp == NULL ) 16 { 17 /* file open fails */ 18 return (1); 19 } 20 /* output (0x20) to the open file */ 21 memset (pData, 0x20, size ); 22 fwrite (pData, size, 1, fp ); 23 fclose (fp); 24 25 /* release the memory block */ free (pData); return (0); 28 } You’ve created another memory block is allocated this process this memory space This “free” will NOT release the first memory block The operating system can not re-allocate this memory space to any other processes even after this process terminates!! this allocated memory space will not be released until this process terminates The operating system can not re-allocate this memory space to any other processes until this process terminates Belady/019

CS 314 Operating Systems Memory Garbage Collection When the available memory space becomes low, an operating system looks for unused memory space (e.g., leaked memory space) in a computer system - Finding the memory space being used by an existing process is easy. - Finding the memory space not being used by any existing process is hard. (or at least time-consuming) Running processes temporarily stop their execution. Programming Languages w/ garbage collector: Programming Languages w/out garbage collector: Java, C#, PHP, Python C, C++, COBOL, Fortlan … Belady/020

2nd-Chance FIFO Algorithm Accessed Memory Pages 1 2 3 4 1 2 3 1 4 4 4 1 3 1 2 3 1 1 1 1 4 1 1 2 3 1 1 2 1 3

CS 314 Operating Systems                    Belady’s Anomaly FIFO Page-Replacement Algorithm 1 2 3 1 4 1 2 3 4 MRU LRU 1 2 3 1 4 1 2 3 4 1 2 3 1 4 1 2 3 1 2 3 1 4 4 4 2                 3 3   3 1 2 3 1 4 1 2 3 4 MRU LRU 1 2 3 1 4 1 2 3 4 1 2 3 1 4 1 2 3 1 2 3 3 3 4 1 2 1 2 2 2 3 4 1               4 4       Distance String = the # of slots from the top of the queue

CS 314 Operating Systems                   Belady’s Anomaly LRU Page-Replacement Algorithm 1 2 3 1 4 1 2 3 4 MRU LRU 1 2 3 1 4 1 2 3 4 1 2 3 1 4 1 2 3 1 2 3 1 4 1 2                  3 3    1 2 3 1 4 1 2 3 4 MRU LRU 1 2 3 1 4 1 2 3 4 1 2 3 1 4 1 2 3 1 2 3 1 4 1 2 1 2 3 3 3 4 1             4 4  3 3   