Shared Memory vs Message Passing in Operating System3 Jul 2025 | 6 min read In this article, you will learn the difference between shared memory and memory passing. But before discussing the differences, you must know about the shared memory and message passing. What is Shared Memory?The fundamental model of inter-process communication is the shared memory system. In a shared memory system, the collaborating communicates with each other by establishing the shared memory region in the address space region. If the process wishes to initiate communication and has data to share, create a shared memory region in its address space. After that, if another process wishes to communicate and tries to read the shared data, it must attach to the starting process's shared address space. Shared Memory Applications: -- High-Performance Computing (HPC): shared memory is required when significant amounts of data are needed to be accessed in high-performance computing environments, mainly for simulations and modelling type applications.
- Embedded in Real-Time Systems: Shared memory facilitates quick data exchange between sensors and control systems, and their applications in the automobile and aviation industries.
- Multimedia Processing: Shared video editing or applications with associated support to enhance processing time because of efficiency in inter-process communication.
- Database Systems: Shared memory facilitates enhanced transaction processing concurrency, which results from fast access to data in database management systems.
- OS: The modern operating system employs shared memory for its inter-process.
Advantages of the Shared Memory Model- Fast Communication: As seen, all the processes can directly access the memory and therefore, the rates of communication are fast.
- Efficient for Large Data Transfers: The data structures can be passed from one process to another in large blocks at once, however, there is no need to copy it in every process memory space.
- Less Kernel Involvement: This was said because after the shared memory space has been established, the kernel doesn't have to keep on transferring data from one process to another, which in the long run, is costly.
Disadvantages of the Shared Memory Model- Complex Synchronization: A process has to take care of such synchronization tools and gadgets as semaphores or mutexes to prevent racing conditions.
- Security Risks: Sharing memory is disadvantageous because processes that are running in this location are vulnerable to security threats as everyone who ard the data can access it.
- Limited to a Single Machine: In a distributed system it can only be used for processes inside the same machine.
What is Message Passing?In this message passing process model, the processes communicate with others by exchanging messages. A communication link between the processes is required for this purpose, and it must provide at least two operations: transmit (message) and receive (message). Message sizes might be flexible or fixed. Message Passing Applications: -- Distributed System It is widely used in cloud computing and microservices. These blocks of processes use message passing for parallel processing with the advantage of less communication overhead between the nodes.
- Networked Applications Message passing is an essential application for real-time games and chat applications. Message passing guarantees the isolation of user sessions and promotes scalability.
- Android/ROS: ROS uses message passing among a number of robotic agents to make it easier to build complex applications.
- Fault-Tolerant Systems: the use of message passing to isolate processes also makes applications fault tolerant, such as aerospace and medical applications.
Advantages of the Message Passing Model- Simplicity: Memory management is hidden on the message-passing model, which sort of makes the actual process communication more straightforward.
- No Synchronization Required: In case of processes, there's no requirement to communicate memory as they do not have access to it hence no issues on synchronization.
- Distributed Systems Friendly: Due to such support of communication between processes on two different machines, this model is adopted in distributed systems.
Disadvantages of the Message Passing Model- Slower Communication: The use of message passing entails data copies in the course of data transfer between processes, which widen the communication impendence especially with voluminous data sets.
- More Kernel Involvement: Since interaction occurs in the kernel, there is high overhead since each sent or received message needs interaction.
- Higher Resource Consumption: A transmission of messages may be more costly compared to direct access to memory particularly so when data volume is large.
Emulating message-passing on a shared memory system (MP → SM)- The shared memory system can be made to act as message passing system. The shared address space can be partitioned into disjoint parts; one part being assigned to each processor.
- Send and receive operations care implemented by writing to and reading from the destination/sender processor's address space. The read and write operations are synchronized.
- Specifically, a separate location can be reserved as the mailbox for each ordered pair of processes
Emulating shared memory on a message-passing system (SM → MP)- This is also implemented through read and write operations. Each shared location can be modeled as a separate process. Write to a shared location is emulated by sending an update message tothe corresponding owner process and read operation to a shared location is emulated by sending a query message to the owner process.
- This emulation is expensive as the processes must gain access to other process memory location. The latencies involved in read and write operations may be high even when using shared memory emulation because the read and write operations are implemented by using network-wide communication.
Key differences between the Shared Memory and Message Passing Here, you will learn the various key differences between Shared Memory and Message Passing. Various differences between Shared Memory and Message Passing are as follows: - Shared memory is used to communicate between the single processor and multiprocessor systems. The communication processes are on the same machine and share the same address space. On the other hand, message passing is most commonly utilized in a distributed setting when communicating processes are spread over multiple devices linked by a network.
- Shared memory offers a maximum computation speed because communication is completed via the shared memory, so the system calls are only required to establish the shared memory. On the other hand, message passing takes time because it is performed via the kernel (system calls).
- The shared memory region is mainly used for data communication. On the other hand, message passing is mainly used for communication.
- Make sure that processes in shared memory aren't writing to the same address simultaneously. On the other hand, message passing is useful for sharing little quantities of data without causing disputes.
- The code for reading and writing the data from the shared memory should be written explicitly by the developer. On the other hand, no such code is required in this case because the message passing feature offers a method for communication and synchronization of activities executed by the communicating processes.
Head-to-head comparison between Shared Memory the Message PassingHere, you will learn the head-to-head comparisons between the Shared Memory and the Message Passing. The main differences between the Shared Memory and the Message Passing are as follows: Shared Memory | Message Passing |
---|
It is mainly used for data communication. | It is mainly used for communication. | It offers a maximum speed of computation because communication is completed via the shared memory, so the system calls are only required to establish the shared memory. | It takes a huge time because it is performed via the kernel (system calls). | The code for reading and writing the data from the shared memory should be written explicitly by the developer. | No such code is required in this case because the message passing feature offers a method for communication and synchronization of activities executed by the communicating processes. | It is used to communicate between the single processor and multiprocessor systems in which the processes to be communicated are on the same machine and share the same address space. | It is most commonly utilized in a distributed setting when communicating processes are spread over multiple devices linked by a network. | It is a faster communication strategy than the message passing. | It is a relatively slower communication strategy than the shared memory. | Make sure that processes in shared memory aren't writing to the same address simultaneously. | It is useful for sharing little quantities of data without causing disputes. |
FAQs: -How does cache coherence become a challenge in shared memory systems, and how is it handled? In shared memory systems, multiple processes or threads may access and modify the same memory location simultaneously. This can cause inconsistencies in CPU caches, known as the cache coherence problem. To handle this, hardware protocols like MESI (Modified, Exclusive, Shared, Invalid) or MOESI are used to ensure all caches reflect a consistent view of memory. In contrast, message-passing systems inherently avoid this problem as no memory is shared. In what scenarios does message passing outperform shared memory in distributed systems? Message passing outperforms shared memory in distributed systems where processes run on separate physical machines without shared physical memory. It provides better fault tolerance, scalability, and modularity. For example, in microservices architectures, message queues (like RabbitMQ or Kafka) efficiently handle communication between services where shared memory isn't feasible. |