Shared Memory
Libraries Required
shmget()
Allocates or accesses a shared memory segment using a key
1int shmget(key_t key, size_t size, int shmflg);
Return Value:
- Success: Shared memory segment ID
- Error: -1
Example:
1int shmid = shmget(IPC_PRIVATE, 1024, IPC_CREAT | 0666);
Options
Option | Description |
---|---|
IPC_CREAT | Create the segment if it does not exist |
IPC_EXCL | Fail if segment exists (used with IPC_CREAT ) |
0666 | Read & write permissions for all (standard mode) |
shmat()
Attaches a shared memory segment to the process’s address space
1void *shmat(int shmid, const void *shmaddr, int shmflg);
Return Value:
- Success: Pointer to shared memory segment
- Error: -1
Example:
1char *data = (char *)shmat(shmid, NULL, 0);
shmdt()
Detaches a shared memory segment from the process’s address space
1int shmdt(const void *shmaddr);
Return Value:
- Success: 0
- Error: -1
Example:
1shmdt(data);
shmctl()
Performs control operations on a shared memory segment (eg., remove, get info).
1int shmctl(int shmid, int cmd, struct shmid_ds *buf);
Return Value:
- Success: 0
- Error: -1
Example:
1shmctl(shmid, IPC_RMID, NULL);
Options
Command | Description |
---|---|
IPC_STAT | Get current segment status into shmid_ds struct |
IPC_SET | Set permissions and other fields from shmid_ds |
IPC_RMID | Remove the segment |
semget()
Creates a new semaphore set or accesses an existing one
1int semget(key_t key, int nsems, int semflg);
Return Value:
- Success: Semaphore set ID
- Error: -1
Example:
1int semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
Options
Option | Description |
---|---|
IPC_CREAT | Create the semaphore set if it does not exist |
IPC_EXCL | Fail if it exists (used with IPC_CREAT ) |
0666 | Read & write permissions |
semop()
Performs one or more atomic operations on semaphores
1int semop(int semid, struct sembuf *sops, size_t nsops);
Return Value:
- Success: 0
- Error: -1
Example:
Memory Mapping
mmap()
Maps a file or anonymous memory into the process’s address space
1void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
Return Value:
- Success: Pointer to mapped area
- Error: MAP_FAILED
Example:
Options
Flag / Protection | Description |
---|---|
PROT_READ | Pages can be read |
PROT_WRITE | Pages can be written |
MAP_SHARED | Updates visible to other processes mapping same region |
MAP_PRIVATE | Copy-on-write; changes not visible to other processes |
MAP_ANONYMOUS | Mapping is not backed by any file (used with -1 as fd ) |
MAP_FAILED | Return value on error |
munmap()
Unmaps a previously mapped memory region from the address space
1int munmap(void *addr, size_t length);
Return Value:
- Success: 0
- Error: -1
Example:
1munmap(addr, 4096);
Last updated on