What is Shared Memory ?

Shared Memory

Shared memory allows multiple processes share a region of memory.It gets mapped to processes’ address space.
 

Shared memory
Shared memory




Advantages of Shared memory

  • Fastest IPC mechanisms
  • Does not require to copying data between the interested processes.
  • Processes directly access the memory

Disadvantages of Shared memory

  • Programmer has to ensure synchronized access to shared memory using semaphore or mutex, otherwise results are unpredictable.

 


Shared Memory System Calls

System calls used for shared memory

  • int shmget(key_t key, size_t size, int shmflg);
  • void *shmat(int shmid,void *shmaddr,int shmflg);
  • int shmdt(void *shmaddr);
  • int shmctl(int shmid, int cmd, struct shmid_ds *buffer);

Include files for shared memory

  • <sys/ipc.h>
  • <sys/shm.h>

 


shmget()

One of the processes desired to use shared memory has to create shared memory block where as others which need to access to that shared memory has to get it.
int  shmget (key_t key, size_t size, int  shmflg);
key – key obtained using ftok()  or IPC_PRIVATE
size a size of the shared memory block in bytes.
shmflag a which can be
IPC_CREAT a create a new shared memory segment.
if segment already exists, returns id of the existing segment.
IPC_EXCL  a do not create if specified key already exists.
In addition, permission mode flags are ORed with shmflag.
Returns ID of the shared memory segment.
shmid = shmget ( key, 1000, IPC_CREAT);
 


shmat()

Attaches shared memory.

  • shared memory gets mapped to the calling process’ address space

void *shmat(int shmid,void *shmaddr, int shmflg);
shmid – ID returned by shmget() 
shmaddr à address where the shared memory to be mapped.
It is recommended to pass value zero (0), to let kernel identify itself the suitable address.
shmflag à indicates the operations that should be allowed on the segment.
SHM_RDONLY à make the segment read-only.
by default segment is given read-write access.
 
Returns pointer to the shared memory segment, to be used for access.
This pointer is no different from conventional pointer, except that it refers to a shared memory segment.
 


shmdt()

Detaches shared memory.

  • shared memory gets unmapped from the calling process’ address space

int shmdt(void *shmaddr);
shmaddr a address returned by shmat()
Example :
void *shmptr;
key_t  key = ftok(“.”, ‘A’);
shmid = shmget (key, 100, IPC_CREAT);
shmptr = shmat(shmid, 0, SHM_RDONLY);
  // logic that uses shared memory segment
shmdt(shmptr);
 


shmctl()

This system call is used to get information of a shared memory segment or to remove it.
  int  shmctl(int shmid, int cmd,
  struct shmid_ds *ds_buf);
  cmd à represents the operation, which can be
IPC_STAT     to get the info about specific segment
IPC_RMID  remove specified segment
NOTE: Segment gets removed when the last process which holds it detaches.
Returns 0 on success , -1 on failure.
Example :
struct shmid_ds  ds_buf;
shmctl(shmid, IPC_STAT, &ds_buf);
shmctl(shmid, IPC_RMID, NULL);
After the purpose is served, shared memory segment must be removed.
 


Shared memory operation by two processes

Shared memory
Shared memory operation by two processes