Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 3.1: Inter-process communication: shared memory and message passing

How cooperating processes exchange data — a shared memory region or messages through the kernel — with the producer–consumer problem, mailboxes and pipes.

Module 3 1.5 hours Theory CO3
← Before
2.6 Deadlocks
End of Module 2
You are here
3.1 Inter-process communication: shared memory and message passing
Module 3 · Process Synchronization
Next →
3.2 Critical sections
Races, Peterson, Bakery

Learning objectives

By the end of this session you should be able to:

  • Compare the shared-memory and message-passing models of IPC.
  • Explain the bounded-buffer producer–consumer solution using in/out indices.
  • List the System V and POSIX calls used to create and attach shared memory.
  • Describe message-passing design choices: direct vs indirect, blocking vs non-blocking, buffering.
  • Use an ordinary pipe between a parent and child process.

Lecture notes

1. Why processes communicate

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 A
↕ write / read
shared region
↕ read / write
Process B
Kernel — only sets up the region

Message passing

Process A
↓ send(m)
Kernel — message queue
↓ receive(m)
Process B
Shared memoryMessage passing
Kernel involvementOnly to create/attach the region; then plain memory reads/writesEvery send and receive is a system call
SpeedFastest for large dataSlower (copying, traps) but fine for small messages
SynchronizationThe processes' own responsibility (races possible!)Built into send/receive
Distributed systemsHard (needs same machine)Natural — works across a network

2. Shared memory and the producer–consumer problem

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.

Bounded buffer in shared memory (Silberschatz)
#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.

in=0 out=0 [ _ _ _ _ _ ]

Producer stores A, B, C and advances in to 3.

[ A B C _ _ ] in=3 out=0

Consumer takes A from buffer[out], out → 1.

[ · B C _ _ ] in=3 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.

Keep this code in mind

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.

Shared memory in practice — System V and POSIX

System V (used in Lab 8)

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.

POSIX

shm_open(name, O_CREAT|O_RDWR, 0666) → ftruncate(fd, size) → mmap(…, MAP_SHARED, fd, 0) → use → shm_unlink(name).

3. Message passing

Message passing provides two operations, send(message) and receive(message), over a communication link. Three design questions define a link:

Naming: direct or indirect

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.

Synchronization: blocking or not

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.

Buffering

Zero capacity (sender must block), bounded capacity (block when full), unbounded capacity (never blocks).

Pipes — the simplest message channel

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.

pipe_demo.c — parent writes, child reads
#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;
}

VideoBytes

Shared memory vs message passing

Video coming soon

Shared memory vs message passing

Recorded by Dr. Krithika L.B. Slot m3-ipc-models

Pipes and the producer–consumer buffer

Video coming soon

Pipes and the producer–consumer buffer

Recorded by Dr. Krithika L.B. Slot m3-pipes

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
Why is shared memory faster than message passing?

System calls are needed only to set up the region; afterwards data moves with ordinary memory accesses, with no kernel copying or traps.

In the bounded buffer with in/out indices, how many items can the buffer hold?

BUFFER_SIZE − 1, because one slot is left empty to tell a full buffer from an empty one.

What is a rendezvous?

Message passing where both send and receive are blocking, so sender and receiver meet at the exchange.

Why must the child close fd[1] in pipe_demo.c?

A reader sees end-of-file only when all write ends are closed; an unused open write end could make read() wait forever.

Give one advantage of indirect communication (mailboxes).

Processes need not know each other's identity, and one mailbox can connect several senders and receivers.

Key takeaways

  • IPC models: shared memory (fast, processes must synchronise) and message passing (kernel-mediated, easy across machines).
  • Producer–consumer with a circular buffer is the canonical shared-memory pattern.
  • Message links differ in naming, blocking and buffering; pipes are the simplest one-way channel.

Where this leads

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 →