Operating Systems chapters (5)
  1. 01OS Structures & System Calls
  2. 02Process & Thread Management
  3. 03CPU Scheduling Algorithms
  4. 04Process Synchronization & Deadlocks
  5. 05Memory Management & Virtual Memory

Chapter 05 · OS

Memory Management & Virtual Memory

4 topics · 2 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

The OS must share main memory among processes safely and efficiently.

This chapter covers contiguous allocation and fragmentation, paging with page tables and the TLB, virtual memory with demand paging, page replacement (FIFO, LRU, Optimal with Belady’s anomaly) and thrashing.

Address translation, effective access time and page-fault counting are core GATE numericals every year.

Understanding why processes can use more memory than RAM, tuning swap, diagnosing slow systems and designing efficient data access patterns.

1

Logical vs Physical Memory

A logical (virtual) address is generated by the CPU and seen by the program; the set of all logical addresses is the logical address space. A physical address is the actual location in main memory seen by the memory unit; the set of all of them is the physical address space. The Memory Management Unit (MMU) maps logical to physical addresses at run time.

In short: The program thinks it owns addresses 0 to N; the MMU secretly places it wherever there is room.

Logical Address (CPU) Page (p) Offset (d) PAGE TABLE Page 0 ➜ Frame 3 Page 1 ➜ Frame 7 Page 2 ➜ Frame 1 Page 3 ➜ Frame 5 Physical RAM (DRAM) Frame (f) Offset (d)
Memory Management & Virtual Memory

Think about it

It is the foundation of all memory management; GATE asks for address translation, bits in logical vs physical addresses and base/limit protection.

Address binding can happen at compile time (absolute code, location must be known), load time (relocatable code) or execution time (needs hardware MMU; logical and physical addresses differ, used by modern OSs).

In the simplest MMU a relocation (base) register is added to every logical address: physical = base + logical, and a limit register checks logical < limit for protection.

Example

  • 1base = 14000, limit = 3000: logical 346 → physical 14346
  • 2logical 3500 → trap (addressing error)

Swapping moves whole processes between memory and backing store; dynamic loading and dynamic linking load routines on demand.

With paging the MMU splits the logical address into page number and offset; with segmentation into segment number and offset (physical = base[s] + d if d < limit[s]).

Logical address space size = 2^(address bits); physical address space size = 2^(physical address bits) and they can differ (a 32-bit logical space on a 16 GB machine).

The user program never sees physical addresses — this is what enables relocation, protection and virtual memory.

Key points

  • MMU: Hardware that translates logical addresses to physical addresses at run time.
  • Relocation register: Base register added to every logical address.
  • Limit register: Largest legal logical address; used for protection.
  • Address binding: Mapping program symbols to memory addresses (compile, load or execution time).
  • Compile-time and load-time binding give identical logical and physical addresses; execution-time binding makes them differ.
  • Segment table example: segment 2 has base 4300, limit 400: logical (2, 53) → 4353; logical (2, 500) → trap.
  • Number of bits: logical address bits = log2(logical address space); physical address bits = log2(physical memory size).
2

Contiguous allocation and fragmentation

In contiguous allocation each process occupies one continuous block of memory. Fixed partitions cause internal fragmentation (unused space inside a block); variable partitions cause external fragmentation (free holes too small to use).

In short: Parking cars in one continuous row: gaps appear as cars leave and a large car may not fit any single gap.

Think about it

Fragmentation motivates paging and segmentation.

Placement strategies for variable partitions: first fit (first hole big enough, fast), best fit (smallest sufficient hole, leaves tiny leftovers), worst fit (largest hole).

First and best fit are generally better than worst fit in speed and utilization.

Compaction moves processes to merge holes but needs dynamic relocation (base register) and is costly.

Protection uses base and limit registers: the MMU checks that a logical address is below the limit and adds the base.

Fifty-percent rule: with first fit, about one third of memory may become unusable due to external fragmentation.

Key points

  • Internal fragmentation: Wasted space inside an allocated block.
  • External fragmentation: Total free memory is enough but not contiguous.
  • Compaction: Shuffling allocated blocks together to form one large free block.
  • Paging has no external fragmentation but suffers internal fragmentation (on average half a page per process).
3

Paging & TLB Architecture

Paging divides the logical address space into fixed-size pages and physical memory into frames of the same size. A page table maps page number to frame number. A logical address is split into page number (p) and offset (d).

In short: Break a book into numbered pages and place them in any free shelf slots; keep a table saying which slot holds which page.

Think about it

Removes external fragmentation and permits non-contiguous allocation.

If logical address has m bits and page size is 2^n, then page number has m - n bits and the offset has n bits.

Physical address = frame number × page size + offset.

Page-table size = (number of pages) × (entry size).

Each entry also has valid/invalid, protection, dirty and reference bits.

A process needs ceil(size/page size) frames and wastes part of the last page.

Large address spaces use multilevel (hierarchical), hashed or inverted page tables.

Smaller pages reduce internal fragmentation but enlarge the page table and increase disk I/O overhead; the optimal page size balances the two (about √(2·s·e) for process size s and entry size e).

Key points

  • Page / frame: Fixed-size block of logical / physical memory.
  • Offset: Position within a page; unchanged by translation.
  • Inverted page table: One entry per physical frame rather than per page.
  • Number of pages = logical address space / page size; number of frames = physical memory / page size.
  • Two-level paging needs two extra memory accesses per reference without a TLB (three total).
  • TLB architecture: the CPU sends the page number to the TLB (associative lookup in parallel); on a hit the frame is available immediately, on a miss the page table is walked and the entry is loaded into the TLB. Full path: logical address → TLB → (miss) page table → frame + offset → physical address → memory.
  • Two-level paging example: 32-bit address, 4 KB pages, 4-byte PTE: 20 page-number bits split as 10 + 10; each page-table page holds 1024 entries (4 KB), so one inner table fits exactly one page. Multi-level EAT with TLB: h(t + m) + (1 − h)(t + (k + 1)m) for k levels.
4

TLB and effective access time

The Translation Lookaside Buffer (TLB) is a small, fast associative cache of recent page-to-frame mappings. Effective access time (EAT) combines TLB hit and miss costs.

In short: A sticky note of the most-used page-table entries so the OS avoids walking the table every time.

Think about it

Without a TLB every memory access would cost double or more.

On a TLB hit the frame number is available at once, so one memory access is needed.

On a miss, the page table in memory is read (one extra access for single-level paging) and the TLB is updated.

With hit ratio h, TLB search time t and memory access time m (single-level): EAT = h(t + m) + (1 - h)(t + m + m).

On a context switch the TLB is flushed unless entries carry an address-space identifier (ASID).

High locality of reference gives a hit ratio above 90 percent.

Key points

  • Hit ratio: Fraction of references found in the TLB.
  • ASID: Address-space identifier avoiding TLB flushes on switches.
  • Locality: Programs tend to reuse recent or nearby addresses.
  • EAT = h·(t + m) + (1 - h)·(t + 2m) for single-level paging.
5

Virtual memory and demand paging

Virtual memory lets a process execute even if only part of it is in physical memory. Demand paging loads a page only when it is first referenced; a reference to a page not in memory causes a page fault.

In short: Keep only what you are using on your desk and fetch other papers from the cupboard (disk) when needed.

Memory mapping for a process

Think about it

Decouples program size from physical memory size.

Page fault handling: trap to the OS → check the reference is valid → find a free frame (or choose a victim) → read the page from disk → update page table (valid bit) and TLB → restart the instruction.

Effective access time with page-fault rate p: EAT = (1 - p)·m + p·(page-fault service time).

Because disk time is milliseconds while memory is nanoseconds, p must be tiny.

Copy-on-write, memory-mapped files and shared libraries use virtual memory.

A dirty (modified) bit avoids writing back unmodified victim pages.

Benefits

programs larger than RAM, higher degree of multiprogramming, simpler programming.

Key points

  • Page fault: Trap raised when the referenced page is not in memory.
  • Valid-invalid bit: Marks whether the page is currently in memory.
  • Dirty bit: Set when page was modified and must be written back on eviction.
  • If memory access is 100 ns and fault service is 8 ms, even p = 0.001 gives EAT ≈ 0.999·100 + 0.001·8,000,000 ≈ 8100 ns, an 81× slowdown.
6

Page Replacement (LRU, FIFO)

When no free frame exists the OS picks a victim page to evict. FIFO evicts the oldest loaded page, LRU evicts the page unused for the longest time, Optimal (OPT) evicts the page that will not be used for the longest time in the future. Thrashing (see the next topic) is excessive paging that leaves the CPU mostly idle.

In short: FIFO throws out the oldest, LRU the least recently used, OPT the one needed furthest in the future (impossible in practice, used as benchmark).

Think about it

Replacement quality determines the page-fault rate and thus overall speed.

OPT gives the lowest fault count and is a yardstick.

LRU approximates OPT using the past, does not suffer from Belady’s anomaly (it is a stack algorithm) and is implemented approximately with reference bits, the clock (second-chance) algorithm or counters.

FIFO is simple but can show Belady’s anomaly: more frames can produce more faults.

Thrashing: if a process lacks enough frames for its working set, it faults continuously; the OS sees low CPU utilization and admits more processes, which worsens things.

Remedies: working-set model (keep the pages used in the last Δ references), page-fault-frequency control, local replacement, and reducing the degree of multiprogramming.

Frame allocation: equal, proportional to size, or priority-based; global vs local replacement.

Key points

  • Belady’s anomaly: Increasing frames increases faults (seen in FIFO, never in LRU or OPT).
  • Working set: Set of pages referenced in the last Δ memory references.
  • Second chance (clock): FIFO that skips pages with reference bit 1 after clearing it.
  • Fault count for reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 with 3 frames: FIFO 15, LRU 12, OPT 9.
  • Belady string 1,2,3,4,1,2,5,1,2,3,4,5 under FIFO: 9 faults with 3 frames, 10 faults with 4 frames.
  • Clock (second chance): pointer sweeps frames; reference bit 1 → clear to 0 and skip; reference bit 0 → evict. LRU approximations: reference bits, aging counters. Page fault rate = faults / references; hit ratio = 1 − fault rate.
7

Thrashing & Working Set

Thrashing occurs when a process (or the whole system) spends more time paging than executing because it does not have enough frames for the pages it is actively using. The working set WS(t, Δ) is the set of distinct pages referenced in the most recent Δ memory references; the working-set size WSS is its cardinality.

In short: The desk is too small for the papers you need right now, so you keep swapping papers with the cupboard and never finish any work.

Set = unique items only

Think about it

Thrashing explains sudden system collapse under memory pressure; GATE tests the cause, the symptom (low CPU utilisation, high paging) and the working-set computation.

Cause chain: too many processes → each gets too few frames → page-fault rate rises → processes block on disk I/O → CPU utilisation falls → the scheduler admits more processes to raise utilisation → even fewer frames → collapse.

The CPU-utilisation versus degree-of-multiprogramming curve rises, peaks, then falls sharply at the thrashing point.

Locality model: processes move from locality to locality; thrashing starts when the sum of the localities exceeds total memory.

Working-set model: choose Δ (e.

g.

10,000 references); D = Σ WSSi = total frame demand; if D > m (frames available) thrashing will occur, so suspend (swap out) a process; if D < m, another process may start.

Page-fault frequency (PFF) control: set upper and lower fault-rate bounds; if a process exceeds the upper bound give it a frame, below the lower bound take one away; if no free frame exists, swap a process out.

Other remedies: local (per-process) replacement so one process cannot steal frames from others, prefetching, larger RAM, lower multiprogramming degree, and better program locality (row-major array traversal).

Frame allocation: equal, proportional (si / S × m) or priority.

Minimum frames per process are set by the instruction set architecture (the largest number of pages one instruction can reference, including indirect addressing).

Key points

  • Locality of reference: Programs reference a small set of pages for a period (temporal and spatial locality).
  • WSS: Working-set size: number of distinct pages in the window Δ.
  • PFF: Page-fault frequency: control scheme adjusting frames to keep the fault rate in a band.
  • Degree of multiprogramming: Number of processes resident in memory.
  • Working-set example with Δ = 5: the last five references are 7 7 7 7 5 → WS = {5, 7}, WSS = 2; the last five references are 2 3 4 1 2 → WS = {1, 2, 3, 4}, WSS = 4. If two such processes run with only 5 frames in total, D = 2 + 4 = 6 > 5 → thrashing risk, so one should be suspended.
  • Proportional allocation: 62 frames, process sizes 10 and 127 pages: a1 = 10/137 × 62 ≈ 4, a2 = 127/137 × 62 ≈ 57.
  • Symptoms: low CPU utilisation, high disk (swap) activity, high page-fault rate. Adding a faster CPU does not help.

Solved examples

Example 01 · Basic

Page size is 4 KB. A page table maps page 3 → frame 9. Translate logical address 13000 (decimal) to a physical address.

Given Data:
  • Page size = 4096 bytes
  • Logical address = 13000
  • Page 3 → Frame 9
Required To Find:

Physical address

Formula Applied:—
Step-by-Step Solution:
Step 1: Page number = floor(13000 / 4096) = 3.
Step 2: Offset = 13000 - 3 × 4096 = 13000 - 12288 = 712.
Step 3: Frame for page 3 is 9, so physical address = 9 × 4096 + 712.
Step 4: 9 × 4096 = 36864; 36864 + 712 = 37576.
Final Answer: Physical address = 37576.

Approach note: The offset never changes; only the page number is replaced by the frame number.

Example 02 · Intermediate

TLB access takes 10 ns and memory access takes 100 ns. The TLB hit ratio is 80% and paging is single-level. Find the effective access time.

Given Data:
  • t = 10 ns
  • m = 100 ns
  • h = 0.8
Required To Find:

EAT

Formula Applied:—
Step-by-Step Solution:
Step 1: Hit time = 10 + 100 = 110 ns.
Step 2: Miss time = 10 + 100 (page table) + 100 (data) = 210 ns.
Step 3: EAT = 0.8 × 110 + 0.2 × 210 = 88 + 42 = 130 ns.
Final Answer: EAT = 130 ns.

Approach note: Check whether the question charges TLB search time on a miss; the standard GATE form does.

Example 03 · Advanced

Reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 with 3 frames initially empty. Count page faults for FIFO, LRU and Optimal.

Given Data:
  • 3 frames
  • 20 references
Required To Find:

Page faults per algorithm

Formula Applied:—
Step-by-Step Solution:
Step 1: FIFO (frames oldest→newest): 7 F[7]; 0 F[7,0]; 1 F[7,0,1]; 2 F[0,1,2]; 0 hit; 3 F[1,2,3]; 0 F[2,3,0]; 4 F[3,0,4]; 2 F[0,4,2]; 3 F[4,2,3]; 0 F[2,3,0]; 3 hit; 2 hit; 1 F[3,0,1]; 2 F[0,1,2]; 0 hit; 1 hit; 7 F[1,2,7]; 0 F[2,7,0]; 1 F[7,0,1]. Faults = 15.
Step 2: LRU: faults on 7,0,1,2 (4); 0 hit; 3 F (evict 1); 0 hit; 4 F (evict 2); 2 F (evict 3); 3 F (evict 0); 0 F (evict 4); 3 hit; 2 hit; 1 F (evict 0); 2 hit; 0 F (evict 3); 1 hit; 7 F (evict 2); 0 hit; 1 hit. Faults = 12.
Step 3: Optimal (evict the page whose next use is farthest): 7, 0, 1 fault; 2 faults and evicts 7 (next use is farthest); 0 hit; 3 faults and evicts 1; 0 hit; 4 faults and evicts 0 (next use at position 11, later than 2 and 3); 2 hit; 3 hit; 0 faults and evicts 4 (never used again); 3 hit; 2 hit; 1 faults and evicts 3; 2, 0, 1 hit; 7 faults and evicts 2; 0, 1 hit. Faults on 7,0,1,2,3,4,0,1,7 = 9.
Step 4: Compare: OPT 9 ≤ LRU 12 ≤ FIFO 15 for this string.
Final Answer: FIFO = 15, LRU = 12, Optimal = 9 page faults.

Approach note: For OPT look forward to the next use of each resident page; for LRU look backward. Hits do not change FIFO order but do refresh LRU recency.

Example 04 · Intermediate

A system has a 32-bit logical address, 4 KB pages and 4-byte page-table entries. Find the number of pages and the single-level page-table size.

Given Data:
  • 32-bit address
  • Page size 4 KB = 2^12
  • Entry = 4 bytes
Required To Find:

Number of pages and page-table size

Formula Applied:—
Step-by-Step Solution:
Step 1: Offset = 12 bits, so page-number bits = 32 - 12 = 20.
Step 2: Number of pages = 2^20 = 1,048,576.
Step 3: Page-table size = 2^20 × 4 bytes = 4 MB per process.
Final Answer: 2^20 pages; the page table is 4 MB per process.

Approach note: A table this large motivates multilevel paging.

Real-world applications

OS

Using Memory Management & Virtual Memory in real projects

Understanding why processes can use more memory than RAM, tuning swap, diagnosing slow systems and designing efficient data access patterns.

Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

Common mistakes

❌ Myth: Paging eliminates all fragmentation.

Why this is wrong: The last page of each process is rarely full, so wasted space remains inside frames.

✅ Verified Truth: Paging removes external fragmentation but still has internal fragmentation.

⚠️ Exam Tip: State both fragmentation types and which scheme has which.

❌ Myth: Adding more frames always reduces page faults.

Why this is wrong: FIFO can show Belady’s anomaly where more frames cause more faults.

✅ Verified Truth: LRU and OPT are stack algorithms and never suffer from it; FIFO can.

⚠️ Exam Tip: Remember the 1,2,3,4,1,2,5,1,2,3,4,5 string: 9 faults vs 10.

❌ Myth: Thrashing means the CPU is overloaded.

Why this is wrong: In thrashing the CPU is mostly idle waiting for page-fault I/O.

✅ Verified Truth: Thrashing is high paging activity from insufficient frames, with low CPU utilization.

⚠️ Exam Tip: Cure is fewer processes or working-set based allocation, not a faster CPU.

Revision notes

The OS must share main memory among processes safely and efficiently. This chapter covers contiguous allocation and fragmentation, paging with page tables and the TLB, virtual memory with demand paging, page replacement (FIFO, LRU, Optimal with Belady’s anomaly) and thrashing.

High-Yield Quick Revision Points

  1. Internal fragmentation (fixed/paging) vs external (variable partitions)
  2. Page number bits = address bits - offset bits; PA = frame × page size + offset
  3. EAT with TLB = h(t + m) + (1 - h)(t + 2m)
  4. Page fault: trap → find frame → read disk → update table → restart
  5. OPT ≤ LRU ≤ FIFO typically; only FIFO has Belady’s anomaly
  6. Thrashing: too few frames, CPU idle; fix with working set or fewer processes

Practice questions

Question 1 · MCQ (GATE)

A system uses 4 KB pages and a 32-bit logical address. How many bits are used for the page number?

Question 2 · MCQ (GATE)

Which page replacement algorithm can suffer from Belady’s anomaly?

Question 3 · MCQ (GATE)

TLB time 20 ns, memory 100 ns, hit ratio 90%, single-level paging. EAT = ?

Question 4 · MCQ (GATE)

External fragmentation occurs in:

Question 5 · MCQ (GATE)

Thrashing is characterised by:

Question 6 · MCQ (GATE)

Reference string 1,2,3,1,2,4 with 3 frames under LRU: number of page faults?