2Outline This topic will cover address translation: TBW Static Paging AlgorithmsOutlineThis topic will cover address translation:TBW
3Definitions Recall that Static Paging AlgorithmsDefinitionsRecall thatThe virtual address space of a process is divided into equally sized pages (for example, 4 KiB)Main memory is divided into equally sized frames, each of which can hold one pageThe virtual address space may be located on a hard drive and pages are copied into frames only as the processor needs them
4Paging There are two classes of paging algorithms: Static Paging AlgorithmsPagingThere are two classes of paging algorithms:Static paging algorithm allocate each process a fixed number of frames and this number does not change over the lifetime of the processDynamic paging algorithms allocate a variable number of frames, as necessary, for each process
5Static Paging Algorithms To implement paging, there are many processes requesting memory locations and all these must be loaded into memory for processes to executeOperations we must be concerned with include:A fetch policy which decides when a page should be loaded into main memoryA replacement policy which decides which pages should be removedA placement policy which decides where new pages should be placed if there is room
6Static Paging Algorithms To model this, we require a means of describing how page references work:A page-reference stream is a sequence of page numbers in the order in which references appear to them in an executing programTo make this reasonable:We will consider accesses to the text, data, and stack segments separately, otherwise most page-reference streams will be rapid accesses between these segmentsWe will consider any continuous sequence of references to a single page to a single referenceOur interest is the order in which the pages are accessed—duplication in the list does not assist us here
7Static Paging Algorithms For example, within the text segment, a possible sequence during initialization may be0, 1, 0, 1, 2, 1, 2, 3, 1, 2, 3, 1, 3, 2, 3, 4, 3, 4, 5, 6, 7Recall that each page is 4 KiB—the time spent in each block may by perhaps milliseconds depending on any loopingIn others, it may be just a single instruction—but it must still be loaded
8Static Paging Algorithms When a program is executed, it is allocated memory within the virtual address space for text, data, and the stackInitially, none of these are in main memoryAt the very least, at least one page from both the text and data segments must be loaded into main memory
9Static Paging Algorithms Fetch PolicyThe fetch policy determines when a page should be copied from the virtual address space to main memoryPrefetching is the process where the memory manager attempts to determine which pages may be loaded into main memory at some point in the near futureExceptionally difficult, as this requires knowledge of the futureWith 32-bit instructions, that is 128 instructions per page, so there is no guarantee that loading one page will require the loading of the nextNormally, demand fetching is used: a page is loaded into a frame only if an instruction accesses a memory location from that page and it is not currently loaded
10Demand Paging Algorithms Static Paging AlgorithmsDemand Paging AlgorithmsGiven a page-reference stream, we require an algorithm that specifies which frame a given page should be copied toWe will consider a number of such algorithms:Random-replacement algorithmsBelady’s optimal algorithmLeast Recently Used (LRU) algorithmLeast Frequently Used (LFU) algorithmFirst-in—First-out (FIFO) algorithmStack algorithms
11Demand Paging Algorithms Static Paging AlgorithmsDemand Paging AlgorithmsWe will test these algorithms with the following page-reference stream: 0, 1, 0, 1, 2, 1, 2, 3, 1, 2, 4, 1, 3, 0, 2, 4, 3, 4, 5, 6, 7
12Random-replacement Algorithms Static Paging AlgorithmsRandom-replacement AlgorithmsA random replacement algorithmWhen a missing page fault occurs and there are no available frames, simply choose one frame at random and replace its contentsThis algorithm, while easy to implement, has been found to be reasonably sub-optimal in comparison to other algorithmsWe may use randomness if there is more than one page to choose from, however...
13Bélády’s Optimal Replacement Static Paging AlgorithmsBélády’s Optimal ReplacementThe ideal replacement, described by Bélády, is to choose that frame that will be referenced most distantly into the futureThis is, of course, impossible to achieve in almost all but the most trivial of systemsThis algorithm is guaranteed to minimize the number of missing page faults
14Bélády’s Optimal Replacement Static Paging AlgorithmsBélády’s Optimal ReplacementHere is Bélády’s optimal replacement algorithm in actionThe number of missing-page faults is10
15Static Paging Algorithms Least Recently UsedWe would like to somehow guess which page will be accessed most recently into the futureThe Least Recently Used (LRU) algorithm chooses that frame that has been accessed most distantly in the pastWe must stamp each frame every time the page it contains is accessedWe must keep an ordered list of framesThis essentially requires a modifiable heap data structure to be efficient...It uses the principle of spatial locality
16Least Recently Used Here is the LRU algorithm in action Static Paging AlgorithmsLeast Recently UsedHere is the LRU algorithm in actionThe number of missing-page faults is13
17Static Paging Algorithms Least Frequently UsedAnother algorithm is the Least Frequently Used (LFU) algorithmRemove that frame that has been least frequently accessedThis requires a modifiable heap together with a counter for each frameUnfortunately, this algorithm reacts very slowly to changes in locality, as we will see...
18Least Frequently Used Here is the LFU algorithm in action Static Paging AlgorithmsLeast Frequently UsedHere is the LFU algorithm in actionThe number of missing-page faults is13Note, however, that pages 1 and 2 are still loaded...
19First-in—First-out Another is the first-in—first-out (FIFO) algorithm Static Paging AlgorithmsFirst-in—First-outAnother is the first-in—first-out (FIFO) algorithmRemove that frame that was loaded most distantly in the pastThis would appear to require significantly more work, but it is quite easy: the frames are loaded in a round-robin fashionIt ignores the principle of locality, but
20First-in—First-out Here is the FIFO algorithm in action Static Paging AlgorithmsFirst-in—First-outHere is the FIFO algorithm in actionThe number of missing-page faults is13
21Comparison In this example: Static Paging AlgorithmsComparisonIn this example:Bélády’s algorithm issued 10 page missing faultsAll other algorithms required moreThat they all required 13 is irrelevant—examples this small cannot be used to suggest which is better...FIFO appears to be the easiest to implement—can we use it?
22Bélády’s Anomaly Here is an interesting question: Static Paging AlgorithmsBélády’s AnomalyHere is an interesting question:Suppose a process is given a fixed number of framesWill all of these algorithms always reduce the number of missing-page faults if the amount of frames allocated is increased?Oddly enough, the answer with respect to the FIFO algorithm is no!
23Bélády’s Anomaly From their original paper: Static Paging AlgorithmsBélády’s AnomalyFrom their original paper:0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4If the process has three frames assigned to it, there are nine missing-page faults
24Bélády’s Anomaly From their original paper: Static Paging AlgorithmsBélády’s AnomalyFrom their original paper:0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4If the process has four frames assigned to it, there are ten missing-page faults
25Static Paging Algorithms Inclusion PropertyConsequently, while FIFO is the easiest to implement, it is also susceptible to this anomaly—we can’t use itThe inclusion property may be held by a frame-replacement algorithm if the set of pages loaded when n frames are available is always a subset of the pages loaded when n + 1 frames are loadedThis clearly true with the LRU algorithm:At a particular instance, if n pages have been loaded, then those are the n pages that have been most recently usedIf there were n + 1 pages available, then the n most recently used pages would still be loaded
26Static Paging Algorithms Inclusion PropertyNutt  showed that any frame-replacement algorithm that satisfies the inclusion property is not susceptible to Bélády’s AnomalyThis includes LRU and LFULFU has issues, so we will try to implement LRU...
27Static Paging Algorithms Implementing LRUOne means of approximating FRU seems almost trivial but it works well in practice with sufficiently many pages of sufficient sizeAssociate each frame with a reference bitThese bits are periodically all set to 0When a page in a frame is referenced, set that bit to 1When a page-missing fault occurs, randomly replace a frame that has the referenced bit set to 0In the very unlikely case that all referenced bits are set, pick any frame at random
28Static Paging Algorithms Dirty BitsAnother issue with frame replacement is copying out the old contentsIf data within a frame has only been read but not written, when that frame is copied out, there is no need to write that page out to secondary memoryAssociate each frame with a dirty bitWhen a page is loaded into the frame, set the dirty bit to 0If the page in the frame is written to, set the dirty bit to 1When the page in the frame is being replaced, you only need to copy that frame out to secondary memory if the dirty bit was set to 1
29Summary This topic covered an introduction to virtual memory: TBW Static Paging AlgorithmsSummaryThis topic covered an introduction to virtual memory:TBW