Operating System Memory Management: Single vs. Multiuser Techniques
Memory Management Fundamentals
Memory management is a function of an operating system (OS) that handles the allocation and deallocation of memory to programs and processes.
Single User Operating Systems (OS)
In a Single User OS, only one user can use the system at a time. Consequently, memory is managed in a simple way.
Key Features of Single User OS Memory
- Single Program in Memory: Only one program is loaded in memory at a time. No multitasking is supported.
- Contiguous Memory Allocation: The program is loaded into a single, continuous block of memory. This approach is simple and easy to manage.
- Manual Allocation: The programmer or user sometimes has to decide memory usage manually. There is no need for complex allocation algorithms.
- Less Fragmentation: Since only one program runs, fragmentation is low. There is no need for complex memory techniques like paging or segmentation.
- Low Resource Usage: Less RAM and CPU are needed. Suitable for small devices or older systems (like MS-DOS).
Advantages and Disadvantages (Single User OS)
Advantages
- Simple and easy to use.
- Less overhead on the system.
- Good for basic computing (typing, printing, small calculations).
Disadvantages
- No multitasking capability.
- Wastes memory if the program does not use all allocated space.
- Not suitable for modern systems and applications.
Multiuser Operating Systems (OS)
A Multiuser OS allows more than one user to use the computer system simultaneously. This requires advanced memory management to ensure each user receives sufficient memory without affecting others.
Why Memory Management is Crucial in Multiuser OS
Since multiple users run different programs concurrently, the OS must divide memory efficiently and safely. No user’s program should interfere with another’s memory area.
Main Memory Management Techniques
- Partitioning: Memory is divided into fixed or variable parts. Each user or program gets one partition.
- Paging: Memory is broken into small, equal blocks called pages. Pages are stored in non-contiguous memory locations, which avoids external fragmentation.
- Segmentation: Divides memory into logical sections (segments) like code, data, and stack. This is useful for user-based memory protection and program structure.
- Virtual Memory: Enables users to run programs larger than physical memory. It uses part of the hard disk as temporary memory (called swap space).
- Memory Protection: Ensures that one user’s data is not accessed by others. This often uses hardware mechanisms like base and limit registers or page tables.
Advantages and Disadvantages (Multiuser OS)
Advantages
- Supports multiple users simultaneously.
- Efficient memory utilization.
- Protects data between users.
- Allows running large programs.
Disadvantages
- More complex to manage.
- Slower than single-user OS due to system overhead.
- Needs more hardware resources (RAM, CPU).
Partitioning: Fixed and Variable
Partitioning is a memory management technique where main memory (RAM) is divided into parts (called partitions). Each partition holds one process or program, helping the OS manage multiple processes efficiently.
1. Fixed Partitioning
Memory is divided into fixed-size blocks at system startup. The size and number of partitions are decided in advance, and each partition holds only one process.
Example of Fixed Partitioning
If RAM is 512 MB, and it is divided into 4 fixed partitions of 128 MB, each program can use only 128 MB, regardless of its actual memory requirement.
Pros and Cons of Fixed Partitioning
- Pros: Simple to implement; easy to manage.
- Cons: Causes internal fragmentation (wasted memory inside the partition); not flexible for programs with varying sizes.
2. Variable Partitioning
Memory is divided dynamically, based on the size of the process. There are no fixed partition sizes.
Example of Variable Partitioning
If a program needs 100 MB, the OS allocates exactly 100 MB of space.
Pros and Cons of Variable Partitioning
- Pros: Less memory wastage (no internal fragmentation); more efficient use of memory.
- Cons: May cause external fragmentation (small free spaces scattered in memory); requires memory compaction (rearranging memory).
Fragmentation in Partitioning
- Internal Fragmentation: Unused space inside a fixed partition.
- External Fragmentation: Free space outside partitions (between processes).
Memory Compaction
A technique used to combine scattered free memory into one large block by shifting processes together. This helps reduce external fragmentation.
Swapping Mechanism in OS
Swapping is a memory management technique where a process is temporarily moved out of main memory (RAM) to a storage device (usually the hard disk) and brought back later for execution. It allows the OS to handle more processes than the available physical RAM.
How Swapping Works
- A process is loaded into RAM and starts running.
- When memory is full and a new process needs space, the OS swaps out an existing process from RAM to secondary storage (called swap space).
- The new process is loaded into the now free RAM space.
- Later, the swapped-out process is swapped in (brought back into RAM) when it is needed again.
This back-and-forth movement is known as swapping.
Swapping Example
Suppose RAM has three processes: P1, P2, P3. A new process P4 arrives, but memory is full. The OS swaps P2 to disk and loads P4 in that space. Later, when P2 is needed, P4 may be swapped out, and P2 is brought back.
Advantages and Disadvantages of Swapping
Advantages
- Allows more processes to run concurrently.
- Increases CPU utilization.
- Improves multitasking capabilities.
Disadvantages
- Slows down the system if swapping happens too often (a condition called thrashing).
- Increases read/write operations on the disk.
- Wastes time if the disk access speed is slow.
Contiguous Memory Allocation
Contiguous Memory Allocation is a technique where a process is stored in one single, continuous block of memory. All instructions and data of the process are kept together in adjacent (side-by-side) memory locations.
Mechanism of Contiguous Allocation
When a program needs memory, the OS finds a large enough empty block of RAM. This block must be continuous, without any gaps. The entire program is loaded into that single block.
Example of Contiguous Allocation
Suppose a program needs 100 KB of memory. The OS finds a free block, for instance, from address 4000 to 4099 (100 KB). The entire program is stored in that block without being broken up.
Types of Contiguous Allocation
- Single Contiguous Allocation: Only one user program is loaded into memory; the rest of the memory is used by the OS. Used primarily in single-user operating systems like MS-DOS.
- Multiple Contiguous Allocation: Memory is divided into multiple partitions, and each partition holds one process. Used in multiprogramming systems.
Advantages and Disadvantages
Advantages
- Simple to understand and implement.
- Fast access because memory is in one block.
- Easy to calculate memory addresses.
Disadvantages
- External Fragmentation: Small free spaces between blocks cannot be used efficiently.
- Difficult to allocate memory for large programs if a sufficiently large continuous block is unavailable.
- Wastes memory if the program does not use the whole allocated block.
Non-Contiguous Memory Allocation
1. Paging
Paging is a technique where logical memory (the program) is divided into fixed-size blocks called pages, and physical memory (RAM) is divided into frames of the same size.
How Paging Works
Pages of the process are loaded into available frames in RAM (which are not necessarily continuous). A page table keeps track of which page is located in which frame.
Paging Example
If page size and frame size are both 1 KB, a program of 4 KB will have 4 pages. These 4 pages can be stored in any 4 free frames in RAM.
Paging Pros and Cons
- Advantages: Removes external fragmentation; better memory utilization; fast memory allocation.
- Disadvantages: Internal fragmentation is possible (the last page may have unused space); slightly complex due to the use of page tables.
2. Segmentation
Segmentation divides a program into variable-size logical parts called segments (e.g., code, data, stack).
How Segmentation Works
Each segment has a name and length. A segment table stores the base address and length of each segment. Segments are loaded into non-contiguous areas of RAM.
Segmentation Example
A program might have a Code segment (2 KB), a Data segment (3 KB), and a Stack segment (1 KB). These three segments are stored in three different memory locations.
Segmentation Pros and Cons
- Advantages: Provides a logical view of the program structure; easy to manage modules (code, data, stack); no internal fragmentation.
- Disadvantages: External fragmentation is possible; more complex than paging.
