Shared memory vs message passing
Shared memory vs message passing
Recorded by Dr. Krithika L.B. Slot m3-ipc-models
How cooperating processes exchange data — a shared memory region or messages through the kernel — with the producer–consumer problem, mailboxes and pipes.
By the end of this session you should be able to:
Session 2.2 showed that processes do not share memory after fork(): a change to x in the child is
invisible to the parent. Yet cooperating processes need to exchange data — a compiler pipeline, a browser's renderer and network
processes, a database server and its clients. The OS therefore provides inter-process communication (IPC)
mechanisms. There are two fundamental models.
Shared memory
Process AMessage passing
Process A| Shared memory | Message passing | |
|---|---|---|
| Kernel involvement | Only to create/attach the region; then plain memory reads/writes | Every send and receive is a system call |
| Speed | Fastest for large data | Slower (copying, traps) but fine for small messages |
| Synchronization | The processes' own responsibility (races possible!) | Built into send/receive |
| Distributed systems | Hard (needs same machine) | Natural — works across a network |
The classic paradigm for cooperating processes: a producer generates items (a compiler producing assembly code), a consumer uses them (the assembler). They share a buffer — unbounded, or bounded with a fixed size, used as a circular array with two indices.
#define BUFFER_SIZE 10
typedef struct { /* ... */ } item;
item buffer[BUFFER_SIZE];
int in = 0; /* next free slot */
int out = 0; /* first full slot */
/* Producer */ /* Consumer */
while (true) { while (true) {
/* produce next_produced */ while (in == out)
while (((in + 1) % BUFFER_SIZE) == out) ; /* empty: wait */
; /* full: do nothing */ next_consumed = buffer[out];
buffer[in] = next_produced; out = (out + 1) % BUFFER_SIZE;
in = (in + 1) % BUFFER_SIZE; /* consume next_consumed */
} }in == out → buffer empty; the consumer spins.
Producer stores A, B, C and advances in to 3.
Consumer takes A from buffer[out], out → 1.
Indices wrap with % BUFFER_SIZE, so the array is used as a ring.
(in + 1) % SIZE == out → full. One slot is always left empty to distinguish full from empty, so this version holds at most BUFFER_SIZE − 1 items.
If we add a count variable to use all slots, the producer's count++ and the consumer's count-- can interleave and corrupt count. That is the race condition of Session 3.2.
shmget(key, size, IPC_CREAT|0666) create/get a segment → shmat(id, NULL, 0) attach (returns a pointer) → read/write → shmdt(ptr) detach → shmctl(id, IPC_RMID, NULL) remove.
shm_open(name, O_CREAT|O_RDWR, 0666) → ftruncate(fd, size) → mmap(…, MAP_SHARED, fd, 0) → use → shm_unlink(name).
Message passing provides two operations, send(message) and receive(message), over a
communication link. Three design questions define a link:
Direct: send(P, msg), receive(Q, msg) — processes name each other. Indirect: messages go to mailboxes/ports (send(A, msg)); a link can be shared by many processes.
Blocking send waits until received; non-blocking send returns at once. Blocking receive waits for a message; non-blocking receive returns a message or null. Blocking send + blocking receive = rendezvous.
Zero capacity (sender must block), bounded capacity (block when full), unbounded capacity (never blocks).
An ordinary pipe (pipe(fd)) is a one-way byte stream between related processes: fd[1] is the
write end, fd[0] the read end. The shell's ls | wc -l (Lab 1) uses exactly this. Named pipes
(FIFOs, mkfifo) work between unrelated processes.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
int fd[2];
char buf[64];
pipe(fd); /* fd[0] read end, fd[1] write end */
if (fork() == 0) { /* child: reader */
close(fd[1]);
int n = read(fd[0], buf, sizeof(buf) - 1);
buf[n] = '\0';
printf("Child received: %s\n", buf);
close(fd[0]);
} else { /* parent: writer */
close(fd[0]);
const char *msg = "hello through the pipe";
write(fd[1], msg, strlen(msg));
close(fd[1]);
wait(NULL);
}
return 0;
}Shared memory vs message passing
Recorded by Dr. Krithika L.B. Slot m3-ipc-models
Pipes and the producer–consumer buffer
Recorded by Dr. Krithika L.B. Slot m3-pipes
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0System calls are needed only to set up the region; afterwards data moves with ordinary memory accesses, with no kernel copying or traps.
BUFFER_SIZE − 1, because one slot is left empty to tell a full buffer from an empty one.
Message passing where both send and receive are blocking, so sender and receiver meet at the exchange.
A reader sees end-of-file only when all write ends are closed; an unused open write end could make read() wait forever.
Processes need not know each other's identity, and one mailbox can connect several senders and receivers.
Shared memory is fast, but it leaves the hardest job to the processes: making sure two of them never update the same data at the same time. Next: race conditions and the critical-section problem.
Continue to 3.2 — Race conditions and the critical-section problem →