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 memoryNow, 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 memorySince 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.
Similar Reads
What is Volatile Memory? The term memory refers to an electronic place to hold the instructions and data. This stored information is then retrieved for the immediate use. Without the memory computer devices cannot function fastly and efficiently. Memory is used by the operating system, other hardware and software. The memor
5 min read
What is Memory Decoding? The digital electronic circuit is a kind of circuit that only processes signal with two states: either zero or one. Transistors in a circuit are used to conduct various Boolean logic. In digital electronics, the memory decoding process took place, when there is a need to access the memory in digital
9 min read
What are In-Memory Caches? In-memory caches are essential tools in modern computing, providing fast access to frequently used data by storing it in memory. This article explores the concept of in-memory caches, their benefits, and their role in enhancing system performance and scalability. Important Topics for In-Memory Cache
12 min read
What is Distributed Shared Memory and its Advantages? Distributed shared memory can be achieved via both software and hardware. Hardware examples include cache coherence circuits and network interface controllers. In contrast, software DSM systems implemented at the library or language level are not transparent and developers usually have to program th
4 min read
What is File Sharing? File Sharing is a procedure of sharing digital files and resources, such as documents, computer programs, multimedia such as audio and video, graphics, photographs and e-books. It is the sharing of resources or data in a network with varying degrees of access permission, either privately or publicly
5 min read