Virtual Memory
Virtual Memory
N.B:
Virtual memory is commonly implemented by demand paging.
Demand Paging
• It is similar to paging system with swapping.
Criteria:
A page is not loaded into main memory from secondary memory, until
it is needed. So, a page is loaded into the main memory by demand.
Hence, it is called demand paging.
Basic Concepts:
• To implement this scheme, we need hardware support to distinguish
between those pages that are in memory and those pages that are in the
disk.
• So, the valid invalid bit can be used for this purpose.
• When this bit is set to “valid” the value indicates that the associated page is
legal and in memory.
• If the bit is set to “invalid” indicates that the page either not valid (i.e, not in
logical address space of process) or is valid but is currently on disk.
• The page table entry for a page that is brought into memory is set as valid
and the page that is not currently in memory is set as invalid or contains the
address of the page on disk.
• Pages marked as invalid will have no effect if the process never tries to
access that page.
Demand paging with page table
when some pages are not in main
memory
But, if a process tries to access a page that
was not brought into memory, then what
happened?
In the above example, 20 page requests result in 15 page faults. So, the page
fault rate= No. of page faults/No. of bits=15/20=75%
Belady’s anomaly
7 0 2 4 3 1 4 7 2 0 4 3 0 3
String2 77 0 2 4 3 1 4 7 2 0 4 3 0 3 2 7
Frame 2 2 2 1 1 1 2 2 2 3 3 3 3 3
3
Frame 0 0 0 3 3 3 7 7 0 0 0 0 0 2 7
2
Frame 7 7 7 4 4 4 4 4 4 4 4 4 4 4 4 4
1
Miss/ MM M M M M H M M M H M H H M M
Hit
In the above example, 16 page requests result in 12 page faults. So, the page fault
rate= No. of page faults/No. of bits=12/16=75%
MFU Page Replacement Algorithm
• In contrast to LFU, the MFU policy replaces the page that has been
visited the most times.
• This approach attempts to maintain pages that are used regularly and
eliminate those that were used frequently in the past but are no
longer used as frequently.
LRU Approximation Page
Replacement Algorithm
• Each page has a reference bit.
• All pages are arranged in a circular list (like a clock).
• When a page is referenced, its reference bit is set to 1.
• When a replacement is needed:
• The algorithm scans the pages in a circular manner.
• If it finds a page with a reference bit of 0, it replaces it.
• If the bit is 1, it gives the page a “second chance” by clearing the bit and
moving on.
Second Chance Algorithm
Frame Allocation Methods