Shared Memory in Linux Inter-Process Communication (IPC)
In Linux, shared memory is an efficient method for inter-process communication (IPC). Through shared memory, multiple processes can access the same memory region directly, enabling fast data transfer. This article details the four main functions for shared memory: shmget, shmat, shmdt, and shmctl.
Shared Memory Functions
1. shmget - Create or Get a Shared Memory Segment
The shmget function is used to create or get a shared memory segment.
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);
- Parameters:
key: A unique identifier for the shared memory segment.size: The size of the shared memory segment in bytes.shmflg: Permission flags and creation control flags, usually a combination ofIPC_CREAT,IPC_EXCL, and permission bits.
- Return Value: Returns the shared memory segment identifier (ID) on success, and
-1on failure.
2. shmat - Attach the Shared Memory Segment to the Process’s Address Space
The shmat function attaches the shared memory segment to the address space of the calling process.
#include <sys/ipc.h>
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);
- Parameters:
shmid: The shared memory segment ID returned byshmget.shmaddr: Address at which to attach the shared memory segment, typicallyNULLto let the system choose the address.shmflg: Flags for the attach operation, such asSHM_RDONLYfor read-only access.
- Return Value: Returns a pointer to the shared memory segment on success, and
(void *) -1on failure.
3. shmdt - Detach the Shared Memory Segment from the Process’s Address Space
The shmdt function detaches the shared memory segment from the address space of the calling process.
#include <sys/ipc.h>
#include <sys/shm.h>
int shmdt(const void *shmaddr);
- Parameters:
shmaddr: Pointer to the shared memory segment, returned byshmat.
- Return Value: Returns
0on success, and-1on failure.
4. shmctl - Control Shared Memory Segment
The shmctl function performs various control operations on a shared memory segment, such as deletion or information retrieval.
#include <sys/ipc.h>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
- Parameters:
shmid: The shared memory segment ID.cmd: Control command, such asIPC_RMID(remove the segment),IPC_STAT(get segment status), etc.buf: Pointer to ashmid_dsstructure for storing or providing shared memory segment information.
- Return Value: Returns
0on success, and-1on failure.
Enabling IPC in the Kernel
Before using shared memory, ensure that IPC support is enabled in the kernel. The following kernel configuration options must be enabled:
CONFIG_SYSVIPC=y
CONFIG_SYSVIPC_SYSCTL=y
Otherwise, you may encounter errors during program execution, such as:
Unable to get shared memory segment(shmget)
shmid = -1, size = 32, size1 = 8192, Error 38
Example Code
Here is a simple example of using shared memory in a program:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#define SHM_KEY 1234
int main() {
int shmid;
char *shmaddr;
const int size = 1024;
// Create the shared memory segment
shmid = shmget(SHM_KEY, size, IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget failed");
exit(1);
}
// Attach the shared memory segment to the process's address space
shmaddr = (char *) shmat(shmid, NULL, 0);
if (shmaddr == (char *) -1) {
perror("shmat failed");
exit(1);
}
// Write data to the shared memory
strncpy(shmaddr, "Hello, Shared Memory!", size);
// Read data from the shared memory
printf("Data in shared memory: %s\n", shmaddr);
// Detach the shared memory segment
if (shmdt(shmaddr) == -1) {
perror("shmdt failed");
exit(1);
}
// Remove the shared memory segment
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl failed");
exit(1);
}
return 0;
}
Summary
Shared memory is an efficient IPC method, ideal for scenarios requiring rapid data transfer. With the shmget, shmat, shmdt, and shmctl functions, Linux provides comprehensive support for shared memory. Ensure IPC is enabled in the kernel, then use these functions to implement shared memory in your programs easily.


