Shared Memory Model – Interprocess Communication

By | September 26, 2021

Shared Memory Model – Interprocess Communication

  • Normally, processes use memory areas within the scope of virtually memory space. Also memory management systems ensure that every process has a well-defined and separate data and code area.
  • In shared memory model parts of private address space of two or more are processes are mapped to same physical memory.
  • Such memory areas are called shared memory regions.
  • Shared memory locations provide another methods for sharing information between the processes and thus serve as mechanism of IPC.

Shared memory space, Shared Memory Model - Interprocess Communication


  • For this, computer hardware should support the shared memory segments a operating system helps processes in creating and destroying shared memory regions.
  • Some operating system support a supervisor call that creates a shared memory space.

Example of Shared Memory Model:

For example, in UNIX, shared memory facility enables an area of memory to shared by two or more processes. For shared memory communication, one process would write into a certain commonly accessed area and another process would read subsequently from that area. Thus, read and write are two basic communication primitives used in this model and they do not require making system calls.

These shared memory regions are managed by processes themselves. There is no intervention from operating system in this regard. Once a shared memory region becomes a part of process address space, the corresponding processes can access the regions without causing any problems of address violation. In order to implement shared memory regions, two important header files are included in the process definition. These header files are shm.h and ipc.h.

The operating system provides various system calls to create a shared memory regions and to attach existing memory regions to process address spaces. The system associates a unique key with each shared memory regions that are created in the system. In other words, we can say that the shared memory segments that are implemented in the kernel are referred to by a non-negative integer identifier.

Shared Memory Model - Interprocess Communication

The processes use this key to attach and detach a shared memory region to (and from) their address spaces. The operating system only creates shared memory regions and attaches them to processes. Granularity (i.e. data types) can decided by processes themselves, and not by operating system. The synchronization of operation executions on shared data in solely done by applications themselves, and not by operating system. In other words, programs for manipulating data in shared memory regions are part of applications themselves and not of the operating system. The operating system helps processes in creation, maintenance and destruction of shared memory regions.

The shared memory facility uses four system calls. These are:

  1. shmget
  2. shmat
  3. shmdt
  4. shmetl

1. Shmget :

This call used by a process to create shared segment in physical memory. In this call, a logical key value supplied and a segment id will returned. Shmget also used by other processes to obtain the segment id of a current shared segment, using the key value.

2. Shmat :

This call is use by a process to access the shared memory. This call attaches the shared memory to process’s logical address space.

3. Shmdt :

This call detaches the shared memory from the calling process.

4. Shmctl :

This call sets a queries certain control parameter or destroys a shared segment.

Implementing Shared Memory

There are various steps in setting up the shared memory model. These are :

1. First step is to set up shared memory mechanism in Kernel. The required data structure will obtained by using skmget( ) system call with following syntax :

in shmget (key_t key, int size, int flag);

  • Here key_t is a data type used for key. It is usually a long integer and is defined in header file <sys/types.h>
  • The second parameter, size is the size of the shared memory region in bytes.
  • The third parameter is a combination of usual file access permissions of r/w/e for users, groups and others.

2. Once the shared memory region has been created, the second step is to attach it to processes that would share it. This will done by using shmat( ) system call. The syntax for this call is:

Char*shmat(int id, char *addr, int flags)

Here first argument id is the identifier of shared memory region. Second parameter, addr is the virtual address where the user wants to attach the shared memory. This argument should be set to zero, if the kernel is to determine the attachment. The third parameter, flags, specify whether the region read only or both.

3. When a process has finished using shared memory region, then this region can detached for that process. This will done using shmdt( ) system call. The system call shmdt( ) takes a single argument, i.e. the address of the shared memory region.

4. The last step is to clean up the kernel’s data space. This can done using a system call-shmctl(). This call takes three parameters as input. These shared memory id, a set of flags and a buffer that allows copying between the user and the kernel data space.

Related Articles

[su_posts template=”templates/default-loop.php” id=”” posts_per_page=”3″ post_type=”post” taxonomy=”category” tax_term=”” tax_operator=”IN” author=”” meta_key=”” offset=”0″ order=”DESC” orderby=”date” post_parent=”” post_status=”publish” ignore_sticky_posts=”no”]