Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementation of page-replacement algorithms and Belady’s anomaly

Similar presentations


Presentation on theme: "Implementation of page-replacement algorithms and Belady’s anomaly"— Presentation transcript:

1 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 Belady/000

2 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

3 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 (16) FFFFFFFFFF(16) Logical Memory Address Space 64MB Page Main Memory Belady/001

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12    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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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 { /* memory block request fails */ return (1); 12 } 13 14 fp = fopen (fileName, "wb" ); 15 if (fp == NULL ) 16 { /* file open fails */ 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

24 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

25 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

26 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

27 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


Download ppt "Implementation of page-replacement algorithms and Belady’s anomaly"

Similar presentations


Ads by Google