Memory Management in Operating Systems: Fragmentation, Paging, Segmentation, and Virtual Memory
Understanding Memory Fragmentation
External Fragmentation
External fragmentation occurs when free memory is divided into small, non-contiguous blocks, making it difficult to allocate larger blocks for processes.
Example:
| Allocated | Free | Allocated | Free | Allocated | Free | Allocated |
In this example, despite having enough total free memory, allocating a large block is impossible due to the scattered free spaces.
Internal Fragmentation
Internal fragmentation occurs when allocated memory blocks are larger than necessary, wasting space within the allocated block.
Example:
| Process A (50 KB) | Process B (30 KB) | Process C (20 KB) |
Here, each process has some wasted space within its allocated block.
Paging for Efficient Memory Management
Paging divides logical and physical memory into fixed-size blocks called pages and frames, respectively. The operating system uses a page table to map logical pages to physical frames.
Paging Calculations:
Given:
- Logical Address Space (LAS) = 128KB
- Physical Address Space (PAS) = 512KB
- Page size = 16KB
Calculations:
- Number of bits for Logical Address (LA) = 17 bits
- Number of bits for Physical Address (PA) = 19 bits
- Number of Pages in LAS = 8 pages
- Number of Frames in PAS = 32 frames
- Page Table Size = 40 bits
Segmentation for Logical Memory Division
Segmentation divides logical memory into variable-size segments based on the program’s structure (e.g., code, data, stack).
Segmentation vs. Paging:
- **Unit of Division:** Paging uses fixed-size pages, while segmentation uses variable-size segments.
- **Address Translation:** Paging uses a page table, while segmentation uses a segment table.
- **Fragmentation:** Paging may suffer from internal fragmentation, while segmentation may suffer from external fragmentation.
Virtual Memory: Expanding Memory Capabilities
Virtual memory creates an illusion of having more memory than physically available by swapping data between RAM and disk storage.
Benefits of Virtual Memory:
- Efficient memory utilization
- Execution of larger programs
- Memory protection and isolation
Logical vs. Physical Address Space
Logical address space is the address space seen by a process, while physical address space is the actual memory available in the hardware.
Key Differences:
- **Perspective:** Logical address space is from the process’s viewpoint, while physical address space is from the hardware’s viewpoint.
- **Visibility:** Each process has its own logical address space, while all processes share the same physical address space.
- **Size:** Logical address space can be larger than physical address space.
Page Replacement Algorithms
Page replacement algorithms determine which page to replace when a page fault occurs.
Common Algorithms:
- **LRU (Least Recently Used):** Replaces the least recently used page.
- **FIFO (First-In-First-Out):** Replaces the oldest page.
- **Optimal:** Replaces the page that will not be used for the longest period (theoretical).
Thrashing: Excessive Paging
Thrashing occurs when the system spends more time swapping pages than executing instructions, leading to performance degradation.
Causes of Thrashing:
- High degree of multiprogramming
- Insufficient memory allocation
- Poor memory management
Demand Paging: On-Demand Loading
Demand paging loads pages into memory only when needed, improving memory utilization and reducing startup time.
Fixed vs. Dynamic Partitioning
Fixed partitioning divides memory into fixed-size partitions, while dynamic partitioning divides memory into variable-size partitions.
Key Differences:
- **Partition Size:** Fixed partitions have predetermined sizes, while dynamic partitions adjust sizes based on process needs.
- **Fragmentation:** Fixed partitioning may lead to external fragmentation, while dynamic partitioning reduces internal fragmentation.
The Role of the Operating System in Security
The operating system plays a crucial role in ensuring system security through various mechanisms:
- User authentication and access control
- Process isolation and privilege separation
- File system security
- Network security
- Malware detection and prevention
- System resource protection
