Comprehensive Study Notes on Main Memory and Virtual Memory Systems
Fundamentals of Main Memory Management
To share the CPU effectively among a set of processes, multiple processes must reside simultaneously in main memory. Operating systems employ memory management schemes to ensure safe and efficient sharing of main memory space across active processes.
Hardware Space Isolation
In modern computer hardware, each executing process is assigned a completely separate memory space. This isolation prevents processes from interfering with one another's memory allocations or corrupted data states.

Memory Management Unit (MMU) & Address Spaces
Memory management requires distinguishing between the address space generated by program code and the actual physical RAM location.
Logical vs. Physical Addresses
- Logical (Virtual) Address: An address generated directly by the CPU during instruction execution. The user program operates entirely within logical address spaces ranging from to .
- Physical Address: The hardware location loaded into the memory-address register (MAR) and recognized by the physical memory unit.
- Memory Management Unit (MMU): A hardware device responsible for executing run-time mapping from logical addresses to physical addresses.

Base Register Address Relocation
Under basic relocation hardware schemes, a Base Register () holds the physical starting address of a process's allocated space in RAM:
- Logical address range generated by CPU:
- Physical address mapped by MMU:
- Mathematical Mapping Equation:

Contiguous Memory Allocation
In contiguous memory allocation, each process is held in a single continuous section of physical memory adjacent to the section containing the next process. Physical memory is divided into fixed or variable-sized partitions, where each partition holds exactly one process.
Unused blocks of available memory are referred to as holes. The operating system maintains an allocation table recording which portions of memory are occupied and which remain free.
Dynamic Storage Allocation Strategies
When a process request of size arrives, the operating system selects an available hole from the free list using one of three primary strategies:
- First-Fit: Allocates the first hole in memory that is large enough to satisfy request . Search stops as soon as a qualifying hole is found.
- Best-Fit: Allocates the smallest hole that is large enough to satisfy request . Requires searching the entire list unless it is ordered by size. Produces the smallest leftover hole.
- Worst-Fit: Allocates the largest available hole in memory. Requires searching the entire list unless sorted by size. Produces the largest leftover hole.
Allocation Example & Walkthrough
Consider a physical memory layout containing processes and unallocated holes:
- Operating System occupies high memory ().
- Active process blocks reside at physical bounds such as , , , and .
- Available free holes exist at specific intervals: Hole 1 () and Hole 2 ().
When Process 7 arrives requesting allocation:
- First-Fit inspects holes sequentially from low to high memory and assigns Process 7 to Hole 1 ().
- Best-Fit evaluates both holes and assigns Process 7 to Hole 1 () to minimize leftover space.
- Worst-Fit evaluates both holes and assigns Process 7 to Hole 2 () to maximize remaining hole size.
Memory Fragmentation Problems
Contiguous memory allocation suffers from two forms of memory degradation:
- External Fragmentation: Occurs when total memory space exists to satisfy a process allocation request, but the available space is split across non-contiguous holes.

- Internal Fragmentation: Occurs when physical memory is partitioned into fixed-size blocks. Memory allocated to a process is slightly larger than requested, leaving unallocated, wasted space internal to the partition.

Non-Contiguous Allocation: Paging
Paging is a memory management scheme that allows a process's physical address space to be non-contiguous, eliminating external fragmentation.
Frame, Page, and Page Table Structures
- Frames: Fixed-sized blocks into which physical memory is divided.
- Pages: Fixed-sized blocks into which logical memory is divided. Pages are identical in size to physical frames, with sizes represented as powers of 2.
- Page Table: A kernel data structure mapping logical process pages to physical memory frames.

Logical Address Structure and Translation
An address generated by the CPU is divided into two distinct components:
- Page Number (): Used as an index into the process page table to locate the corresponding frame base address.
- Page Offset (): The specific byte displacement within the target page or frame.
Address Translation Process
- CPU generates a logical address containing .
- MMU extracts page number and indexes into the page table.
- The page table returns physical frame number stored at index
- Page number is replaced by frame number .
- Offset remains unchanged.
- The final physical address is constructed as .
Paging Calculations & Examples
Given a logical address format with high-order page bits and low-order offset bits:
Calculation Problem 1
If offset bits and page number bits :
- Page Size:
- Addressable Pages:
- Total Logical Memory Size:
Calculation Problem 2
Given a logical address space of addresses and a page size of addresses:
- Low-order offset bits ():
- Number of pages:
- High-order page bits ():
Virtual Memory Management
Virtual memory separates logical memory as perceived by software developers from physical storage memory, enabling programs to execute even if their size exceeds physical RAM.

Demand Paging Mechanics
Demand paging loads pages into main memory only as they are referenced during execution.
Each page table entry includes a Valid-Invalid Bit:
- Valid (): Indicates the associated page is currently loaded in physical RAM.
- Invalid (): Indicates the page either is out of the process's logical address space or resides strictly on secondary storage (backing store).
Page Fault Execution Sequence
Attempting to access a page marked invalid () triggers a hardware trap called a Page Fault.

- Memory reference instruction is issued by the CPU.
- Page table lookup detects valid-invalid bit set to , generating a trap signal to the operating system.
- OS checks internal process tables to confirm whether the access reference was legal.
- OS locates the missing page on the backing store disk.
- OS selects a free frame from the system Free-Frame List.
- OS schedules a secondary storage read operation to copy the page into the allocated free frame.
- Page table entry is updated: frame number set to allocated location, bit modified to valid ().
- OS resumes and restarts the instruction that caused the original fault.
Page Replacement Algorithms
When a page fault occurs and no frames remain on the free-frame list, page replacement is performed to evict an existing page from physical RAM to make room for the incoming page.
First-In-First-Out (FIFO) Algorithm
Tracks the time each page was brought into main memory. Replaces the oldest loaded page.
- Drawback: May evict heavily used pages or early initialized variables required throughout program lifetime.
- Trace Example: Reference string
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1allocated across 3 physical frames produces 15 total page faults.

Optimal Page Replacement Algorithm
Replaces the page that will not be accessed for the longest duration in the future.
- Advantage: Guarantees the lowest theoretical page fault rate for a given frame allocation.
- Drawback: Cannot be implemented in practice, as it requires future knowledge of reference strings.
Least Recently Used (LRU) Algorithm
Replaces the page that has not been accessed for the longest duration in past execution history.
- Approximates Optimal replacement by relying on temporal locality of reference.
Frequency-Based Algorithms (LFU and MFU)
Maintains a counter tracking the number of references made to each active page:
- Least Frequently Used (LFU): Replaces the page with the lowest reference count.
- Most Frequently Used (MFU): Replaces the page with the highest reference count (assuming pages with low counts were recently loaded and need time to execute).
Frequency Algorithm Tracing Example
Consider reference string sequence: 0, 4, 6, 4, 4, 6, 4, 9
Prior to processing request 9, page access frequency totals are:
- Page
0: 1 access - Page
4: 4 accesses - Page
6: 2 accesses
Replacement behavior upon encountering page 9:
- LFU Strategy: Evicts Page
0(smallest access count = 1). - MFU Strategy: Evicts Page
4(highest access count = 4).
Translation Look-Aside Buffer (TLB)
The Translation Look-aside Buffer (TLB) is a fast hardware associative cache built into the MMU that stores recent logical-to-physical address mappings.

Hardware Features
- Contains associative key-value entries: Key = Page Number (), Value = Frame Number ().
- Size is limited (typically between 32 and 1024 entries) to ensure single-cycle hardware lookup speed.
- Managed using replacement algorithms similar to page replacement policies when full.
Lookup Operations
- TLB Hit: Page number is found in the TLB. Frame number is retrieved immediately, allowing direct construction of physical address .
- TLB Miss: Page number is not present in the TLB.
- MMU performs a lookup in the main memory page table.
- Page Table Hit: Frame number is extracted from the page table entry, and mapping is written into the TLB.
- Page Fault: Entry in page table is marked invalid (), triggering a trap to the OS page fault handler.
Comprehensive System Calculations & Simulation
Quantitative Problem Analysis
Given system parameters:
- Physical Memory Size: 32 addresses (
- Virtual Memory Size: 64 addresses (
- Offset Bits: 2 bits
Solutions:
- Virtual Address Bit Length:
- Single Page/Frame Size:
- Page Number Allocation Bits:
- Total Physical Frames:
- Total Virtual Pages:
ParaCache Simulation Summary
Simulation Configuration Parameters:
- Physical Page Size: 32 ( total addresses across 8 frames)
- Offset Bits: 2 (yielding 4 addresses per page)
- Virtual Memory Size: 64 ( total addresses across 16 pages)
- TLB Entries: 5

Instruction Trace Execution (LOAD 6 over address sequence 7, 8, 9, 10):
- Instruction
0: Miss - Instruction
1: TLB Hit - Instruction
2: TLB Hit - Instruction
15: Miss - Instruction
4: Miss - Instruction
3: TLB Hit
Simulated Performance Metrics:
- Hit Rate:
- Miss Rate: