Ch-10b - OS Virtual Memory2
Ch-10b - OS Virtual Memory2
CS3500
PROF. SUKHENDU DAS DEPTT. OF COMPUTER SCIENCE
AND ENGG., IIT MADRAS, CHENNAI – 600036.
Email: [email protected]
URL: http://www.cse.iitm.ac.in/~vplab/os.html
OCT. – 2022.
VM_ II
PAGE REPLACEMENT
WHAT HAPPENS IF THERE IS NO FREE FRAME?
Note now potentially 2 page transfers for page fault – increasing EAT;
Use dirty bit to swap out contents (for code, say ?)
PAGE REPLACEMENT
PAGE AND FRAME REPLACEMENT ALGORITHMS
• Frame-allocation algorithm determines
• How many frames to give each process
• Which frames to replace
• Page-replacement algorithm
• Want lowest page-fault rate on both first access and re-access
• Evaluate algorithm by running it on a particular string of memory references
(reference string) and computing the number of page faults on that string
• String is just page numbers, not full addresses
• Repeated access to the same page does not cause a page fault
• Results depend on number of frames available
• In all our examples, the reference string of referenced page numbers is
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
GRAPH OF PAGE FAULTS VERSUS THE NUMBER OF FRAMES
FIRST-IN-FIRST-OUT (FIFO) ALGORITHM
• Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
15 page faults
• 12 faults (?)
– better than FIFO but worse than OPT
• Stack implementation
• Keep a stack of page numbers in a double link form:
• Page referenced:
• move it to the top
• requires 6 pointers to be changed
• But each update more expensive
• No search for replacement
LRU ALGORITHM (CONT.)
• LRU and OPT are cases of stack algorithms that don’t have Belady’s
Anomaly
• Use of A Stack to Record Most Recent Page References
LRU APPROXIMATION ALGORITHMS
• LRU needs special hardware and still slow
• Reference bit
• With each page associate a bit, initially = 0
• When page is referenced bit set to 1
• Replace any with reference bit = 0 (if one exists)
• We do not know the order, however.
• Additional Reference bits used too – use K-bit Shift Reg; least No. replaced
• Second-chance algorithm
• Generally FIFO, plus hardware-provided reference bit
• “Clock” replacement algo.
• If page (victim) to be replaced has
• Reference bit = 0 -> replace it
• reference bit = 1 then:
• set reference bit 0, leave page in memory
• replace next victim page, subject to same rules
SECOND-CHANCE
ALGORITHM
ENHANCED SECOND-CHANCE ALGORITHM
• Improve algorithm by using reference bit and modify bit (if available) in
concert
• Take ordered pair (reference, modify):
• (0, 0) neither recently used not modified – best page to replace
• (0, 1) not recently used but modified – not quite as good, must write out
before replacement
• (1, 0) recently used but clean – probably will be used again soon
• (1, 1) recently used and modified – probably will be used again soon and
need to write out before replacement
• When page replacement called for, use the clock scheme but use the four
classes - replace page in lowest non-empty class
• Might need to search circular queue several times
COUNTING ALGORITHMS
• Keep a counter of the number of references that have been made to each page
• Not common
• Lease Frequently Used (LFU) Algorithm:
• Replaces page with smallest count – page heavily used and then idle in
memory for a long time; Solution – use SR - exponentially decaying average
usage count.
• Most Frequently Used (MFU) Algorithm:
• Based on the argument that the page with the smallest count was probably just
brought in and has yet to be used
• Possibly, keep free frame contents intact and note what is in them
• If referenced again before reused, no need to load contents again from disk
• Generally useful to reduce penalty if wrong victim frame selected
APPLICATIONS AND PAGE REPLACEMENT
• All of these algorithms have OS guessing about future page access
• Some applications have better knowledge – i.e. databases
• Memory intensive applications can cause double buffering
• OS keeps copy of page in memory as I/O buffer
• Application keeps page in memory for its own work
• Operating system can given direct access ( ie the ability to use a secondary
storage partition as a large sequential array of logical blocks) to the disk,
getting out of the way of the applications
• Raw disk mode – not like conventional File system access
• Bypasses buffering, locking, etc.