Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 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 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 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 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 7 Example The page with lowest counter value is removed PageR bitCounter 0011110000 1101100000 2100100000 3001000000 4010110000 5001010001 Before clock tick 1 PageR bitCounter 0001111000 1010110000 2010010000 3000100000 4001011000 5000101000 After clock tick 1 01111000 10110000 00101000

8 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 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 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 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 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 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 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.

15 Algorithm Details

16 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 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

18 Example 20141 12130 19800 20140 12130 19800 20140 12130 19800 20141 12130 22041 New page 1 2 34 2204 Current virtual time Time of last use R bit(M bit not shown) M=1  =200 M=0

19 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 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 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 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 23 Algorithm Details

24 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 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 26 Example 20141 12130 19800 20140 12130 19800 20140 12130 19800 20141 12130 22041 New page 1 2 34 2204 Current virtual time Time of last use R bit(M bit not shown) M=1  =200 M=0

27 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 28 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

29 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 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: 0 1 2 3 0 1 4 0 1 2 3 4 FIFO is used 012301401234 Youngest012301444233 01230111422 Oldest0123000144 PPPPPPPPP9 page faults 012301401234 Youngest012333401234 01222340123 0111234012 Oldest000123401 PPPPPPPPPP10 page faults

31 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 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 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 021354637473355311171341 021354637473355311171341 02135463747733533317134 0213546334477755533713 021354666644477755577 02115555566644444455 0221111111166666666 002222222222222222 0000000000000000 Page faultsPPPPPPPPPPP Top part Bottom part

34 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 35 An Observation Reference string021354637473355311171341 021354637473355311171341 02135463747733533317134 0213546334477755533713 021354666644477755577 02115555566644444455 0221111111166666666 002222222222222222 0000000000000000 P 012301401234 Youngest012301444233 01230111422 Oldest0123000144 PPPPPPPPP9 page faults 012301401234 Youngest012333401234 01222340123 0111234012 Oldest000123401 PPPPPPPPPP10 page faults LRU FIFO M(4,r)  M(5,r)

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

37 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 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 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 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 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 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


Download ppt "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."

Similar presentations


Ads by Google