Open In App

What is a Shared Memory?

Last Updated : 10 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Shared Memory is a concept of Operating Systems and is mainly used in Inter-process Communication. If two processes want to share data with each other, they need some intermediate medium to do so and this is where shared memory comes into the picture.

What is a Shared Memory?

Every process has a dedicated address space in order to store data. If a process wants to share some data with another process, it cannot directly do so since they have different address spaces. In order to share some data, a process takes up some of the address space as shared memory space. This shared memory can be accessed by the other process to read/write the shared data.

Working of Shared Memory

Let us consider two processes P1 and P2 that want to perform Inter-process communication using a shared memory.

P1 has an address space, let us say A1 and P2 has an address space, let us say A2. Now, P1 takes up some of the available address space as a shared memory space, let us say S1. Since P1 has taken up this space, it can decide which other processes can read and write data from the shared memory space.

For now, we will assume that P1 has given only reading rights to other processes with respect to the shared memory. So, the flow of Inter-process communication will be as follows:

  • Process P1 takes up some of the available space as shared memory S1
  • Process P1 writes the data to be shared in S1
  • Process P2 reads the shared data from S1
Working of shared memory
Working of shared memory

Now, let us assume that P1 has given write rights to P2 as well. So the communication will shown in the below diagram:

working of shared memory-2
Working of shared memory

Since P1 took up the space for shared memory i.e. since process P1 is the creator process, only it has the right to destroy the shared memory as well.

Use Cases of Shared Memory

  • Inter-Process Communication: Shared memory is primarily used in IPC where two processes need a shared address space in order to exchange data.
  • Parallel Processing: Multiple processes can share and modify data in the shared address space, thereby speeding up computations.
  • Databases: Shared memory is used in databases, in the form of cache, so that reading and writing of data can be much faster
  • Graphics and Multimedia Applications: CPU and GPU can access data concurrently which is helpful in tasks such as video manipulation and processing.
  • Distributed Systems: Two different machines can access data from a shared space and work as a single system.

Advantages

  • Shared memory is one of the fastest means of IPC since it avoids overheads.
  • Easy access to data once set up.
  • It is memory efficient as processes do not need to separately store shared data.

Disadvantages

  • Since it is operating system specific, it is difficult to implement common synchronization and authorization techniques.
  • Memory leaks can take place.
  • If the processes wait indefinitely for each other in order to release the shared memory space, a deadlock can occur.

Conclusion

Shared memory is a way for multiple processes to access the same memory space, making it easy to share data. This approach allows different programs to communicate and exchange information quickly and efficiently, helping them work together better. Shared memory is an efficient way for Inter-process communication and allows process to exchange data seamlessly. But it can pose several issues if proper synchronization and authorization techniques are not implemented.


Next Article

Similar Reads