Shared Memory

POSIX Shared Memory

Shared memory is one of the fastest IPC mechanisms because it allows processes to share a region of memory. However, you must handle synchronization carefully, typically using semaphores or mutexes, since multiple processes can read and write to the same memory simultaneously.

If you don't use shared memory, it's a skill issue?

That’s what I’ve been thinking. Sure, it’s more complicated because of all this synchronization, which is bug prone, but is that it?

#include <iostream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
int main() {
    const char *shm_name = "/my_shared_memory";
    const int SIZE = 4096;
 
    // Create and configure shared memory
    int shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
    ftruncate(shm_fd, SIZE);
 
    pid_t pid = fork();
    if (pid == 0) {
        // Child process: Read from shared memory
        char *shared_mem = (char *)mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
        std::cout << "Child read: " << shared_mem << std::endl;
        munmap(shared_mem, SIZE);
    } else {
        // Parent process: Write to shared memory
        char *shared_mem = (char *)mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
        const char *message = "Hello from parent!";
        sprintf(shared_mem, "%s", message);
        munmap(shared_mem, SIZE);
        wait(NULL);  // Wait for child to finish
    }
 
    shm_unlink(shm_name);  // Remove shared memory
    return 0;
}