Practical Session 9, Memory Management continues

Slides:



Advertisements
Similar presentations
CS Introduction to Operating Systems
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 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.
Chapter 3.3 : OS Policies for Virtual Memory
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
Scribe for 7 th April 2014 Page Replacement Algorithms Payal Priyadarshini 11CS30023.
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
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  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 38 Frame Allocation Read.
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.
1 Memory Management Managing memory hierarchies. 2 Memory Management Ideally programmers want memory that is –large –fast –non volatile –transparent Memory.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Virtual Memory Chapter 8.
Memory Management Virtual Memory Page replacement algorithms
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
CSI 400/500 Operating Systems Spring 2009 Lecture #9 – Paging and Segmentation in Virtual Memory Monday, March 2 nd and Wednesday, March 4 th, 2009.
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.
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
CSS430 Virtual Memory Textbook Ch9
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 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
Page Replacement Algorithms Memory Management. Optimal Page Replacement ▪The label for each page in memory is labeled with the number of instructions.
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.
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.
Virtual Memory The memory space of a process is normally divided into blocks that are either pages or segments. Virtual memory management takes.
Lecture Topics: 11/24 Sharing Pages Demand Paging (and alternative) Page Replacement –optimal algorithm –implementable algorithms.
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.
Operating Systems Practical Session 10, Memory Management continues.
1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.
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.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
PAGE REPLACEMNT ALGORITHMS FUNDAMENTAL OF ALGORITHMS.
COS 318: Operating Systems Virtual Memory Paging.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
Memory Management (2).
ITEC 202 Operating Systems
Day 22 Virtual Memory.
Demand Paging Reference Reference on UNIX memory management
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Demand Paging Reference Reference on UNIX memory management
Practical Session 8, Memory Management 2
Chapter 9: Virtual-Memory Management
Lecture 39 Syed Mansoor Sarwar
5: Virtual Memory Background Demand Paging
Demand Paged Virtual Memory
Practical Session 9, Memory Management continues
Practical Session 8, Memory Management 2
Operating Systems CMPSC 473
CSS430 Virtual Memory Textbook Ch10
Module 9: Virtual Memory
Chapter 9: Virtual Memory CSS503 Systems Programming
Practical Session 8, Memory Management 2
Presentation transcript:

Practical Session 9, Memory Management continues Operating Systems Practical Session 9, Memory Management continues

Page replacement algorithms Quick recap Page replacement algorithms

Optimal Assumes the memory manager knows the “future” sequence of page references The optimal algorithm: page out the page that will be used latest Problem: the manager doesn’t know the future sequence of requests!

FIFO/FIFO Second-chance First page In will be the First page taken Out Problem: we may be removing a page that will be constantly in use: Assume memory size of 2 frames, and take the following sequence of page requests: 1,2,3,1,2,3,1,2,3,1… FIFO second-chance: Add a reference bit that will be turned on whenever the page is accessed When a “swap out” is needed: go over the pages from the oldest to newest and if the page’s reference bit is on, clear it; otherwise remove the page. Both FIFO and FIFO second-chance can be implemented as a circular queue: the “clock algorithm”.

2nd chance FIFO (clock)

Least Recently Used (LRU) If we need to remove a page, then the Least Recently Used page will be chosen Throw out the page that has been unused for longest time period Problem: have to keep “history” and remember when a page was referenced Counter for each page, updated on every memory reference! LRU can be approximated: Shift counter Updating every page reference can be too often! => shift only every clock tick (modified version of NFU, also known as aging) Use n2 bit matrix Hardware LRU algorithm, where n is the number of page frames In xv6 it is simply a done with a list, however, here they are talking about a hardware solution

Modified NFU (Aging) Page references Clock tick 0 Clock tick 1 Clock tick 2 Clock tick 3 Clock tick 4 1 1 1 1 1 1 2 3 4 5 10000000 00000000 11000000 10000000 01000000 00000000 11100000 11000000 00100000 10000000 01100000 10100000 11110000 01100000 00010000 01000000 10110000 01010000 01111000 10110000 10001000 00100000 01011000 00101000 Pages On each clock tick: shift right all counters, set MSB to 1 for every page which have the referenced bit on. Clear the reference bit. Evacuate the page with the smallest counter.

Hardware LRU algorithm (bit tables) 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 When page-frame k is referenced, set all bits of row k to 1 and all bits of column k to 0. The row with lowest binary value is least recently used. Notice that when a page i is referenced all it’s bit are set to 1 (other than bit i). Now every other page that is references zeros the corresponding bit in row i, so every referenced page decreases the binary value of row i. When all other pages are references row i will have a value of 0. Reference string is: 0,1,2,3,2,1,0,3,2,3

Quick recap: global vs. local The scope of the page replacement policy can be: Local: choose a page to remove only among the pages of the process that caused the page fault Global: choose a page to remove from all pages in main memory, independent of the process Global policies are more efficient Dynamically allocate page frames among the runnable processes. This is useful when the size of a WS is dynamically changing. Local policies may have variable allocation of pages per process (“working set”) The advantage of local page replacement is its scalability: each process can handle its page faults independently without contending for some shared global data structure.

Local vs. global algorithms Adding page A6: Last reference time Local policy Global policy

Question 1 Program A: int i, j, a[100][100];   for (i = 0; i < 100; i++) {         for (j = 0; j < 100; j++) {                 a[i][j] = 0;         } } Program B: int i, j, a[100][100];   for (j = 0; j < 100; j++) {         for (i = 0; i < 100; i++) {                 a[i][j] = 0;         } } Assume that the array a is stored consecutively: a[0,0], a[0,1] ... and also assume that the size of each entry is one word. The virtual memory has a page size of 200 words. The program code is in address 0-199 in the virtual memory. a[0][0] is at virtual address 200. We run both programs on a machine with physical memory of 3 frames. Where the code of the program is in the 1'st frame and the other two are empty. If the page replacement algorithm is LRU, how many page faults will there be in each of the programs? Explain. Notice that the array is assumed to be stores consecutively, no as an array of pointers to arrays.

Question 1 Array a is stored in a[0][0],a[0][1] ... in virtual pages 1..50 The reference string (specifying only possible page faults) of program A will be: 0,1,0,2,0,3...50 We'll get a total of 50 page faults. The reference string of B will be: 0,1,0,2...,0,50,0,1,0,2....0,50,.. Leading to a total of 5000 page faults. Note that due to the use of the LRU algorithm, page 0 will be in memory at all times.

Question 2 Consider the following page reference string: 7,0,1,2,0,3, 0,4,2,3,0,3,2,1,2,0,1,7,0,1 Assuming that the memory size is 3 frames, how many page faults would occur for the following algorithms: FIFO LRU Optimal Note: Remember that all frames are initially empty, so your first unique pages will all cost one fault each.

Question 2: FIFO 15 page faults 7 1 2 3 4  

Question 2: LRU 12 page faults 7 1 2 3 4  

Question 2: Optimal 9 page faults 7 1 2 3 4  

Question 3 – 2001 a נתונה סדרת דרישות הדפים הבאה: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2 אם משתמשים ב-LRU, כתוב את ה-distance string עבור הסדרה הנתונה. חשב מתוך ה-distance string כמה page-faults יהיו עבור זיכרון פיזי בן 4 דפים. האם כדאי להגדיל את הזיכרון הפיזי ל-5 דפים במקרה זה? עבור אלג' FIFO וזיכרון פיסי בן 4 דפים, חשב מספר ה page faults. Stack algorithms: Satisfy M(m, r) ⊆ M(m + 1, r) where m varies over the page frames and r is an index into the reference string. What this says is that the set of pages included in the top part of M for a memory with m page frames after r memory references are also included in M for a memory with m + 1 page frames. In other words, if we increase memory size by one page frame and re-execute the process, at every point during the execution, all the pages that were present in the first run are also present in the second run, along with one additional page. With a little thought about how it works, it should be clear that LRU has this property. Some other algorithms (e.g. optimal page replacement) also have it, but FIFO does not. Algorithms that have this property are called stack algorithms. Distance string: For stack algorithms, it is often convenient to represent the reference string in a more abstract way than the actual page numbers. A page reference will be henceforth denoted by the distance from the top of the stack where the referenced page was located.

Question 3 – 2001 a 1. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Page fault p distance ∞

Question 3 – 2001 a בשביל לחשב את מספר ה-page-faults כשמשתמשים בזיכרון פיזי בן 5 דפים, נצטרך לסכום על כל המרחקים הגדולים מ-5: ישנם 8 כאלו. מנענו page-fault אחד.

Question 3 – 2001 a 2. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Page fault p

Question 4 Consider the following virtual page reference string: 0, 1, 2, 3, 0, 0, 1, 2, 3 Which page references will cause a page fault when a basic clock replacement algorithm is used? Assume that there are 3 page frames and that the memory is initially empty. Show all page faults (including frame loading).

Question 4 1 2 3 *0 *3 3 *2 2 *1 1 Page fault pf Where: 1 2 3 *0 *3 3 *2 2 *1 1 Page fault pf Where: * represents the placement of the clock’s “hand” before the request pf represents a page fault