Sub topic 1
Why Processes Communicate: Shared Memory versus Message Passing
Processes are isolated from one another. Each process normally has its own virtual address space, so a variable in one process cannot be read directly by another process. Inter-process communication (IPC) is the set of operating-system mechanisms that let processes exchange data or coordinate their actions.
Prerequisites
This chapter assumes that you understand processes, threads, process states, context switching, preemption, and basic C-like pseudocode. You should also know that the scheduler can interrupt a process between two instructions.
Two IPC models
IPC uses one of two main models:
- Shared memory: The operating system creates a memory region that multiple processes can access. The processes communicate by reading and writing that region.
- Message passing: Processes exchange discrete messages through an operating-system mechanism. A sender submits a message, and a receiver obtains it.
| Model | How data moves | Main advantage | Main concern |
|---|---|---|---|
| Shared memory | Processes read and write a common region | Fast after setup | Requires synchronization |
| Message passing | Processes send and receive messages | Clear ownership and isolation | Copying and system-call overhead |
Shared memory is often efficient because large amounts of data can remain in one region instead of being copied through the kernel for every exchange. However, the processes must agree on the layout and access rules. If one process writes while another reads, the result can depend on timing. Locks, semaphores, or other synchronization tools are usually required.
Message passing provides a stronger communication boundary. The operating system controls the channel and can block a receiver until a message arrives. The channel can be local to one machine or, in the case of sockets, extend across a network. Message passing can therefore simplify ownership, but sending and receiving may require system calls and data copying.
Common message-passing mechanisms
Common mechanisms include:
- Pipes: One-way byte streams, often used between a parent and child process.
- FIFOs: Named pipes that unrelated local processes can open.
- Message queues: Kernel-managed queues that preserve message boundaries.
- Sockets: Endpoints for local or network communication.
- Shared-memory objects: Named or unnamed regions mapped into multiple address spaces.
A pipe does not normally preserve the idea that one write equals one complete message. It provides a byte stream. A message queue, by contrast, stores separate messages. This difference matters when the receiver must distinguish records.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
int fd[2];
char buffer[6] = {0};
if (pipe(fd) == -1) return 1;
pid_t pid = fork();
if (pid == -1) return 1;
if (pid == 0) {
close(fd[1]);
if (read(fd[0], buffer, 5) == 5)
printf("child got: %s\n", buffer);
close(fd[0]);
} else {
close(fd[0]);
write(fd[1], "hello", 5);
close(fd[1]);
wait(NULL);
}
return 0;
}The parent and child inherit the pipe's file descriptors after fork. The parent writes five bytes, and the child reads them. Closing unused ends is important: it prevents resources from remaining open and allows end-of-file behavior to work correctly.
Choosing a model
Use shared memory when processes exchange large or frequently updated data and can safely coordinate access. Use message passing when clear message boundaries, isolation, blocking communication, or communication across machines matters more than minimum copying overhead.
Exercise
A process writes records into shared memory while another process reads them. Does choosing shared memory remove the need for synchronization?
Show answer
No. Both processes can access the same bytes concurrently, so they still need a protocol, such as semaphores or a mutex, to prevent races and to indicate when a record is ready.