Skip to content

Memory Sharing and Protection

Introduction

Memory management in an operating system (OS) involves two critical aspects:

  1. Memory Sharing – Multiple processes must be able to share memory safely and efficiently to improve system performance.
  2. 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

  1. 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.

  1. 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.

  1. 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

FeatureMemory SharingMemory Protection
PurposeAllows multiple processes to access common dataPrevents one process from interfering with another
BenefitImproves efficiency and reduces redundancyEnsures system stability and security
ExampleShared memory in web serversPage protection in virtual memory
Techniques UsedShared memory, file mappingPaging, 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.