Introduction
Memory management in an operating system (OS) involves two critical aspects:
- Memory Sharing – Multiple processes must be able to share memory safely and efficiently to improve system performance.
- Memory Protection – The OS must ensure that one process does not interfere with another process’s memory, preventing unauthorized access and data corruption.
An OS must balance efficient memory sharing while ensuring strict memory protection to maintain system stability and security.
Memory Sharing
Memory sharing allows multiple processes to access the same memory space for efficient communication and performance. It helps in reducing redundancy and optimizing memory usage.
Types of Memory Sharing
- Shared Memory
Multiple processes share a common memory segment.
Fastest method for inter-process communication (IPC).
Used in multi-threading and client-server models.
Example:
- In a web server, multiple worker threads share memory for handling client requests efficiently.
- Virtual Memory Sharing
The OS provides shared virtual memory to processes.
Each process sees a consistent view of memory, even if it’s swapped to disk.
Example:
- Multiple instances of a browser (Chrome) share common code libraries in memory.
- File Mapping (Memory-Mapped Files)
A file is mapped to memory, allowing multiple processes to read/write it as a shared resource.
Reduces the need for explicit file I/O operations.
Example:
- Databases (e.g., MySQL, PostgreSQL) use memory-mapped files to improve access speed.
Memory Protection
While sharing memory is essential, the OS must protect memory from: Unauthorized access (security threats).
Process crashes affecting other processes.
Corruption of system memory (kernel protection).
Memory Protection Techniques
1. Base and Limit Registers
Each process gets a base register (starting memory address) and a limit register (maximum address).
The OS restricts access beyond these addresses.
Example:
- If Process A is allocated memory from 2000 to 5000, it cannot access address 6000.
2. Segmentation Protection
Memory is divided into segments (code, stack, data).
Each segment has read/write permissions.
Example:
- A code segment is marked read-only to prevent accidental modification.
3. Paging Protection
Memory is divided into fixed-size pages.
The OS controls access using page tables and protection bits.
Example:
- If a process tries to access an invalid page, it gets a segmentation fault (error).
4. Virtual Memory Protection
Each process has its own virtual memory space, preventing direct access to another process’s memory.
The OS handles memory translation using page tables.
Example:
- Process A (Notepad) cannot access Process B’s (Chrome) memory, even if both are running.
5. Access Control & Privileges
Memory permissions: Read, Write, Execute (RWX).
Kernel mode vs. User mode: Prevents normal processes from modifying system memory.
Example:
- Linux uses User & Kernel Space to prevent users from modifying OS code.
Comparison of Memory Sharing & Protection
Feature | Memory Sharing | Memory Protection |
---|---|---|
Purpose | Allows multiple processes to access common data | Prevents one process from interfering with another |
Benefit | Improves efficiency and reduces redundancy | Ensures system stability and security |
Example | Shared memory in web servers | Page protection in virtual memory |
Techniques Used | Shared memory, file mapping | Paging, segmentation, base-limit registers |
Conclusion
Memory sharing and protection are two sides of the same coin in OS memory management. While sharing improves efficiency, protection ensures system stability and security. The OS must implement proper memory access controls to allow safe and effective memory sharing without compromising security.