Post

16. Shared Memory

Shared Memory is one of the fastest IPC (Inter-Process Communication) mechanisms on UNIX-like systems, allowing multiple processes to access the same memory region.

Unlike pipes or message queues, shared memory enables direct memory access, making it ideal for high-performance communication between processes. However, because multiple processes can access it simultaneously, you usually need synchronization mechanisms (like semaphores) to avoid race conditions.

SysV Shared Memory Functions

FunctionPurpose
shmget()Creates or gets a shared memory segment.
shmat()Attaches the shared memory to the process’s address space.
shmdt()Detaches the shared memory from the process’s address space.
shmctl()Controls or removes the shared memory segment.

Basic Working Flow

  1. Generate a key using ftok().
  2. Create or access a shared memory segment with shmget().
  3. Attach to the segment with shmat().
  4. Read/write data in the shared memory.
  5. Detach with shmdt() after use.
  6. Delete the memory segment using shmctl() (if needed).

Below is a C program that demonstrates:

  • Writing to shared memory.
  • Reading from shared memory.
  • Cleaning up the segment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>

#define SHM_SIZE 1024  // 1 KB shared memory segment

int main() {
    key_t key;
    int shmid;
    char *data;

    // Generate a unique key
    key = ftok("shmfile", 65);
    if (key == -1) {
        perror("ftok");
        exit(1);
    }

    // Create shared memory segment
    shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
    if (shmid == -1) {
        perror("shmget");
        exit(1);
    }

    // Attach to the shared memory
    data = (char *)shmat(shmid, (void *)0, 0);
    if (data == (char *)(-1)) {
        perror("shmat");
        exit(1);
    }

    // Write to shared memory
    printf("Writing to shared memory...\n");
    strcpy(data, "Hello from shared memory!");

    // Simulate some delay (optional)
    sleep(2);

    // Read from shared memory
    printf("Data read from shared memory: %s\n", data);

    // Detach from shared memory
    if (shmdt(data) == -1) {
        perror("shmdt");
        exit(1);
    }

    // Destroy the shared memory segment
    if (shmctl(shmid, IPC_RMID, NULL) == -1) {
        perror("shmctl");
        exit(1);
    }

    printf("Shared memory removed.\n");
    return 0;
}

Compile & Run

1
2
3
touch shmfile  # Required for ftok
gcc -o shm_example shm_example.c
./shm_example

Summary -

  • ftok() generates a unique key.
  • shmget() creates a shared memory segment (or accesses it if it already exists).
  • shmat() attaches the segment to the process’s address space.
  • shmdt() detaches the memory.
  • shmctl() with IPC_RMID deletes the segment from the system.
  • Memory persists even after the process exits until explicitly deleted with shmctl.
This post is licensed under CC BY 4.0 by the author.