8 Virtual Memory And Paging

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.

Virtual Memory and Paging: notes

Page fault definition and occurrence

A page fault happens when a process uses a page that is not in main memory. It is a trap to the OS. The method is called demand paging: a page is loaded only when it is needed.

What happens on a page fault:

  1. The CPU checks the page table. The valid/invalid bit shows the page is not in memory.
  2. A trap passes control to the OS.
  3. The OS finds the page on the disk.
  4. It finds a free frame. If none is free, a page replacement algorithm picks a victim page.
  5. The page is read from disk into the frame.
  6. The page table is updated and the instruction is restarted.

Page faults are slow because they involve disk access. The effective access time is:

$$EAT = (1 - p) \times t_m + p \times t_f$$

Here $p$ is the page fault rate, $t_m$ is the memory access time and $t_f$ is the page fault service time. Even a very small $p$ slows the system a lot.

FIFO page replacement algorithm

FIFO replaces the page that has been in memory the longest. A queue of pages in order of loading is enough to run it.

Example: reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 3 frames.

ReferenceFrames afterFault
77Yes
07 0Yes
17 0 1Yes
22 0 1Yes
02 0 1No
32 3 1Yes
02 3 0Yes
44 3 0Yes
24 2 0Yes
34 2 3Yes
00 2 3Yes
30 2 3No
20 2 3No

Total faults = 10.

FIFO can show Belady's anomaly: giving a process more frames can sometimes cause more faults.

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 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.

RefFrames (contents)Fault
77F
07 0F
17 0 1F
27 0 1 2F
07 0 1 2Hit
3replace 1 → 7 0 3 2F
07 0 3 2Hit
4replace 7 → 4 0 3 2F
24 0 3 2Hit
3Hit
0Hit
3Hit
2Hit
3Hit

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.

RefFramesFIFO QueueFault
77[7]F
07 0[7,0]F
17 0 1[7,0,1]F
27 0 1 2[7,0,1,2]F
07 0 1 2[7,0,1,2]Hit
3replace 7 → 0 1 2 3[0,1,2,3]F
00 1 2 3[0,1,2,3]Hit
4replace 0 → 1 2 3 4[1,2,3,4]F
21 2 3 4[1,2,3,4]Hit
31 2 3 4[1,2,3,4]Hit
0replace 1 → 2 3 4 0[2,3,4,0]F
32 3 4 0[2,3,4,0]Hit
22 3 4 0[2,3,4,0]Hit
32 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).

RefFrames (queue oldest→newest)ActionFault
77(0)loadF
07(0) 0(0)loadF
17(0) 0(0) 1(0)loadF
27(0) 0(0) 1(0) 2(0)loadF
07(0) 0(1) 1(0) 2(0)hit, set R(0)=1Hit
37 is oldest, R=0 → evict 7. New: 0(1) 1(0) 2(0) 3(0)replace 7F
00(1) 1(0) 2(0) 3(0)hit (0 already R=1)Hit
4Oldest 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 1F
22(1) 3(0) 0(0) 4(0)hit, set R=1Hit
32(1) 3(1) 0(0) 4(0)hit, set R=1Hit
02(1) 3(1) 0(1) 4(0)hit, set R=1Hit
32(1) 3(1) 0(1) 4(0)hitHit
22(1) 3(1) 0(1) 4(0)hitHit
32(1) 3(1) 0(1) 4(0)hitHit

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

AlgorithmPage Faults
Optimal6
FIFO7
Second Chance6
Open this answer on its own page →

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]

A page fault occurs when a process references a page that is not currently present in main memory (RAM). The OS traps the fault, locates the page on disk, finds a free frame (or evicts a page via a replacement algorithm), loads the page, updates the page table, and restarts the instruction. --- -...

Open this answer on its own page →

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]

--- Virtual memory is a memory management technique that allows a computer to execute programs that are larger than the available physical memory (RAM). It creates an illusion of a very large memory space for each process by using a portion of the secondary storage (hard disk) as an extension of ...

Open this answer on its own page →

2079 · 5 marks

What is TLB? Explain the importance of TLB in conversion of logical address to physical address. [5]

Show solved 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 NumberFrame Number
pf

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:

  1. One to fetch the page table entry (from main memory)
  2. 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 p is found in the TLB.
  • The corresponding frame number f is retrieved directly.
  • Physical address = f * page_size + d
  • Only one memory access is needed (for actual data).

Case 2: TLB Miss

  • The page number p is 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

PointExplanation
SpeedAvoids slow page table lookup in main memory on a hit
EfficiencyReduces average memory access time significantly
TransparencyWorks automatically without programmer involvement
LocalityExploits 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.

Open this answer on its own page →

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]

Best: The Optimal (OPT / Belady's) algorithm replaces the page that will not be used for the longest time in the future. This produces the minimum possible number of page faults for any reference string and frame count. It is the theoretical lower bound against which all real algorithms are measu...

Open this answer on its own page →

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]

  • Reference string: 3, 2, 1, 3, 4, 2, 8, 9, 7, 4, 2, 9, 8, 3 (14 references) - Frames = 4 --- Replace the page whose next use is furthest in the future. Step Page Frames after the reference Fault? Replacement reasoning ----------------------------------------------------------------------- 1 3 {3...
Open this answer on its own page →