- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
Shared memory in Linux allows multiple processes to access a common memory segment, enabling efficient inter-process communication. Below are two common methods to implement shared memory in C.
1. Using mmap
The mmap function is a modern and flexible way to create shared memory.
Steps:
Use mmap to allocate a shared memory buffer.
Set the MAP_SHARED flag to allow sharing between processes.
Use fork() to create child processes that can access the shared memory.
Example Code:
#include <stdio.h>#include <stdlib.h>#include <sys/mman.h>#include <string.h>#include <unistd.h>void* create_shared_memory(size_t size) {int protection = PROT_READ | PROT_WRITE;int visibility = MAP_SHARED | MAP_ANONYMOUS;return mmap(NULL, size, protection, visibility, -1, 0);}int main() {char parent_message[] = "Hello from parent";char child_message[] = "Hello from child";void* shmem = create_shared_memory(128);memcpy(shmem, parent_message, sizeof(parent_message));if (fork() == 0) { // Child processprintf("Child read: %s\n", (char*)shmem);memcpy(shmem, child_message, sizeof(child_message));printf("Child wrote: %s\n", (char*)shmem);} else { // Parent processsleep(1); // Wait for child to writeprintf("Parent read: %s\n", (char*)shmem);}return 0;}Copied!✕Copy Shared Memory in Linux: A Comprehensive Guide - linuxvox.com
Nov 14, 2025 · Shared memory is one of the most powerful IPC mechanisms available in Linux. It allows multiple processes to access the same physical memory region, enabling fast data exchange and …
How to use shared memory with Linux in C - Stack …
Mar 3, 2014 · There's a complete example in the Rationale section of the …
- Reviews: 3
Code sample
char parent_message[] = "hello";char child_message[] = "goodbye";void* shmem = create_shared_memory(128);memcpy(shmem, parent_message, sizeof(parent_message));int pid = fork();...Using Shared Memory in Linux - DEV Community
Sep 30, 2023 · We will explore how shared memory works, discuss relevant functions, provide a code example, and highlight the benefits of using shared …
IPC through shared memory - GeeksforGeeks
Jan 10, 2025 · The Shared memory is a memory segment that multiple processes can access concurrently. It is one of the fastest IPC methods because the …
Linux Shared Memory Tutorial: Process Communication …
Dec 10, 2024 · As a fundamental part of Linux interprocess communication (IPC), shared memory lets different processes exchange data faster than any other IPC …
- Watch full videoWatch full video
POSIX Shared Memory in Linux - SoftPrayog
Oct 3, 2018 · In UNIX-like systems, shared memory, semaphores and message queues are designated as the three IPC mechanisms. For each mechanism, there …
GitHub - jandriea/simple-shmem: Simple Shared Memory Example
This repository provides a simple example of how to use shared memory for interprocess communication in a Unix-like environment.
shm_overview (7) — Linux manual page - man7.org
Accessing shared memory objects via the filesystem On Linux, shared memory objects are created in a (tmpfs(5)) virtual filesystem, normally mounted under /dev/shm. Since Linux 2.6.19, Linux supports …
Basic example shared memory reader and writer — C Pointers
There are many functions used in shared memory . We can classify those functions based on functionalities. shm_open is used to opens a shared memory object. For example, mmap is used to …
POSIX shared-memory API - GeeksforGeeks
Oct 31, 2023 · Several IPC mechanisms are available for POSIX systems, including shared memory and message passing. Here, we explore the POSIX API for shared memory. POSIX shared memory is …