Cogs and Levers A blog full of technical stuff

Unix IPC: Shared Memory

Shared memory allows multiple processes to view, modify and control shared segments of memory. This snippet will show you how to obtain a pointer to some shared memory and then release the pointer.

key_t key;
int shmid;

/* get an ipc key */
key = ftok("filename", 'R');

/* connect to the segment */
shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT);

/* attach to the segment */
data = shmat(shmid, (void *)0, 0);

/* perform writes and reads on the "data" pointer */

/* detach from the segment */
shmdt(data);

Further reading