Operating Systems
Virtual Memory
Background
❏ The term “virtual memory” refers to something which appears to be present but actually it is
not.
❏ The virtual memory technique allows users to use more memory for a program than the real
memory of a computer.
❏ Virtual memory is a concept that we use when we have processes that exceed the main
memory.
❏ When computer runs out of physical memory, it writes its requirement to the hard disc in a
swap file as “virtual memory”.
Demand Paging
❏ Bring a page into memory only when it is needed
- Less I/O needed
- Less memory needed
- Faster response
- More users
❏ Disadvantage: Page fault interrupt
❏ Required hardware support:
- Page Table with valid-invalid bit
- Secondary memory
Valid-Invalid Bit
❏ An extra bit in the page table which indicates the
existence of the page in the main memory.
❏ Attempt to access page
❏ If page is valid (in memory) then continue
processing instruction as normal.
❏ If page is invalid then a page-fault trap / page-fault
interrupt occurs.
❏ Page is needed ⇒ reference to it
- Invalid reference ⇒ abort
- Not-in-memory ⇒ bring to memory
Page Fault
If there is ever a reference to a page, first reference will
trap to OS ⇒ page fault
1. OS looks at another table to decide:
▪ Invalid reference ⇒ abort.
▪ Just not in memory.
2. Find empty/ free frame.
3. Load page from disk into frame.
4. Reset tables, validation bit = 1.
5. Restart instruction that caused page fault
Demand Paging Flowchart
Page Replacement
Page Replacement Algorithms:
❏ FIFO (First In First Out)
❏ LRU (Least Recently Used)
❏ OPT (Optimal)
FIFO (First In First Out)
❏ Selects the page for replacement that has been in the memory for the longest amount of time
LRU (Least Recently Used)
❏ Replace the least recently used page in the past
❏ Can be implemented by associating a counter with every page that is in main memory
Optimal
❏ Replace the page which is not used in longest dimension of time in future