Sub topic 1
Why processes communicate: shared memory versus message passing
Prerequisites
This chapter assumes you know what a process and a thread are, that a context switch saves one process's state so the CPU can run another, and that the CPU scheduler can preempt a running process. You should be able to read C-like pseudocode.
Processes run in separate address spaces, so one process cannot read another process's variables directly. Inter-process communication (IPC) is the set of OS mechanisms that let processes exchange data anyway. There are two models.
Communicating through shared memory
In the shared-memory model, the OS maps one region of memory into the address spaces of both processes. Reads and writes are fast because the kernel does not copy the data. The cost is that the processes must coordinate access themselves. The OS does not stop two writers from touching the same bytes at the same time.
Message passing: pipes, FIFOs, message queues and sockets
In the message-passing model, the kernel copies data from sender to receiver. Common mechanisms include:
- Pipes: a one-way byte stream, usually between related processes such as a parent and its child.
- FIFOs (named pipes): a pipe with a name in the file system, so unrelated processes can open it.
- Message queues: the kernel stores discrete messages and delivers them in order.
- Sockets: endpoints that connect processes on one machine or across a network.
| Model | Who moves the data | Speed | Main risk |
|---|---|---|---|
| Shared memory | Processes | Fast | Race conditions |
| Message passing | Kernel | Slower (copies) | Blocking and buffer limits |
#include <stdio.h>
#include <unistd.h>
int main(void) {
int fd[2];
char buf[6] = {0};
if (pipe(fd) == -1) return 1;
if (fork() == 0) {
close(fd[1]);
read(fd[0], buf, 5);
printf("child got: %s\n", buf);
close(fd[0]);
} else {
close(fd[0]);
write(fd[1], "hello", 5);
close(fd[1]);
}
return 0;
}Race conditions and interleavings
A race condition occurs when the result depends on the order in which processes or threads run. Suppose two threads each execute counter = counter + 1 on a shared counter that starts at 0. Each increment has three steps: load, add and store. If both threads load 0 before either stores, both store 1, and the final value is 1 instead of 2. That specific interleaving produces the wrong result.
The critical-section problem
A critical section is the part of a process's code that accesses shared data. The critical-section problem is to design an entry section and an exit section around each critical section so that the race cannot occur.
Mutual exclusion, progress and bounded waiting
A correct solution must meet three requirements:
- Mutual exclusion: at most one process is in its critical section at a time.
- Progress: if no process is in its critical section and some want to enter, the decision about who enters next is made by the processes that want to enter, and is not postponed indefinitely.
- Bounded waiting: after a process requests entry, a limit exists on how many times other processes can enter first.
Exercise
Two threads each run counter = counter + 1 once on a shared counter that starts at 0. Which final values are possible, and why?
Show answer
The final value can be 2 when the updates do not overlap, or 1 when both threads load 0 before either stores. The value 0 is not possible because each thread's store writes at least 1.