Shared memory system is one of the fundamental models of interprocess communication. In the shared memory system, the cooperating processes communicate with each other by establishing the shared memory region, in its address space. Shared memory model allows the fastest interprocess communication.
Among the cooperating processes, the process that wants to initiate the communication and have some data to share, establish the shared memory region in its address space. Next, another process that wishes to communicate and want to read the shared data, must attach itself to the initiating process’s shared address space.
Content: Shared Memory System
In Shared Memory system, the cooperating processes communicate, to exchange the data or the information with each other. For this, the cooperating processes establish a shared region in their memory. The processes share data by reading and writing the data in the shared segment of the processes.
Consider a scenario, there are two cooperating processes P1 and P2. Both the processes P1 and P2, have their different address spaces. Now, P1 wants to share some data with P2. So, P1 and P2 will have to perform the following steps.
Step 1: As the process P1 has some data, to share with process P2. Process P1 has to take the initiative and establish a shared memory region in its own address space and store the data or information to be shared in it’s shared memory region.
Step 2: Now, P2 requires the information stored in the shared segment of the P1. So, process P2 has to attach itself to the shared address space of P1. Now, P2 can read out the data from there.
Step 3: Process P1 and P2 can exchange information or data by reading and writing data in the shared segment of the process.
Now let us clear this concept with the help of the diagram given below.
As you can see in the image above process P1 has some data to be shared with P2. So, P1 writes the data in its shared memory. Now, process P2 wants the data shared by P1 so it attaches itself to shared address space of P1 and reads the required data from there.
As we all know several processes execute concurrently on the system. Now, these concurrently executing processes can either be independent processes which do not affect the other process neither get affected by other processes or they can be Cooperating processes.
Cooperating Processes can affect the other processes in the system or it can be get affected by the other processes. In an easy language, the processes that share data with each other are called Cooperating processes. And for the sharing of the data, the cooperating processes can use either of two IPC (interprocess communication) mechanisms which are sharing memory system or message passing system.
Operating system strictly restricts one process to share the memory of the other process. But Sometimes processes need to interact and share data. And for this, we have to set an environment where the cooperating processes can exchange data or information with each other.
Interprocess communication has two fundamental models i.e. shared memory system and message passing system.
- In the shared memory system, the cooperating process which wants to initiate the communication establishes a region of shared memory in its address space. The other cooperative process which requires the shared data has to attach itself to the address space of the initiating process.
- The message-passing system shares the data by passing the messages. If a process needs to share the data, it writes the data in the message and passes it on to the kernel. The process that requires the shared data read it out from the kernel.
What is the need of the Shared Memory System?
As we have learned above, the operating system never allows a process to access the memory space of another process. But, what if two processes need to communicate and share data? We must provide an environment to the processes where they can easily exchange data with each other.
The communicating processes are of two types related or unrelated.
- Related Processes: Now, the inter-related processes share data using Pipes.
- Unrelated Processes: The unrelated processes communicate or share data using popular interprocess communication techniques that are shared memory system and message passing. Leaving message passing for later we will focus on the shared memory system.
Shared memory model removes the restriction from the processes, that no process will access each other memory, by establishing the shared region in the process’s address space. This shared region in the address space of the memory is used for exchanging of information between the cooperating processes.
Shared Memory Example
We can understand the concept of the shared memory system with the help of the most popular producer-consumer problem. The producer and the consumer process both share a “buffer” which they use as a “queue”. The job of the producer process is to generate the data and fill it into the buffer. The job of the consumer process is to consume the data and empty the buffer.
The problem is that the producer must not generate data if the buffer is full and the consumer must not consume data if the buffer is empty.
The solution to this problem is shared memory. The producer and consumer processes must have a common buffer, that resides in the region of memory. This region of memory is shared by both the producer process and the consumer process. Now, the producer and the consumer both the processes must be synchronized, so that the consumer may not consume the data that has not yet been produced by the producer.
There are two types of Buffer
- Bounded buffer: In bounded buffer, the producer has to wait to produce new data in case the buffer is full and the consumer has to wait to consume the data in case the buffer is empty.
- Unbounded buffer: In unbounded buffer, the producer may go on producing the data, there is no limit on the size of the buffer. But, the consumer may have to wait if the buffer is empty.
Advantages of Shared Memory
- Shared memory system is faster interprocess communication model.
- Shared memory allows cooperating processes to access the same pieces of data concurrently.
- Using shared memory, also speed ups the computation power of the system as the long task can be divided into smaller sub-tasks and can be executed in parallel.
- Modularity can also be achieved if we divide up the system functions into separate processes or threads.
- Using shared memory user can perform multiple tasks at a time. Like user can print, create a document, playing MP3 at the same time.
- The shared memory system is one of the fundamental models of interprocess communication.
- Two processes communicate with each other by establishing a shared memory in the address space of the process which wants to initiate the communication. and the other process that wishes to communicate attaches itself to the shared address space of the initiating process.
- Shared memory system removes the restriction put up by an operating system that no process must access the memory of the other process. Which in turn allows the processes to exchange the data.
- Shared memory speeds up the computation and allows multitasking.
- Shared memory system is faster than the message passing system.
In shared memory system a shared region is established in the address space of the process that has to share the data. Processes requesting for shared data must attach themselves to the address space of the process sharing the data.