1.
Swapping (Detailed Explanation)
Swapping is a memory management technique used by operating systems to move entire
processes between main memory (RAM) and secondary storage (disk). It is used to manage
memory efficiently when the system is overloaded or when many processes are running
simultaneously.
1.1 What is Swapping?
Swapping involves:
Swapping Out: Moving a complete process from RAM to disk to free memory.
Swapping In: Bringing the process back to RAM when required.
The disk area used for this purpose is called swap space.
1.2 Why Swapping is Needed
To increase degree of multiprogramming
To free space when memory is full
To load larger processes that cannot fit entirely in RAM
To improve CPU utilization by keeping processes ready for execution
1.3 Swapping Process
1. The OS selects a process to swap out (based on scheduling/priority).
2. The process is copied to disk (swap area).
3. When needed again, the process is copied back into memory at:
o The same location (old systems)
o A different location (modern systems using relocation)
1.4 Advantages
Allows more processes to be in the system.
Reduces memory overloading.
Improves multiprogramming.
1.5 Disadvantages
Slow: Disk I/O is much slower than RAM.
Excessive swapping leads to thrashing.
Full processes must be moved → heavy overhead.
2. Virtual Memory (Detailed Explanation)
Virtual memory is a technique that allows a computer to execute programs larger than the
available physical memory and to run multiple programs efficiently.
2.1 Key Idea
It provides a large “virtual” address space that may exceed the actual RAM. This is done by
storing parts of programs temporarily on disk.
2.2 Features of Virtual Memory
Logical memory > Physical memory
Only a portion of a program is loaded into RAM at any time
Uses paging or segmentation
Provides isolation between processes
Prevents memory overflow
2.3 Demand Paging
Pages are loaded only when required.
If the required page is not in memory → Page Fault occurs.
2.4 Benefits of Virtual Memory
Large programs can execute even on small RAM.
Increases multiprogramming.
Reduces I/O since not all pages are loaded.
Provides better system stability and protection.
2.5 Costs / Drawbacks
Slow access when page faults occur.
May lead to thrashing when too many page faults happen.
Requires hardware support (MMU, TLB).
3. Page Replacement Algorithms (Detailed
Explanation)
When physical memory is full and a new page must be loaded, the OS must decide which
existing page to remove. This is done using Page Replacement Algorithms.
3.1 Objectives of Page Replacement
Reduce page faults
Improve CPU utilization
Optimize use of memory
3.2 Common Page Replacement Algorithms
1. FIFO (First-In First-Out)
Replaces the page that entered memory first.
Simple implementation using a queue.
Suffers from Belady’s anomaly (more frames → more faults).
2. Optimal (OPT) Page Replacement
Replace the page that will not be used for the longest time.
Produces the minimum possible page faults.
Not practical because future knowledge is required.
Used mainly as a theoretical benchmark.
3. LRU (Least Recently Used)
Replace the page that has not been used for the longest time.
Based on the assumption: recently used pages will be used again.
Implementations:
Counters
Stack operations
Advantage: Good performance
Disadvantage: High overhead
4. LFU (Least Frequently Used)
Replace the page that has the lowest access count.
Pages used rarely are removed.
Problems:
Expensive to maintain counters
Older pages accumulate counts unwantedly
5. MFU (Most Frequently Used)
Opposite of LFU.
Pages used the most are removed first.
Based on the idea that frequently used pages have served their purpose.
6. Second Chance / Clock Algorithm
Improvement over FIFO.
Uses a circular list (clock hand).
Each page has a reference bit:
o If 1 → second chance is given
o If 0 → page is replaced
Efficient and commonly used.
7. NRU (Not Recently Used)
Uses two bits:
Referenced (R)
Modified (M)
Pages are divided into four classes; the one with the lowest class is replaced.
4. Segmentation (Detailed Explanation)
Segmentation is a memory management technique that divides programs into logical segments,
such as:
Code
Data
Stack
Heap
Functions and modules
Each segment has a variable size depending on the program's structure.
4.1 Logical Representation
Address is a pair:
(Segment Number, Offset)
Example:
Segment 2, offset 100 → Go to segment 2’s base address + 100
4.2 Segment Table
Each entry contains:
Base Address: Starting location of the segment in physical memory
Limit: Length of the segment
Used for:
Address translation
Memory protection
4.3 Advantages of Segmentation
Matches the logical structure of programs
Supports modular programming
Enables memory protection
Allows easy sharing of code (shared libraries)
4.4 Disadvantages
Causes external fragmentation
Complicated memory allocation
Overhead of maintaining variable-size segments
Quick Comparison: Paging vs Segmentation
Feature Paging Segmentation
Size Fixed-size pages Variable-size segments
Purpose Physical division Logical division
Fragmentation Internal External
Visible to programmer No Yes
Address Page no + offset Segment no + offset