Memory Management: Address Binding, Logical vs Physical Address Space & Swapping
Overview: With base register 4,000 and logical offset 120, relocation produces physical address 4,120 if the offset is within the allowed range.
Introduction to Memory Management
- In a computer system, the CPU can only fetch and execute instructions directly from Main Memory (RAM) and CPU registers—it cannot execute…
- Keeping track of which parts of RAM are currently in use and by which process.
- Deciding which processes (or parts of processes) to move into and out of RAM.
- Allocating RAM space when processes request it and deallocating (freeing) it when processes terminate.
- Protecting one process’s memory space from being accessed or corrupted by another process (Base and Limit Registers).
Address Binding (Compile Time, Load Time & Execution Time)
- When you write a C or Java program, you refer to variables using symbolic names like
int count = 10;ortotalMarks. - 1. Compile-Time Binding (Static Binding): When it happens: If it is known at compile time exactly where the process will reside in RAM (e.g., starting at physical address
2000),… - Drawback: If the starting location in RAM ever changes later, the entire program must be recompiled from scratch!
- 2. Load-Time Binding: When it happens: If it is not known at compile time where in RAM the process will reside, the compiler generates Relocatable Code…
- Final address binding is delayed until Load Time, when the OS Loader picks a free starting address in RAM (say
5000) and adds… - Drawback: Once loaded into RAM, the process cannot be moved to a different location in RAM during execution.
- 3. Execution-Time (Runtime / Dynamic) Binding: When it happens: If a process can be moved from one memory segment in RAM to another during its execution (for example, during…
- How it works: Special hardware inside the CPU called the MMU (Memory Management Unit) dynamically adds the value of a **Relocation…
View diagram · Figure 4.1: The Three Stages of Address Binding in an Operating System
Logical vs Physical Address Space & Role of MMU
- This is one of the most important 5-mark questions in Unit 4 of DAVV BCA exams!
- A. Logical Address (Virtual Address): An address generated by the CPU while a program is running (e.g., byte
346of the current program). - Logical Address Space: The set of all logical addresses generated by a program (typically ranges from
0toMAX). - B. Physical Address (Real Address): The actual hardware address seen by the Main Memory (RAM) unit (the address loaded into the Memory Address Register
MARof RAM, e.g.,… - Physical Address Space: The set of all physical addresses corresponding to the logical addresses.
- C. Role of the MMU (Memory Management Unit): Who translates the CPU’s Logical Address into RAM’s Physical Address on the fly?
- Formula: Physical Address = Relocation (Base) Register Value + Logical Address
- 1. Generated By: Generated by the CPU during program execution.
View diagram · Figure 4.2: Dynamic Address Translation by the Memory Management Unit (MMU)
Swapping in Operating Systems
- What is Swapping?: Main Memory (RAM) has limited size (e.g., 8 GB).
- Swapping is managed by the Medium-Term Scheduler.
- Two Operations of Swapping: Swap Out (Roll Out): Moving a blocked or low-priority process from Main Memory (RAM) → Backing Store (Disk) to free up RAM space.
- Swap In (Roll In): Bringing a ready or higher-priority process from Backing Store (Disk) → Main Memory (RAM) so the CPU can execute it.
- Advantages & Disadvantages of Swapping: Advantage: Allows the total memory used by all running processes to exceed the physical RAM size, increasing the **Degree of…
- Disadvantage: Disk transfer time (Swap-out + Swap-in time) is slow compared to RAM speed, so frequent swapping causes high I/O overhead.
View diagram · Figure 4.3: Process Swapping Between Main Memory (RAM) and Disk Backing Store
Contiguous & Non-Contiguous Allocation, Fragmentation & Compaction
Overview: Three free holes of 4 KB each total 12 KB, yet none can fit a contiguous 8 KB request.
Contiguous vs Non-Contiguous Memory Allocation
- When a process needs to be loaded into Main Memory (RAM), the Operating System can allocate memory to it using two fundamental approaches:
View diagram · Figure 4.4: Contiguous Memory Allocation vs Non-Contiguous Memory Allocation
Contiguous Memory Allocation Techniques
- In Contiguous Memory Allocation, every process must occupy a single unbroken block of contiguous physical memory.
- A. Fixed-Size Partitioning (MFT — Multiprogramming with a Fixed Number of Tasks): Main Memory (RAM) is divided at system boot time into several fixed-size partitions (blocks) (e.g., four partitions of
100 KBeach). - Each partition can hold exactly one process.
- Problem: If a small
30 KBprocess is placed inside a100 KBfixed partition, the remaining **70 KBinside that partition is… - B. Variable / Dynamic Partitioning (MVT — Multiprogramming with a Variable Number of Tasks): Initially, all user RAM is treated as one large block of free memory called a Hole.
- When a process arrives (e.g., needing
75 KB), the OS carves out a partition of exact size75 KBfor it. - As processes enter and leave RAM over time, memory becomes a checkerboard of allocated blocks and scattered small free Holes of varying…
Dynamic Storage Allocation Algorithms (First Fit, Best Fit, Worst Fit)
- When a new process of size N arrives and there are several free Holes (empty blocks) scattered across RAM, how does the OS choose which hole…
- First Fit:
- Scans the list of free holes from the beginning and allocates the FIRST hole that is big enough (≥ N).
- Pros: Fastest algorithm because it searches as little as possible.
- Best Fit:
- Searches the entire list of free holes and allocates the SMALLEST hole that is big enough (≥ N), so that the leftover wasted hole is as…
- Cons: Slower (must scan entire list unless sorted) and leaves behind tiny, unusable leftover slivers (
1 KB,2 KB) across RAM! - Worst Fit:
Fragmentation: Internal vs External Fragmentation
- Fragmentation refers to memory space in RAM that is wasted and cannot be used efficiently to allocate processes.
- 1. Internal Fragmentation (Waste INSIDE Block) Occurs when memory is divided into Fixed-Size blocks (Fixed Partitioning / Paging)
- 2. External Fragmentation (Waste OUTSIDE Blocks) Occurs in Variable-Size Contiguous Allocation & Segmentation
- Comparison Table: Internal Fragmentation vs External Fragmentation: This table is a Practice Question in DAVV BCA exams!
- 1. Definition: Wasted memory INSIDE an allocated memory block when the block is slightly larger than the process…
- 2. Cause: Caused by Fixed-Size memory allocation (Fixed Partitioning and Paging).
- 3. Who Owns the Waste?: The wasted space is assigned to a specific partition/process.
- 4. Solution / Remedy: Use Variable-sized partitions or smaller page sizes.
View diagram · Figure 4.5: Internal Fragmentation vs External Fragmentation
Compaction (Defragmentation of RAM)
- What is Compaction?: Compaction is a technique used by the Operating System to solve External Fragmentation!
- How It Works: The OS shifts (shuffles) all currently allocated processes toward one end of Main Memory (RAM) so that all the scattered…
- Now a
60 KBor100 KBprocess can easily fit into the combined100 KBblock! - Important Requirement & Drawback of Compaction: Requires Dynamic (Execution-Time) Address Binding: Because Compaction physically moves running processes to new addresses in RAM, it is **ONLY possible if address binding is…
- High CPU Overhead: Moving megabytes of processes in RAM takes CPU time, so an alternative solution is to allow non-contiguous allocation…
View diagram · Figure 4.6: How Compaction Eliminates External Fragmentation
Paging and Segmentation in Operating Systems
Overview: Paging splits memory into fixed-size units.
Paging (Non-Contiguous Memory Management)
- To completely eliminate External Fragmentation without the heavy cost of Compaction, Operating Systems use Paging!
- 3 Core Building Blocks of Paging: Frames (In Physical RAM):
- Pages (In Logical Process Memory):
- Formula: Page Size = Frame Size (Always equal, e.g., 4 KB)
- Page Table (The Map):
How Address Translation Works in Paging
- Every Logical Address generated by the CPU is divided into two parts:
- Page Number (p): Used as an index into the Page Table to look up the corresponding Frame Number (f) in RAM.
- Page Offset / Displacement (d): Specifies the exact byte position within that page/frame.
- Role of TLB (Translation Lookaside Buffer) in Paging: Problem: Since the Page Table itself is stored in Main Memory (RAM), reading a single variable requires 2 memory accesses: (1)…
- Solution — TLB: A TLB (Translation Lookaside Buffer) is a small, ultra-fast hardware associative cache inside the MMU that caches…
- Advantages & Disadvantages of Paging: Advantages:
- 100% Zero External Fragmentation! Any free frame can be given to any process.
- Simple memory allocation and easy sharing of common code pages.
Segmentation in Operating Systems
- Why Was Segmentation Invented?: Paging divides a program blindly into fixed
4 KBchunks (like cutting a book every 50 lines, even if it cuts a function or… main()function- Subroutines / Methods
- Symbol Table
- Stack
- Global Variables / Arrays
How Address Translation Works in Segmentation
- In Segmentation, a Logical Address consists of a two-tuple:
- Formula: < Segment Number (s),\ Offset (d) >
- Segment Base: The starting physical address in RAM where that segment begins.
- Segment Limit: The length (size) of that segment!
- Step-by-Step Translation in Segmentation: The Segment Number (s) indexes into the Segment Table to fetch its Limit and Base.
- Bounds Check: The hardware checks whether the Offset d is valid: 0 ≤ d < Limit.
- If d ≥ Limit: The process is trying to access memory outside its segment!
- If d < Limit: Valid!
Comparison: Paging vs Segmentation
- 1. Basic Unit: Divides a program into Pages of Fixed Size.
- 2. Who Divides the Program?: Divided automatically by the Hardware / OS (invisible to programmer).
- 3. User’s View of Memory: Does not match the user’s logical view of a program.
- 4. Table Entry Contents: Page Table stores only the Frame Base Number (f).
- 5. Internal Fragmentation: Suffers from Internal Fragmentation (in the last page).
- 6. External Fragmentation: Zero External Fragmentation!
Virtual Memory, Demand Paging & Performance of Demand Paging
Overview: Demand paging first checks whether the requested page is resident.
What is Virtual Memory?
- Have you ever wondered how your laptop with only 8 GB of physical RAM can smoothly run a massive 60 GB video game alongside Chrome and…
- Only the currently needed pages of a program are kept in Physical RAM, while the rest of the program stays on the **Hard Disk / SSD…
- Major Benefit: Programs can be much larger than total physical RAM, and many more programs can run simultaneously (higher Degree of…
Demand Paging (How Virtual Memory is Implemented)
- Virtual Memory is most commonly implemented using Demand Paging.
- Golden Rule of Demand Paging: Never load a page into Main Memory (RAM) unless the CPU actually demands (references) it during execution!
- The Valid-Invalid Bit in the Page Table: How does the MMU hardware know which pages of a process are currently in RAM and which pages are still sitting on the **Hard…
v(Valid =1): The page is legal AND currently in Main Memory (RAM).i(Invalid =0): The page is either not valid or is currently sitting on the Hard Disk (Backing Store)!
What is a Page Fault & How is it Handled? (6 Steps)
- When the CPU tries to access a page whose Page Table entry is marked
i(Invalid — not currently in RAM), the MMU hardware triggers a trap… - Check Page Table MMU checks Page Table entry for Page X; sees bit is i (Invalid)
- Trap to OS (Page Fault) MMU generates a Page Fault Trap to the Operating System Kernel
- Locate Page on Backing Store OS verifies the reference is legal and finds Page X on the Hard Disk / SSD
- Read Page into Free RAM Frame OS finds a free Frame in RAM (or replaces an old frame) and loads Page X from Disk
- Update Page Table to Valid (v) Once disk read finishes, OS sets the Frame Number and flips bit from i → v (Valid)
- Restart Instruction CPU restarts the exact instruction that caused the Page Fault — now it succeeds!
- What is Pure Demand Paging?: In Pure Demand Paging, the OS starts executing a process with ZERO pages loaded in RAM!
View diagram · Figure 4.9: The 6 Steps of Handling a Page Fault in Demand Paging
Performance of Demand Paging (Effective Access Time — EAT)
- Your DAVV Unit 4 syllabus explicitly mentions “Performance of Demand Paging”!
- Let ma = Normal Memory Access Time of RAM (typically
100 nsto200 ns). - Let p = Page Fault Rate (0 ≤ p ≤ 1, the probability that a memory access causes a Page Fault):
- If p = 0: No page faults occur at all.
- If p = 1: Every single access is a page fault.
- If no page fault occurs (probability 1 - p), the access takes just ma.
- If a page fault occurs (probability p), the OS must read the page from the slow Hard Disk (Page Fault Service Time, typically `8 ms =…
- Formula for Effective Access Time (EAT): Formula: Effective Access Time (EAT) = (1 − p) × Memory Access Time (ma) + p × Page Fault Service Time
Thrashing (When Demand Paging Collapses)
- What is Thrashing?: If a process does not have enough RAM frames allocated to hold the active pages it is currently using (its Working Set), it…
- During Thrashing, Disk I/O goes to
100%while CPU Utilization plunges to near0%, making the computer freeze completely. - Solution to Thrashing: Reduce the Degree of Multiprogramming (suspend/swap out some processes) or give each active process enough frames using the…
Page Replacement Algorithms: FIFO, Optimal (OPT) & LRU
Overview: With frames holding A, B and C, suppose A arrived first but was used most recently.
Why Do We Need Page Replacement?
- In a Demand Paging system, when a Page Fault occurs, the OS needs to bring the requested page from the Hard Disk into a **free Frame in…
- Primary Goal: Choose the victim page that will result in the minimum number of total Page Faults!
- 1. FIFO (First-In, First-Out) Replaces the OLDEST page that was brought into RAM first (Suffers from Belady’s Anomaly)
- 2. Optimal (OPT / MIN) Replaces the page that will NOT be used for the longest period of time in the FUTURE
- 3. LRU (Least Recently Used) Replaces the page that has NOT been used for the longest period of time in the PAST
View diagram · Figure 4.10: The 3 Major Page Replacement Algorithms in Operating Systems
Common Reference String for Solved Examples
- To compare all three algorithms fairly, let us use the following Page Reference String (12 page requests) with 3 RAM Frames (F = 3):
- Formula: Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3
- Page Miss (Page Fault): Requested page is NOT in RAM → Must be loaded from Disk.
- Page Hit: Requested page is ALREADY in RAM → No page fault!
Algorithm 1: FIFO (First-In, First-Out) Page Replacement
- How It Works: The simplest page-replacement algorithm.
- Step-by-Step Solved Numerical (FIFO with 3 Frames): Reference String:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3 - 1. Page
7: 7 - 2. Page
0: 7 - 3. Page
1: 7 - 4. Page
2: 2 - 5. Page
0: 2 - 6. Page
3: 2
Algorithm 2: Optimal (OPT / MIN) Page Replacement
- How It Works: Discovered as a result of Belady’s Anomaly, the Optimal Page Replacement Algorithm looks ahead into the FUTURE of…
- Why is it important? It guarantees the lowest possible page-fault rate for a fixed number of frames and never suffers from Belady’s Anomaly.
- Drawback: Impossible to implement in a real OS because an OS cannot predict the future!
- Step-by-Step Solved Numerical (Optimal with 3 Frames): Reference String:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3 - 1–3.
7, 0, 1: 7 - 4. Page
2: 2 - 5. Page
0: 2 - 6. Page
3: 2
Algorithm 3: LRU (Least Recently Used) Page Replacement
- How It Works: Since we cannot look into the future (Optimal), LRU uses the recent PAST as an approximation of the near future!
- Golden Rule of LRU: When a page fault occurs, look BACKWARD into the past and **replace the page that has NOT been used for the longest…
- Why is LRU the most popular algorithm? It is practical, performs almost as well as Optimal, and never suffers from Belady’s Anomaly (because it is a Stack…
- Step-by-Step Solved Numerical (LRU with 3 Frames): Reference String:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3 - 1–3.
7, 0, 1: 7 - 4. Page
2: 2 - 5. Page
0: 2 - 6. Page
3: 2
Summary Comparison: FIFO vs Optimal vs LRU
- 1. Replacement Rule: Replaces the oldest loaded page in RAM.
- 2. Belady’s Anomaly?: YES (Suffers from Belady’s Anomaly).
- 3. Practicality: Easy to implement (simple queue), but poor performance.
- 4. Page Fault Rate: Highest page faults (
10in our example).