Unlock Fast Interprocess Communication: Master Linux Shared Memory IPC
This article explains Linux shared memory IPC, covering its data structures, system limits, essential API calls for creating, controlling, attaching, and detaching memory segments, conventions between parent and child processes, and provides a complete C example demonstrating interprocess communication.
Shared Memory IPC Principles
Shared memory is a dedicated memory segment with its own data structures, including permissions, size, and timestamps. It is used for large data transfers between processes, as illustrated below.
The data structure is defined as:
from /usr/include/linux/shm.h
struct shmid_ds {
struct ipc_perm shm_perm; /* operation perms */
int shm_segsz; /* size of segment (bytes) */
__kernel_time_t shm_atime; /* last attach time */
__kernel_time_t shm_dtime; /* last detach time */
__kernel_time_t shm_ctime; /* last change time */
__kernel_ipc_pid_t shm_cpid; /* pid of creator */
__kernel_ipc_pid_t shm_lpid; /* pid of last operator */
unsigned short shm_nattch; /* no. of current attaches */
unsigned short shm_unused; /* compatibility */
void *shm_unused2; /* ditto - used by DIPC */
void *shm_unused3; /* unused */
};Before using the shared memory segment, processes must attach it to their address space.
The system imposes the following limits:
#define SHMMAX 0x2000000 /* max shared seg size (bytes) */
#define SHMMIN 1 /* min shared seg size (bytes) */
#define SHMMNI 4096 /* max number of segments system-wide */
#define SHMALL (SHMMAX/getpagesize()*(SHMMNI/16))
#define SHMSEG SHMMNI /* max shared segs per process */Linux Shared Memory Management
1. Create Shared Memory
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);2. Control Shared Memory
#include <sys/ipc.h>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);3. Attach Shared Memory
#include <sys/types.h>
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);4. Detach Shared Memory
#include <sys/types.h>
#include <sys/shm.h>
int shmdt(const void *shmaddr);Conventions Between Parent and Child Processes
After fork(), the child inherits the parent's attached shared memory.
Calling exec() unloads all attached shared memory.
Calling exit() detaches the process from shared memory.
Example Program
The program creates a shared memory segment, the parent writes an integer, and the child reads it.
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#define SHM_SIZE 1024
int main() {
int shm_id, pid;
int *ptr = NULL;
/* Create shared memory */
shm_id = shmget((key_t)1004, SHM_SIZE, IPC_CREAT | 0600);
/* Attach */
ptr = (int*)shmat(shm_id, 0, 0);
printf("Attach addr is %p
", ptr);
*ptr = 1004;
printf("The Value of Parent is : %d
", *ptr);
if ((pid = fork()) == -1) {
perror("fork Err");
exit(0);
} else if (!pid) {
printf("The Value of Child is : %d
", *ptr);
exit(0);
} else {
sleep(1);
}
/* Detach */
shmdt(ptr);
/* Remove */
shmctl(shm_id, IPC_RMID, 0);
return 0;
}Sample output:
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
