Operating Systems · Unit 8
Virtual Memory and Paging
Exam-focused notes for Virtual Memory and Paging (Operating Systems, BIT204): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Virtual Memory and Paging in short
Virtual memory keeps only some pages of a process in RAM. When the process needs a page that is not there, the OS must load it. This chapter explains page faults, page replacement and the TLB.
What this unit covers
- Page fault definition and occurrence
- FIFO page replacement algorithm
- Second Chance page replacement algorithm
- Optimal page replacement algorithm
- LRU page replacement algorithm
- Translation Lookaside Buffer
- TLB importance in address conversion
Page fault definition and occurrence
2082 · 10 marks
When does page fault occur?Given the 7 page reference strings 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3, find the number of page fault using Optimal Page Replacement, FIFO and Second Chance Replacement algorithm. Assume the size of page frame is 4.[1+9]
Show solved answerHide answer
Page Fault and Page Replacement Algorithms
When Does a Page Fault Occur?
A page fault occurs when a process references a page that is not currently present in main memory (RAM). The CPU generates a trap, and the OS must bring the required page from secondary storage into a frame. If no free frame exists, a page replacement algorithm chooses a victim to evict.
Given Data
- Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3
- Number of frames: 4
- Total references: 14
1. Optimal Page Replacement
Replace the page not needed for the longest time in the future.
| Ref | Frames (contents) | Fault |
|---|---|---|
| 7 | 7 | F |
| 0 | 7 0 | F |
| 1 | 7 0 1 | F |
| 2 | 7 0 1 2 | F |
| 0 | 7 0 1 2 | Hit |
| 3 | replace 1 → 7 0 3 2 | F |
| 0 | 7 0 3 2 | Hit |
| 4 | replace 7 → 4 0 3 2 | F |
| 2 | 4 0 3 2 | Hit |
| 3 | Hit | |
| 0 | Hit | |
| 3 | Hit | |
| 2 | Hit | |
| 3 | Hit |
At ref 3 (index 6), page 1 is never used again → evict 1.
At ref 4, remaining future refs: 2,3,0,3,2,3. Page 7 never used again → evict 7.
Optimal Page Faults = 6
2. FIFO Page Replacement
Replace the oldest loaded page.
| Ref | Frames | FIFO Queue | Fault |
|---|---|---|---|
| 7 | 7 | [7] | F |
| 0 | 7 0 | [7,0] | F |
| 1 | 7 0 1 | [7,0,1] | F |
| 2 | 7 0 1 2 | [7,0,1,2] | F |
| 0 | 7 0 1 2 | [7,0,1,2] | Hit |
| 3 | replace 7 → 0 1 2 3 | [0,1,2,3] | F |
| 0 | 0 1 2 3 | [0,1,2,3] | Hit |
| 4 | replace 0 → 1 2 3 4 | [1,2,3,4] | F |
| 2 | 1 2 3 4 | [1,2,3,4] | Hit |
| 3 | 1 2 3 4 | [1,2,3,4] | Hit |
| 0 | replace 1 → 2 3 4 0 | [2,3,4,0] | F |
| 3 | 2 3 4 0 | [2,3,4,0] | Hit |
| 2 | 2 3 4 0 | [2,3,4,0] | Hit |
| 3 | 2 3 4 0 | [2,3,4,0] | Hit |
FIFO Page Faults = 7
3. Second Chance (Clock) Page Replacement
FIFO order maintained, but on a hit the reference bit R is set to 1. When selecting a victim, if the oldest page has R=1, clear it to 0 and move it to the back (give it a second chance); if R=0, replace it.
Notation: page(R).
| Ref | Frames (queue oldest→newest) | Action | Fault |
|---|---|---|---|
| 7 | 7(0) | load | F |
| 0 | 7(0) 0(0) | load | F |
| 1 | 7(0) 0(0) 1(0) | load | F |
| 2 | 7(0) 0(0) 1(0) 2(0) | load | F |
| 0 | 7(0) 0(1) 1(0) 2(0) | hit, set R(0)=1 | Hit |
| 3 | 7 is oldest, R=0 → evict 7. New: 0(1) 1(0) 2(0) 3(0) | replace 7 | F |
| 0 | 0(1) 1(0) 2(0) 3(0) | hit (0 already R=1) | Hit |
| 4 | Oldest 0 has R=1 → clear to 0, move back. Next oldest 1 has R=0 → evict 1. New: 2(0) 3(0) 0(0) 4(0) | replace 1 | F |
| 2 | 2(1) 3(0) 0(0) 4(0) | hit, set R=1 | Hit |
| 3 | 2(1) 3(1) 0(0) 4(0) | hit, set R=1 | Hit |
| 0 | 2(1) 3(1) 0(1) 4(0) | hit, set R=1 | Hit |
| 3 | 2(1) 3(1) 0(1) 4(0) | hit | Hit |
| 2 | 2(1) 3(1) 0(1) 4(0) | hit | Hit |
| 3 | 2(1) 3(1) 0(1) 4(0) | hit | Hit |
Step detail at ref 4: clock pointer at oldest = 0 (R=1) → set R=0, advance; page 1 (R=0) → victim. Result frames = {2,3,0,4}. This matches FIFO's replacement here because giving 0 a second chance saved it.
Second Chance Page Faults = 6
Summary
| Algorithm | Page Faults |
|---|---|
| Optimal | 6 |
| FIFO | 7 |
| Second Chance | 6 |
0 · 10 marks
What is page fault? Consider the following page reference string, 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1. How many page faults would occur for each of the following page replacement algorithms assuming 3 frames? a. FIFO b. Second Chance c. LRU[10]
Translation Lookaside Buffer
2080 · 10 marks
Why virtual memory technique is used in the computer system? What is logical address? Explain the process of conversion of logical address to physical address using TLB.[10]
2079 · 5 marks
What is TLB? Explain the importance of TLB in conversion of logical address to physical address. [5]
Show solved answerHide answer
Translation Lookaside Buffer (TLB)
Definition
A Translation Lookaside Buffer (TLB) is a small, fast hardware cache (associative memory) that stores recently used page table entries. It acts as a high-speed lookup table used by the Memory Management Unit (MMU) to speed up the translation of logical (virtual) addresses to physical addresses.
Note: No specific reference notes were found for this topic; the answer is based on standard OS theory as taught in BSc CSIT curriculum.
Structure of TLB
Each entry in the TLB contains:
| Page Number | Frame Number |
|---|---|
| p | f |
The TLB is implemented using associative memory, meaning all entries are searched simultaneously (in parallel), making lookup extremely fast.
Importance of TLB in Logical to Physical Address Conversion
Without TLB, every memory access requires two memory accesses:
- One to fetch the page table entry (from main memory)
- One to access the actual data/instruction
TLB eliminates this overhead in most cases.
Step-by-Step Address Translation with TLB
Logical Address = (Page Number p, Offset d)
|
Search TLB
/ \
TLB Hit TLB Miss
| |
Get frame f Access Page Table
| (in main memory)
| |
| Get frame f
| Update TLB
| |
+--------+--------+
|
Physical Address = (f, d)
|
Access Memory
Case 1: TLB Hit
- The page number
pis found in the TLB. - The corresponding frame number
fis retrieved directly. - Physical address =
f * page_size + d - Only one memory access is needed (for actual data).
Case 2: TLB Miss
- The page number
pis not found in the TLB. - The page table in main memory is accessed to find frame
f. - The entry
(p, f)is loaded into TLB for future use. - Physical address is then formed.
- Two memory accesses are needed.
Effective Memory Access Time (EMAT)
$$EMAT = \alpha \times (T_{TLB} + T_m) + (1 - \alpha) \times (T_{TLB} + 2T_m)$$
Where:
- $\alpha$ = TLB hit ratio
- $T_{TLB}$ = TLB access time
- $T_m$ = Main memory access time
A high hit ratio (typically 90-99%) means most accesses are fast, greatly reducing average memory access time.
Summary of Importance
| Point | Explanation |
|---|---|
| Speed | Avoids slow page table lookup in main memory on a hit |
| Efficiency | Reduces average memory access time significantly |
| Transparency | Works automatically without programmer involvement |
| Locality | Exploits temporal/spatial locality of memory references |
In conclusion, TLB is a critical hardware component that makes paged memory systems practical and efficient by caching frequently used address translations.
Optimal page replacement algorithm
2080 · 10 marks
Why Optimal Page Replacement is best but not practically feasible page replacement algorithm? Calculate the number of page faults for Optimal, LRU and FIFO replacement algorithm for the reference string: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6, 3, 2, 1, 4, 2 using 3 page frames.[10]
2079 · 5 marks
Consider the following page reference string: 3,2,1,3,4,2,8,9,7,4,2,9,8,3. Calculate the total number of page faults for Optimal and LRU page replacement algorithms using a 4 page frame. [5]
Make Unit 8 stick
Practice BIT204 with flashcards & quizzes