Horje
What is a Shared Memory?

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.

Frequently Asked Questions on Shared Memory – FAQs

What is shared memory and how is it used in Inter-process communication?

Shared memory is a technique that is used for data exchange between two or more processes. Inter-process communication works on the concept of shared memory. A process takes up some of the available space and uses it as shared memory. It then provides the right to other processes in order to exchange data with them

What is Mutual Exclusion in shared memory?

Mutual Exclusion means that only one process can access the shared memory at a time. Other processes have to wait until shared memory is released by the process using it. It can be achieved by message passing indicating the acess and release of the shared memory by a process.

How to ensure security of shared memory segments?

The shared memory segments help to exchange data and hence need to be secure. Implementing authentication mechanisms can help in verifying the identity of the process that is trying to access the shared memory. Authorization checks can help in checking if a process has the necessary rights for accessing the shared memrory.




Reffered: https://www.geeksforgeeks.org


Computer Subject

Related
What is a Device? What is a Device?
What is Hexadecimal Numbering ? What is Hexadecimal Numbering ?
What is the Formatting Toolbar? What is the Formatting Toolbar?
What is Terabyte? What is Terabyte?
What is Computer Icon? What is Computer Icon?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17