Shared memory and message passing are the two methods of establishing interprocess communication. Interprocess communication is communication between the two processes of the operating system.
Interprocess communication always takes place between the cooperating processes. These processes can affect the execution of other processes in the system. Or it can get affected by the other processes of the system.
To establish the interprocess communication between the cooperative processes there are two models, the shared memory model and the message-passing model.
Let us try to understand these interprocess communication models and their differences.
Quick Comparison: Shared Memory Vs Message Passing
|Basis||Shared Memory System||Message Passing System|
|Communication||Communication is established using a shared memory region.||Communication is established using the message passing facility.|
|Data Size||Useful for exchanging larger amount of data.||Useful for exchanging small amount of data.|
|Process Location||Useful for the communicating processes that are on same system.||Useful for the communicating processes that are on different remote system.|
|Speed||Communication between the process is faster.||Communication speed is comparatively slower.|
|Kernel Intervention||Kernel intervention is not there.||Kernel intervention is there.|
What is Shared Memory System?
The shared memory system implements the shared memory model to let the processes communicate with each other. In a shared memory system the interprocess communication is established by creating a shared memory region.
Here a cooperating process creates a shared memory region in its address space. Consider that another cooperating process wants to communicate with this process. So, this another cooperating process will attach itself to the address space of the previous process.
Thus, the shared memory system processes exchange their data by writing the data on the shared memory region. And reading the data from the shared memory region.
In general, the operating system restricts a process from accessing the memory space of another process. but for implementing the shared memory model, two more processes must agree to remove the restriction imposed by the operating system.
The processes sharing the memory space must ensure that while sharing the data they are not writing on the same memory location. Here the communication between the processes is faster as both the processes are present in the same system no kernel intervention is required
What is Message Passing?
The message passing system implements the message passing model to let the processes communicate with each other. Like the shared memory model, here the processes do not share any common memory space. Instead, here the cooperating processes communicate using a message-passing facility.
This technique allows the communicating processes to synchronize their action. Here the processes are not required to be on the same system and thus mechanism is useful for the communication processes that are present in the distributed environment.
The message passing technique allows processes to communicate using two operations. The two operations are:
- Send message
- Receive message
The message passing facility allows the messages to be either of the fixed size or of variable size.
The system implementation for the fixed-size messages is simple but it programming for such system is a complex. However, the system implementation for the variable size messages is complex but programming for this system is simpler.
The most common example of the message passing model is using a chat program over the internet. Here the communicating participants exchange data by sending messages to each other.
Here the communication between the processes is a bit slower as the communication processes are available on a different machine and the kernel intervention is also required, this makes it slower.
Difference Between Shared Memory and Message Passing Process Communication
In a shared memory system the processes communicate with each other using a shared memory region in the address space of the process. Whereas in the message-passing system the processes communicate with each other by passing messages to each other.
The shared memory system is useful for sharing a large amount of data. Whereas the message passing system is useful for sharing small amounts of data.
In the shared memory system, the communicating processes are on the same system. Whereas, in a message-passing system, the communicating processes are on the remote system.
In the shared memory system the communication between the process is faster. But in the message-passing system, the communication between the process is slower.
In the shared-memory model, the processes shared information through a common memory space and thus do not require kernel intervention for sharing information. On the other hand, the message passing requires kernel intervention.
The interprocess communication between the cooperating system can be established by implementing two fundamental models, the shared memory model and the message-passing model.
The communicating processes establish a shared memory region in the shared memory model. And the process shares the information by reading and writing the data to this shared block of memory.
In message passing the communicating processes share the information using the message passing facility and it is useful for the distributed environment.