§12.4 static paging algorithms douglas wilhelm harder, m.math. lel department of electrical and...
TRANSCRIPT
§12.4 Static Paging Algorithms
Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
© 2012 by Douglas Wilhelm Harder. Some rights reserved.Based in part on Gary Nutt, Operating Systems, 3rd ed.
Outline
This topic will cover address translation:– TBW
Static Paging Algorithms
Definitions
Recall that– The 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 page– The virtual address space may be located on a hard drive and
pages are copied into frames only as the processor needs them
Static Paging Algorithms
Paging
There 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 process
– Dynamic paging algorithms allocate a variable number of frames, as necessary, for each process
Static Paging Algorithms
Paging
To implement paging, there are many processes requesting memory locations and all these must be loaded into memory for processes to execute
Operations we must be concerned with include:– A fetch policy which decides when a page should be loaded into
main memory– A replacement policy which decides which pages should be
removed– A placement policy which decides where new pages should be
placed if there is room
Static Paging Algorithms
Paging
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 program
To 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 segments
– We will consider any continuous sequence of references to a single page to a single reference
• Our interest is the order in which the pages are accessed—duplication in the list does not assist us here
Static Paging Algorithms
Paging
For example, within the text segment, a possible sequence during initialization may be
0, 1, 0, 1, 2, 1, 2, 3, 1, 2, 3, 1, 3, 2, 3, 4, 3, 4, 5, 6, 7
– Recall that each page is 4 KiB—the time spent in each block may by perhaps milliseconds depending on any looping
– In others, it may be just a single instruction—but it must still be loaded
Static Paging Algorithms
Paging
When a program is executed, it is allocated memory within the virtual address space for text, data, and the stack
Initially, none of these are in main memory– At the very least, at least one page from both the text and data
segments must be loaded into main memory
Static Paging Algorithms
Fetch Policy
The fetch policy determines when a page should be copied from the virtual address space to main memory– Prefetching is the process where the memory manager attempts
to determine which pages may be loaded into main memory at some point in the near future
• Exceptionally difficult, as this requires knowledge of the future• With 32-bit instructions, that is 128 instructions per page, so there is
no guarantee that loading one page will require the loading of the next
– Normally, 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
Static Paging Algorithms
Demand Paging Algorithms
Given a page-reference stream, we require an algorithm that specifies which frame a given page should be copied to
We will consider a number of such algorithms:– Random-replacement algorithms– Belady’s optimal algorithm– Least Recently Used (LRU) algorithm– Least Frequently Used (LFU) algorithm– First-in—First-out (FIFO) algorithm– Stack algorithms
Static Paging Algorithms
Demand Paging Algorithms
We 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
Static Paging Algorithms
Random-replacement Algorithms
A random replacement algorithm – When a missing page fault occurs and there are no available
frames, simply choose one frame at random and replace its contents
– This algorithm, while easy to implement, has been found to be reasonably sub-optimal in comparison to other algorithms
• We may use randomness if there is more than one page to choose from, however...
Static Paging Algorithms
Bélády’s Optimal Replacement
The ideal replacement, described by Bélády, is to choose that frame that will be referenced most distantly into the future– This is, of course, impossible to achieve in almost all but the
most trivial of systems– This algorithm is guaranteed to minimize the number of missing
page faults
Static Paging Algorithms
Bélády’s Optimal Replacement
Here is Bélády’s optimal replacement algorithm in action– The number of missing-page faults is10
Static Paging Algorithms
Least Recently Used
We would like to somehow guess which page will be accessed most recently into the future
The Least Recently Used (LRU) algorithm chooses that frame that has been accessed most distantly in the past– We must stamp each frame every time the page it contains is
accessed– We must keep an ordered list of frames
• This essentially requires a modifiable heap data structure to be efficient...
– It uses the principle of spatial locality
Static Paging Algorithms
Least Recently Used
Here is the LRU algorithm in action– The number of missing-page faults is13
Static Paging Algorithms
Least Frequently Used
Another algorithm is the Least Frequently Used (LFU) algorithm– Remove that frame that has been least frequently accessed– This requires a modifiable heap together with a counter for each
frame– Unfortunately, this algorithm reacts very slowly to changes in
locality, as we will see...
Static Paging Algorithms
Least Frequently Used
Here is the LFU algorithm in action– The number of missing-page faults is13– Note, however, that pages 1 and 2 are still loaded...
Static Paging Algorithms
First-in—First-out
Another is the first-in—first-out (FIFO) algorithm– Remove that frame that was loaded most distantly in the past– This would appear to require significantly more work, but it is
quite easy: the frames are loaded in a round-robin fashion– It ignores the principle of locality, but
Static Paging Algorithms
First-in—First-out
Here is the FIFO algorithm in action– The number of missing-page faults is13
Static Paging Algorithms
Comparison
In this example:– Bélády’s algorithm issued 10 page missing faults– All other algorithms required more
• That 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?
Static Paging Algorithms
Bélády’s Anomaly
Here is an interesting question:– Suppose a process is given a fixed number of frames– Will 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!
Static Paging Algorithms
Bélády’s Anomaly
From their original paper:0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
– If the process has three frames assigned to it, there are nine missing-page faults
Static Paging Algorithms
Bélády’s Anomaly
From their original paper:0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4
– If the process has four frames assigned to it, there are ten missing-page faults
Static Paging Algorithms
Inclusion Property
Consequently, while FIFO is the easiest to implement, it is also susceptible to this anomaly—we can’t use it– The 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 loaded
– This 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 used• If there were n + 1 pages available, then the n most recently used
pages would still be loaded
Static Paging Algorithms
Inclusion Property
Nutt [1992] showed that any frame-replacement algorithm that satisfies the inclusion property is not susceptible to Bélády’s Anomaly– This includes LRU and LFU– LFU has issues, so we will try to implement LRU...
Static Paging Algorithms
Implementing LRU
One means of approximating FRU seems almost trivial but it works well in practice with sufficiently many pages of sufficient size– Associate each frame with a reference bit– These bits are periodically all set to 0– When a page in a frame is referenced, set that bit to 1– When a page-missing fault occurs, randomly replace a frame
that has the referenced bit set to 0• In the very unlikely case that all referenced bits are set, pick any
frame at random
Static Paging Algorithms
Dirty Bits
Another issue with frame replacement is copying out the old contents– If 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 memory
– Associate each frame with a dirty bit– When a page is loaded into the frame, set the dirty bit to 0– If the page in the frame is written to, set the dirty bit to 1– When 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
Static Paging Algorithms
Summary
This topic covered an introduction to virtual memory:– TBW
Static Paging Algorithms