Sub topic 3
Message Passing: Pipes, FIFOs, Message Queues and Sockets
Prerequisites: process and thread basics, C-like pseudocode, process states, context switching, and CPU preemption. This chapter assumes you know that separate processes normally have separate address spaces.
Shared memory versus message passing
Inter-process communication (IPC) lets processes exchange data. In shared-memory IPC, processes map the same memory region and read or write it directly. This is fast, but the processes need synchronisation to prevent races.
In message-passing IPC, the operating system provides a communication channel. A sender places data into the channel, and a receiver obtains it. The processes do not need to share ordinary variables.
Message passing usually provides stronger isolation. Its cost is copying data, system-call overhead, or waiting when the channel is empty or full.
The main mechanisms
| Mechanism | Typical relationship | Data model | Important property |
|---|---|---|---|
| Anonymous pipe | Related processes | Byte stream | Usually one-way |
| FIFO | Unrelated processes | Byte stream | Has a filesystem name |
| Message queue | Related or unrelated processes | Separate messages | Preserves message boundaries |
| Socket | Local or network processes | Stream or datagrams | Supports bidirectional communication |
An anonymous pipe is created with pipe. After fork, both processes inherit its file descriptors. One process closes the read end, and the other closes the write end. A read can block until data is available. A read returns end-of-file after every write end has been closed.
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
int fd[2];
if (pipe(fd) == -1) return 1;
pid_t pid = fork();
if (pid == -1) return 1;
if (pid == 0) {
char buffer[6] = {0};
close(fd[1]);
ssize_t n = read(fd[0], buffer, 5);
if (n > 0) printf("child got: %s\n", buffer);
close(fd[0]);
return 0;
}
close(fd[0]);
write(fd[1], "hello", 5);
close(fd[1]);
waitpid(pid, NULL, 0);
return 0;
}A pipe is a byte stream. If the sender performs two writes, the receiver is not guaranteed to receive two matching reads. The receiver must define its own format, such as fixed-size records or newline-terminated lines.
A FIFO, also called a named pipe, has a pathname created with mkfifo. Unrelated processes can open that pathname. The kernel still transfers a byte stream, and opening one side can block until the other side opens the corresponding side.
A message queue stores discrete messages. A receiver can request a message by type or priority. This makes record boundaries explicit, but queues have finite capacity and require queue-management operations.
A socket is an endpoint for communication. Unix-domain sockets provide local IPC. Internet-domain sockets can connect processes on different machines. Stream sockets resemble reliable byte streams, while datagram sockets preserve packet boundaries but may lose or reorder packets depending on the protocol.
Exercise
Which mechanism should two unrelated local processes use when they need discrete, typed messages instead of an unstructured byte stream?
Show answer
A message queue is the direct choice because it preserves message boundaries and can select messages by type. A FIFO or stream socket would require the processes to define and parse their own record format.