Inter-Process Communication in Operating Systems: Shared Memory

Inter-Process Communication in Operating Systems: Shared Memory (Shared Memory)

Shared memory is one of the most efficient forms of Inter-Process Communication (IPC), as it allows two or more processes to directly access the same region of physical memory, avoiding multiple copies of data between kernel space and user space.

1. Core Concepts

  • Definition: Multiple processes map the same segment of physical memory into their respective address spaces, enabling direct read and write access to this memory.
  • Characteristics:
    • High Speed: Communication does not require system calls or data copying.
    • Unstructured: The memory region is merely a raw byte array; processes must negotiate the data format (e.g., structures, queues) themselves.
    • Synchronization Issues: Concurrent reads and writes by multiple processes can cause data races, necessitating the use of synchronization mechanisms (e.g., semaphores, mutex locks).

2. Detailed Implementation Steps (Using the POSIX Standard as an Example)

  • Step 1: Create/Open the Shared Memory Region

    • Use the shm_open() function to create a named shared memory object, which returns a file descriptor.
    • Example: fd = shm_open("/my_shm", O_CREAT | O_RDWR, 0666);
    • Purpose: Creates a shared object in the virtual filesystem (e.g., /dev/shm) that other processes can access by name.
  • Step 2: Set the Shared Memory Size

    • Use the ftruncate() function to adjust the size of the shared memory object.
    • Example: ftruncate(fd, SIZE);
    • Principle: The initial size of shared memory is 0; it must be explicitly expanded to the required size.
  • Step 3: Memory Mapping

    • Use the mmap() function to map the shared memory into the process's address space.
    • Example: ptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    • Parameter Explanation:
      • MAP_SHARED: Ensures modifications to the mapping are visible to other processes.
      • PROT_READ/PROT_WRITE: Sets read and write permissions.
    • Result: The process can directly access the shared memory via the pointer ptr.
  • Step 4: Read/Write Operations

    • Processes perform memory reads and writes using the pointer, e.g., memcpy(ptr, data, size).
    • Note: Writes are immediately visible to other processes, but race conditions require additional synchronization.
  • Step 5: Synchronization Mechanism (Critical Supplement)

    • Example: Use semaphores to control access order.
    • The producer process writes data and then executes sem_post() to notify the consumer.
    • The consumer process waits for data readiness via sem_wait().
  • Step 6: Cleanup Resources

    • Use munmap() to unmap the memory.
    • Use shm_unlink() to delete the shared object and release physical resources.

3. Underlying Principles

  • Page Table Mapping: Page table entries of different processes point to the same physical page frame; modifications are written directly to physical memory.
  • Avoiding Copy-on-Write (COW): Specify the MAP_SHARED flag during mapping to ensure writes are not copied to independent pages.

4. Advantages and Disadvantages Analysis

  • Advantages:
    • Extremely high performance: Eliminates kernel buffer overhead and multiple copy operations.
    • Suitable for large-scale data exchange (e.g., image processing, scientific computing).
  • Disadvantages:
    • Requires manual synchronization, increasing programming complexity.
    • Low security: Malicious processes can corrupt shared data.

5. Application Scenarios

  • Database buffer pool management (e.g., Oracle SGA).
  • High-frequency real-time data processing systems.
  • Multi-process collaborative frame buffer processing in graphics rendering.

By combining synchronization tools (e.g., semaphores), shared memory can achieve high-speed communication while ensuring data consistency, making it the preferred IPC solution for performance-sensitive scenarios.