Shared Memory

Libraries Required

1#include <sys/ipc.h>
2#include <sys/shm.h>
3#include <sys/sem.h>
4#include <sys/mman.h>
5#include <fcntl.h>
6#include <unistd.h>

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

OptionDescription
IPC_CREATCreate the segment if it does not exist
IPC_EXCLFail if segment exists (used with IPC_CREAT)
0666Read & 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

CommandDescription
IPC_STATGet current segment status into shmid_ds struct
IPC_SETSet permissions and other fields from shmid_ds
IPC_RMIDRemove 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

OptionDescription
IPC_CREATCreate the semaphore set if it does not exist
IPC_EXCLFail if it exists (used with IPC_CREAT)
0666Read & 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:

1sops[0].sem_num = 0;
2sops[0].sem_op = -1;
3sops[0].sem_flg = 0;
4semop(semid, sops, 1);

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:

1int fd = open("file.txt", O_RDWR);
2char *addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

Options

Flag / ProtectionDescription
PROT_READPages can be read
PROT_WRITEPages can be written
MAP_SHAREDUpdates visible to other processes mapping same region
MAP_PRIVATECopy-on-write; changes not visible to other processes
MAP_ANONYMOUSMapping is not backed by any file (used with -1 as fd)
MAP_FAILEDReturn 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