0% found this document useful (0 votes)
36 views31 pages

PageAlgorithms

Memory management algorithms aim to efficiently utilize limited physical memory by replacing pages on disk with pages in memory as needed. Common page replacement algorithms include: - Least Recently Used (LRU) which replaces the page that has been unused for the longest time, aiming to retain heavily used pages in memory. - Second Chance modifies FIFO to avoid replacing heavily used pages by moving referenced pages to the back of the queue. - Clock uses a circular list and advances a pointer to the oldest page on each replacement, retaining referenced pages.

Uploaded by

Tu Tu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views31 pages

PageAlgorithms

Memory management algorithms aim to efficiently utilize limited physical memory by replacing pages on disk with pages in memory as needed. Common page replacement algorithms include: - Least Recently Used (LRU) which replaces the page that has been unused for the longest time, aiming to retain heavily used pages in memory. - Second Chance modifies FIFO to avoid replacing heavily used pages by moving referenced pages to the back of the queue. - Clock uses a circular list and advances a pointer to the oldest page on each replacement, retaining referenced pages.

Uploaded by

Tu Tu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Memory Management

Page Replacement Algorithms


Review
• Memory Manager
– coordinate how the different types of memory are used
– keep track memory to allocate and release areas of main
memory to processes
– manage swapping between main memory and disk
• No Memory Abstractions
– Single program
• One OS, only one process
• Special register is used to protection between OS and process
• Disadvantages: slow
– Multiple program
• One OS, many processes locate in memory
• Divided into fixed size block
• Disadvantages
– Internal fragmentations
– Two programs both reference absolute physical memory
 static relocation
Review
• Memory Abstractions
– Multiple processes locate in memory (both primary and secondary)
– Base and Limit Registers
• Multiple processes locate in memory and protection
• Base contains the first address of process in memory, Limit contains the
length of process, the process owns private address space
• Disadvantages
– External fragmentation (Defragment)
– Slow
– Swapping
• Multiple processes with ready processes locate in memory and passive
processes locate in HDD – swap file area
• Swap out/ in operator
• Disadvantages
– External fragmentation
– Process can not grow in memory and the swap area on the disk is full
 data segment upward, stack segment downward
Review
• Memory Abstractions
– Memory Management with Bitmaps
• The memory divides units with same size that has a bit corresponding bit
in the bitmap (0: free, 1: occupied)
• Disadvantages
– Slow when searching the bitmap to find a run of k consecutive 0 bits in
the map (small)
– Internal fragment (large size)
– Memory Management with Linked Lists
• Maintain a linked list of allocated (P) and free memory (H)
• Allocating algorithms
– First, Next, Best, Worst, Quick Fit
– Virtual Memory
• Software/ Process sizes larger than memory
• Overlays
– Developer splits program to many overlays
– Disadvantages: developer‘s knowledge is important
Review
• Memory Abstractions
– Virtual Memory
• Paging
– Address space is broken up into pages
– Physical memory is divided up into page frames
– Virtual address vs. Physical address, manage address space with bit
– MMU transfers Virtual address  p, d; then it looks up page table
following the index to get the page frame; the page frame combines
with d to determine the physical address
– Page fault: the program references a part of its address space that is
not in physical memory
– Page table
» Load to collection registers, load to memory using base register,
using TLB
» Excessive large page table: multilevel page, inverted page table,
inverted page table with hash or TLB
» Entry: Caching disabled, Referenced, Modified, Protection,
Present/absent, page frame number
Page replacement algorithms
Problems
• At page fault and full physical memory
– Space has to be made
– A currently loaded virtual page has to be evicted from memory
• Choosing the page to be evicted
– Not a heavily used page  reduce the number of page faults
• Page replacement
– The old page has to be written on the disk if it was modified
– The new virtual page overwrite the old virtual page into the page
frame
Page replacement algorithms
Optimal
• Each page can be labeled with the number of instructions
that will be executed before that page is first reference
• Choose the page that will be the latest one accessed in the
future between all the pages actually in memory
• Very simple and efficient (optimal)
• Impossible to be implemented in practice
– there is no way to know when each page will be referenced next
• It can be simulated
– At first run collect information about pages references
– At second run use results of the first run (but with the same input)
• It is used to evaluate the performance of other, practically
used, algorithms
Page replacement algorithms
Optimal
• Ex:
– a memory with free three frames
–7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 0 701

– Number of page faults are 9 times


– Number of page hits are 12 times
→ minimum of page fault is at least size of page frame that
is allocated to process
→ principles: more and more page frame is allocated, less
and less page fault occurs
Page replacement algorithms
Not Recently Used (NRU)
• Each page has two status bits associated
– Referenced bit (R)
– Modified bit (M)
• The two bits
– updated by the hardware at each memory reference
– once set to 1 remain so until they are reset by OS
– can be also simulated in software when the mechanism is not supported by hardware
• Algorithms
– At process start the bits are set to 0
– Periodically (on each clock interrupt) the R bit is cleared
– For page replacement, pages are classified
• Class 0: not referenced, not modified
• Class 1: not referenced, modified
• Class 2: referenced, not modified
• Class 3: referenced, modified
• The algorithm removes a page at random from the lowest numbered nonempty
class
• It is easy to understand, moderately efficient to implement, and gives an adequate
performance
Page replacement algorithms
Not Recently Used (NRU) – Example
• A computer has four page frames. The time of loading, time of last
access, and the R and M bits for each page are as shown below (the
times are in clock ticks). Which page will be replaced?
Page Loaded Last R M
Reference
0 226 280 0 0
1 160 265 0 1
2 110 270 1 0
3 120 285 1 1

• The page 0 is replaced


Page replacement algorithms
First-In, First-Out (FIFO)
• OS maintains a list of all pages currently in memory, with
– the most recent arrival at the tail
– the least recent arrival at the head
• On a page fault, the page at the head is removed and the
new page added to the tail of the list
• It’s rarely used
• Ex:
– a memory with free three frames
–7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 010 7 0 1
Page replacement algorithms
Second-Chance
• A modification of FIFO to avoid throwing out a heavily
used page
• Inspect the R bit of the oldest page
• 0  page is old and unused  replaced
• 1  page is old but used  its R bit = 0 and the page is moved at the end
of the queue as a new arrived page
• Look for an old page that has not been not referenced in
the previous clock interval
• If all the pages have been references  FIFO

Tanenbaum, Fig. 3-15.


Page replacement algorithms
Second-Chance – Example
• A computer has four page frames. The time of loading, time of last
access, and the R and M bits for each page are as shown below (the
times are in clock ticks). Which page will be replaced?
Page Loaded Last R M
Reference
0 226 280 0 0
1 160 265 0 1
2 110 270 1 0
3 120 285 1 1

• The page 1 is replaced


Page replacement algorithms
Clock
• In a second chance algorithms, it is unnecessarily
inefficiency because it is constantly moving pages around
on its list
• Keep all the page frames on a circular list in the form of a
clock
• The hand points to the oldest page
• How to work
– When a page fault occurs, the page being pointed to by the hand
is inspected
– If its R bit is 0, the page is evicted, the new page is inserted into
the clock in its place, and the hand is advanced one position
– If R is 1, it is cleared and the hand is advanced to the next page
Page replacement algorithms
Clock

Tanenbaum, Fig. 3-16.


Page replacement algorithms
Least Recently Used (LRU)
• Based on the observation that
– pages that have been heavily used in the last few instructions will probably be heavily
used again in the next few
• Throw out the page that has been unused for the longest time when a page
fault occurs
• The algorithm keeps a linked list
– the referenced page is moved at the front of the list
– the page at the end of the list is replaced
– the list must be updated at each memory reference  costly
• Implementing LRU with a hardware counter
– keep a 64-bit counter which is incremented after each instruction
– each page table entry has a field large enough to store the counter
– the counter is stored in the page table entry for the page just referenced
– the page with the lowest value of its counter field is replaced
• Implementing LRU with a hardware bits matrix
– N page frames  N x N bits matrix
– when virtual page k is referenced
• bits of row k are set to 1
• bits of column k are set to 0
– the page with the lowest value is removed
Page replacement algorithms
Least Recently Used (LRU)
• Ex:
– a memory with free three frames
–7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 10 7 01

– A memory with four page frames


– 0123210323

Tanenbaum, Fig. 3-17.


Page replacement algorithms
Least Recently Used (LRU) – Example
• A computer has four page frames. The time of loading, time of last
access, and the R and M bits for each page are as shown below (the
times are in clock ticks). Which page will be replaced?
Page Loaded Last R M
Reference
0 226 280 0 0
1 160 265 0 1
2 110 270 1 0
3 120 285 1 1

• The page 1 is replaced


Page replacement algorithms
Not Frequently Used (NFU)
• A software implementation of LRU
• A software counter associated with each page (initially 0)
• At each clock tick the R bit is added to the counter for all
the pages in memory; after that the R bits are reset to 0
• The page with the lowest counter is chosen when a page
fault occurs
• Problem:
– The useful pages are evicted
– The bit reference is useful to count the page to be reference, but
it is not known the order of reference
Page replacement algorithms
Aging
• A software implementation of LRU
• modification of NFU
– Shift right the counter one position
– R bit is added to the leftmost bit
– The page whose counter is the lowest is removed when a page
fault occurs
– Differences from LRU
• does not know the page which was referenced first between two ticks;
for example pages 3 and 5 at step (e)
• the finite number of bits of counters → does not differentiate
between pages with the value 0 of their counter
Page replacement algorithms
Aging

Tanenbaum, Fig. 3-18.


Page replacement algorithms
Thrashing
• A program causing page faults every few instructions
• A process is not allocated enough page frames as its requirements
→ Many processes are located in memory, thus, each process is allocated at least
page frames
→ The page replacement takes more time (than execution)
→ The CPU utilization is decreased and some new process is loaded in
the memory
• The impact of thrashing on system
– The system can be hanged
– The process can not continually progress
• Solution
– Allocating enough page frame (!?) to the process
– How to know the process need the number of page frame in progress
Page replacement algorithms
Locality of Reference
• The process references only a relatively small fraction
of its pages in execution
– A small fraction is a set of pages that a process is currently
using
– The process moves from small fractions (that is needed in
execution in time) to others
• The process involves many small fractions that can
intersected
Page replacement algorithms
Demand paging vs. Prepaging
• Demand paging: Pages are loaded only on demand
– Processes are started up with none of their pages in memory
– CPU tries to fetch the first instruction → page fault
• Prepaging
– Loading the pages before letting processes run
– Loading the pages before resuming the process
Page replacement algorithms
Working Set
• The set of pages that a process is currently using
• If the entire working set is in memory, the process will
run without causing many faults until it moves into
another execution phase
• Working set can changes over time
• Problems
– If the available memory is too small to hold the entire
working set, the process will cause many page faults and run
slowly → thrashing
– The process will just cause page faults until its working set
has been loaded → slow, waste considerable CPU time
• Solutions
– Using working set model
Page replacement algorithms
Working Set Model
• Keep track of each process’s working set and make sure that it is
in memory before letting the process run (prepaging)
• Reduce the page fault rate (avoid thrashing)
• Is implemented as
– OS keeps track of which pages are in the working set
– When a page fault occurs, find a page not in the working set and this page is
located in
– R bit is examined.
– If R’s value is 1, the current virtual time is written into the Time of last use
field
– If R’s value is 0, the page may be a candidate to removal
• The age (the current virtual time – its Time of last use) is computed and
compared with τ (span multiple clock ticks).
• If age > τ, the page is no longer in the working set, and the new page replaces it
• Otherwise, the page is still in the working set
• If the entire table is scanned without finding the candidate to evict and one or
more pages with R = 0 is founds, the one with the greatest age is evicted.
Otherwise, the chosen random is executed (R = 1)
Page replacement algorithms
• Problem WSClock
– Working set algorithm is cumbersome, since the entire page table has to be
scanned at each page fault until a suitable candidate is located
• WSClock
– Improved WS algorithm is based on the clock algorithm
– Simplicity of implementation and good performance
– A circular list of page frames is used
– How it works
• This list is empty (initially)
• The page is loaded, it is added to the list to form a ring (with Time of last use
field and R bit)
• As with the clock program, at each page fault the page pointed to by hand is
examined first
• If R is set to 1, the page has been used during the current tick, then R set to 0, the
hand advanced to next page
• If R equals 0,
– if the age is greater than τ and the page is clean, it is not in working set and a valid copy
exists on disk. The page frame is simply claimed and the new page put there
– If the page dirty, it cannot claim immediately (clean page is define base on write to disk
scheduled)
Page replacement algorithms
WSClock

Tanenbaum, Fig. 3-21.


Page replacement algorithms
Summary

Tanenbaum, Fig. 3-22.


Summary
• Page replacement Algorithms

Q&A
Next Lecture
• Design the Paging Systems

You might also like