Download presentation
Presentation is loading. Please wait.
1
Page Replacement FIFO, LIFO, LRU, NUR, Second chance
Dynamically adding/subtracting frames Belady’s anomaly
2
Page Replacement Suppose the OS allocates a fixed number of page frames to a process. If a page from a process is referenced by the process, and it is not currently in memory (this is called a page fault) then a page frame from that process must be selected for removal so the newly referenced page can be put in memory. Which page frame should be selected? Local Replacement When a process needs to bring in a page to memory, the page selected for removal is chosen from the set of pages currently in memory for that same process. Global Replacement When a process needs to bring in a page to memory, the page selected for removal is chosen from the set of all pages currently in memory for any process.
3
Process A has 3 page frames
Process A has pages 0,1,and 4 in main memory in real page frames: 98,96 and 100 respectively
4
OS allocating page frames
The operating system will allocate page frames to a process. It may not be enough frames to load the entire program. When the level of multiprocessing is very high, the number of frames could be low. The number of page frames a process may have can vary dynamically while the program is running.
5
Selecting a page to remove
So if The Operating System currently is giving a process 3 page frames to load pages into (because so many other programs are currently running and space is limited) And currently all 3 pages fames are occupied with process pages The process is now going to execute an instruction from a page not in memory We need to go out to the disk drive and bring the pages in and therefore select a page to remove (write on top of). Note: if we select a page that has been modified, we must also first, write those modifications back to disk before replacing the page. This will take extra time.
6
Ex) Page 3 needs to come in
Where should be page 3 be written to? Why? Discuss: FIFO, LRU, Optimal The Goal is to predict the future from the past but also do not take up a lot of time doing it.
7
First or Last in First Out
FIFO (First In First Out) Method: Select the page that has been in MM the longest and remove it to make room. Implementation: Maintain a linked list data structure of all pages currently in MM and remove the page that is pointed to by the head of the linked list. LIFO (Last In First Out) Method: Select the page that has been in MM the shortest and remove it to make room. Implementation: Maintain a stack of all pages currently in MM and remove the page that is at the top of the stack the linked list.
8
Statistics based (Least Recently Used)
LRU (Least Recently Used) Method: Select the page that has been referenced the least recently of all frames in Main Memory. Implementation 1: Maintain a linked list of all pages currently in MM and each time a page is referenced (read or write), move the page to the tail of the linked list. Implementation 2: Maintain timestamps of all pages currently in MM and each time a page is referenced (read or write), update the timestamp. When replacing a page, pick the page with the oldest timestamp.
9
Not Used Recently NUR (Not Used Recently)
Method: Select the frame that has been referenced the somewhat least recently of all frames in MM. This method is that same idea as LRU but the implementation is much simpler and therefore faster. Implementation: Maintain an array of bits for each page, when a page is referenced, set the bit to 1. At the time when all page reference bits are set to 1, then set all reference bits to zero to start the whole reference recording over. At the time of removal selection, pages that have not been referenced recently will have the reference bit set to 0. Pick on of them.
10
Second Chance Second Chance
Method: Same as NUR but with 2 bits. At the time when a page is selected for removal for the first time, it is allowed to stay in memory for a second chance. Implementation: Maintain 2 arrays of bits for each page, (just like the NUR) but when a page is selected, check if the second chance bit is set, if not, set it, and skip over this process an continue the selections process.
11
Optimal Page Replacement
Method: Look into the future at the pages that will be referenced (if possible), and of all the pages currently in memory, select the page that’s next reference will be the furthest in the future. Implementation: Predict the future. The OS must know the exact sequence of referenced that the process will make. Not feasible if the program has if…then…else statements. Possible if the process is a straight process without decisions. Comment: Although this is not realistic to implement, it is the benchmark to check other algorithms against. That is, if the optimal algorithm produces x page faults, you would compare other algorithms against x.
12
Example: page reference string
Consider a process in Main Memory where the OS has given the process 3 page frames of Main Memory to process out of but the process has a total of four pages numbered 0,1,2 and 3. When a process is running, the list of pages referenced in order is called the reference string for that process. Suppose the reference for this process is 0,1,2,3,2,3,1,0,2,3 and the following page replacements are used.
13
Example using FIFO Miss ratio = 5/10 Hit ratio = 1 – Miss Ratio = 5/10
14
Example using LRU Miss ratio = 7/10 hit ratio = 1 – Miss ratio = 3/10
15
Example using Optimal Best miss ratio for this reference string is 5/10
16
Conclusions? FIFO is better than LRU in general based on the last example reference string? No, just because it is better here doesn’t mean it is usually better. Does one algorithm take up more processor time (therefore make processes take longer to complete) than another algorithm? Yes, but maybe it would be worth it to produce fewer page faults. Can any algorithm have less page faults than the Optimal algorithm? No, That’s why it is called the optimal algorithm. The best any algorithm can do is tie the optimal algorithm. It is often impossible to know future page references of a running program but once a program has been running, it is easy to calculate how well the Optimal Algorithm would have been and use that as a bench mark to compare the algorithm the OS is actually using.
17
Dynamically adding/subtracting frames
Operating System has 12 available frames to give to 4 processes. How many frames should each process get? Guess 12/4 = 3…why not? Should the OS make adjustments to frame/process?
18
Working Set (see queuing theory)
The working set principle take into account that whichever page replacement algorithm used, to maximize overall throughput, the goal is to have the processor(s) running as close to 100% of the time. A state where all processes are waiting for the disk drives for a page and none are “ready to run” should be avoided.
19
Belady’s Anomaly Compare FIFO with 3 frames vs. 4 frames
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.