Mastering Linux Shared Memory: IPC Basics, APIs, and Sample Code
This article explains the principles of Linux shared memory interprocess communication, details the shmid_ds structure and system limits, walks through creation, control, attachment, detachment APIs, and provides a complete C example demonstrating parent‑child data sharing via shmget, shmat, shmdt, and shmctl.
Shared Memory IPC Principles
Shared memory is a separate memory segment with its own data structures, including permissions, size, and timestamps. The 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; /* creator pid */
__kernel_ipc_pid_t shm_lpid; /* last operator pid */
unsigned short shm_nattch; /* number of current attaches */
unsigned short shm_unused; /* compatibility */
void *shm_unused2; /* used by DIPC */
void *shm_unused3; /* unused */
};Two processes must attach the shared memory segment to their address spaces before using it.
The system imposes the following limits:
#define SHMMAX 0x2000000 /* max shared segment size (bytes) */
#define SHMMIN 1 /* min shared segment size (bytes) */
#define SHMMNI 4096 /* max number of segments system‑wide */
#define SHMALL (SHMMAX/getpagesize()*(SHMMNI/16))
#define SHMSEG SHMMNI /* max shared segments 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
void *shmat(int shmid, const void *shmaddr, int shmflg);
4. Detach shared memory
int shmdt(const void *shmaddr);
Conventions Between Parent and Child Processes
After fork(), the child inherits the parent's attached shared memory.
If exec() is called, all attached shared memory is automatically detached.
When a process calls exit(), its attached shared memory is detached.
Example Program
The following C program allocates 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>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.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 shared memory to process address space
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 shared memory
shmdt(ptr);
// Delete shared memory
shmctl(shm_id, IPC_RMID, 0);
return 0;
}Running the program produces output showing the same integer value in both parent and child processes.
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.
