🧠 L8: Disjoint Memory Schemes

Lecture Goal

Move beyond the assumption that a process must sit in contiguous physical memory. Two mechanisms achieve this: Paging and Segmentation.

The Big Picture

In L7, processes had to occupy one big, unbroken chunk of RAM. But what if we could scatter a process’s memory across physical RAM? That’s exactly what paging does.


πŸ“¦ Part 1: Paging

1.1 The Core Idea

In contiguous allocation, a process occupies one big chunk. This causes external fragmentation β€” lots of little free holes that can’t be used together.

Paging’s solution: Let a process’s memory be scattered across physical RAM!

πŸ“„ ConceptπŸ“ Where it livesπŸ“ Fixed/Variable size
PageLogical (process) memoryFixed βœ…
FramePhysical memoryFixed βœ… (same size as page)

Hotel Analogy

Think of physical memory as a hotel with numbered rooms (frames), all the same size.

  • A process is like a tour group that needs several rooms
  • The hotel doesn’t need to give them adjacent rooms β€” any free rooms will do
  • The page table is the receptionist’s ledger mapping each group member (page) to their assigned room (frame)
Logical View (Process):          Physical Reality (RAM):
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β” Frame 0
β”‚Page 0β”‚Page 1β”‚Page 2β”‚    ──►    β”‚Frame 3β”‚ (Page 0) ← scattered!
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜           β”œβ”€β”€β”€β”€β”€β”€β”€ Frame 1
                                 β”‚Frame 7β”‚ (Page 2) ← not contiguous!
                                 β”œβ”€β”€β”€β”€β”€β”€β”€ Frame 2
                                 β”‚Frame 1β”‚ (Page 1)
                                 β””β”€β”€β”€β”€β”€β”€β”˜

1.2 Logical Address Translation

Because page size and frame size are both powers of 2, the address splits cleanly with bit-slicing β€” no division needed!

flowchart TD
    subgraph Translation ["Translation Process"]
        P[Page Number p]
        D[Offset d]
        PT[Page Table]
        F[Frame Number f]
    end

    LA[Logical Address] --> P
    LA --> D
    P --> PT
    PT --> F
    F --> PA[Physical Address]
    D --> PA

Why Power of 2?

The offset is just the lower bits of the address, and the page number is the upper bits. No arithmetic β€” just bit masking!

Example Walkthrough:

Given: 4-bit logical address, page size = 4 bytes
β†’ n = 2 (offset bits), m = 4 (total bits)

Logical address: 0b1001
β”œβ”€β”€ Page number: 0b10 = 2 (upper 2 bits)
└── Offset:      0b01 = 1 (lower 2 bits)

Page table says: Page 2 β†’ Frame 1

Physical address = Frame Γ— Page size + Offset
                 = 1 Γ— 4 + 1
                 = 5 β†’ 0b00101

1.3 Fragmentation: The Good News

🧩 TypeπŸ“ˆ Paging Behaviour
External fragmentationβœ… Eliminated! Every free frame is usable
Internal fragmentation⚠️ Still possible β€” last page may not be full

Internal Fragmentation Example

Process needs 10,050 bytes. Page size = 4 KB.

  • Pages needed: ⌈10050 / 4096βŒ‰ = 3 pages = 12,288 bytes
  • Wasted: 12,288 - 10,050 = 2,238 bytes (internal fragmentation)

Key insight: External fragmentation is solved by paging. We only have to worry about internal fragmentation, which is much smaller and more predictable.


1.4 Hardware Support: The TLB

NaΓ―ve paging requires two memory accesses per reference:

  1. Access page table to get frame number
  2. Access the actual data

This doubles memory access time! Enter the Translation Look-Aside Buffer (TLB) β€” a small, ultra-fast on-chip cache.

flowchart LR
    CPU[CPU Page #] --> TLB{TLB Hit?}
    TLB -- "Yes (~99%)" --> Frame[Get Frame #]
    TLB -- "No (~1%)" --> PT[Page Table in RAM]
    PT --> Frame
    PT --> Update[Update TLB]
    Frame --> Access[Access Physical Memory]

Average Access Time Formula

With 99% hit rate, TLB = 2ns, MEM = 100ns:

Compare to 200ns without TLB β€” nearly 2Γ— faster!


1.5 Protection & Sharing

The page table does more than just translation. Each entry has metadata bits:

πŸ”’ BitπŸ“ Purpose
Valid/InvalidIs this page in the process’s address space?
Read/Write/ExecuteAccess rights for this page
DirtyHas this page been modified?
ReferencedHas this page been accessed recently?

Copy-On-Write (COW)

When fork() creates a child process, parent and child initially share the same physical pages. Only when one writes to a page does the OS make a copy. This saves memory and makes fork() much faster!


πŸ“ Part 2: Segmentation

2.1 Motivation

Paging treats memory as one flat space. But programs have distinct regions with different needs:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” High addresses
β”‚     Stack       β”‚ ← Needs to grow, limited access
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     Heap        β”‚ ← Needs to grow, read/write
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     Data        β”‚ ← Fixed size, read/write
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     Code        β”‚ ← Fixed size, read-only, executable
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Low addresses

Building Analogy

A process is like a building. It has different floors for different purposes (lobby, offices, storage). Segmentation treats each floor as an independently managed unit β€” each can expand or contract without affecting others.


2.2 Basic Idea

The logical address space is divided into named segments of variable size.

flowchart TD
    CPU["Logical Address (S, D)"] --> ST[Segment Table]
    ST --> Base[Base Address]
    ST --> Limit[Size Limit]
    D[Offset] --> Check{"D < Limit?"}
    Check -- Yes --> Add["Base + D"]
    Check -- No --> Error["Segmentation Fault"]
    Add --> PA["Physical Address"]

How it works:

  1. Logical address = (segment_number, offset)
  2. Look up segment in segment table β†’ get base and limit
  3. Check: offset < limit? If no β†’ segfault
  4. Physical address = base + offset

Segmentation Fault

This occurs when your program accesses an offset beyond the limit of a segment. The hardware catches the out-of-bounds access and traps to the OS.

Common causes: Stack overflow, null pointer dereference, array out of bounds.


2.3 Segmentation vs Paging

βš–οΈ FeatureπŸ“„ PagingπŸ“ Segmentation
Unit SizeFixed (hardware-decided)Variable (usage-based)
Ext. Fragmentation❌ None⚠️ Yes (variable sizes)
Int. Fragmentation⚠️ Yes (last page)❌ None (perfect fit)
Logical StructureFlat (no regions)Structured (matches program)
SharingPage-levelSegment-level (more natural)

Which is better?

Neither! They solve different problems:

  • Paging: Solves external fragmentation, simple allocation
  • Segmentation: Matches program structure, enables logical protection

πŸ”— Part 3: Segmentation with Paging

3.1 Best of Both Worlds

Segmentation suffers from external fragmentation. The fix: page the segments!

flowchart LR
    LA["Logical Address (S, P, D)"] --> ST[Segment Table]
    ST --> PT["Page Table for Segment S"]
    PT --> Frame["Frame F"]
    Frame --> PA["Physical Address (F, D)"]

How it works:

  1. Segment gives logical structure (Code, Data, Stack, Heap)
  2. Each segment is divided into pages
  3. Page table maps pages to frames
  4. Best of both: logical structure + no external fragmentation
Address ComponentMeaning
SWhich segment? (Code, Data, Stack, etc.)
PWhich page within that segment?
DOffset within that page

Modern Systems

Modern x86-64 uses a simplified version: paging alone. Segments exist for legacy reasons but are mostly set to base=0, limit=max. The OS manages memory almost entirely through paging.


❓ Mock Exam Questions

Q1: Address Calculation

Page size = 512 bytes, Logical address = 16 bits. How many bits for page number and offset?

Answer

Page size = 512 bytes = bytes

  • Offset bits = = 9 bits
  • Page number bits = 16 - 9 = 7 bits

Maximum pages = = 128 pages Maximum addressable memory = = 64 KB

Q2: TLB Performance

TLB lookup = 2 ns, Memory access = 100 ns, Hit rate = 80%. Calculate the average memory access time.

Answer

Note: Without TLB, every access would take 200 ns (2 memory accesses). The TLB saves ~40% time at 80% hit rate!

Q3: Segmentation Fault

A segment has base = 1000, limit = 500. Which logical addresses cause a segmentation fault?

Answer

Valid offsets: 0 to 499 (offset < limit)

  • Offset 300 β†’ Valid (300 < 500)
  • Offset 500 β†’ Invalid (500 β‰₯ 500) ← segfault!
  • Offset 1000 β†’ Invalid (1000 β‰₯ 500) ← segfault!

Remember: The check is strictly less than. offset == limit is already out of bounds.


⚠️ Common Mistakes to Watch Out For

Fragmentation Confusion

Paging eliminates external fragmentation, but internal fragmentation still exists in the last page. Segmentation is the opposite!

TLB Miss Penalty

On a TLB miss, you still spent time checking the TLB! Total time = TLB + MEM + MEM = TLB + 2 Γ— MEM

Don’t forget the TLB lookup time in your calculation!

Page vs Segment

  • Page size: Fixed, determined by hardware (e.g., 4 KB)
  • Segment size: Variable, determined by program structure

Offset Range

For a segment with limit = N, valid offsets are 0 to N-1. Address N is out of bounds!


πŸ”— Connections

πŸ“š LectureπŸ”— Connection
L7Base+Limit registers β†’ generalized to segmentation
L9Paging + disk backing β†’ Virtual Memory! Present bit enables pages to live on disk

The Key Insight

Paging solved external fragmentation by making allocation units fixed-size. Segmentation preserved logical structure by using variable-size units. Paged segmentation combines both β€” segments for logic, pages for allocation.