Inter-Process Communication: Pipes, Shared Memory & Message Passing
“How processes talk to each other — and how GATE turns fork() into a counting puzzle.”
1.Why IPC Exists
§ 2.1.Processes have isolated address spaces by design — a process cannot read or write another process's memory directly. Yet cooperative processes often need to exchange data: a web server passes an HTTP request body to a worker, a shell pipes output from ls into grep.
The OS provides Inter-Process Communication (IPC) mechanisms that allow processes to exchange data safely under kernel supervision. The two fundamental models are shared memory (processes map the same physical page frames) and message passing (the kernel copies data between process buffers).
2. IPC Mechanisms
| Feature / Aspect | Mechanism | Mechanics & GATE Significance |
|---|---|---|
| Unnamed Pipe | Created with pipe(fd[2]). Unidirectional byte stream. Data flows fd[1] (write) to fd[0] (read). Only between related processes (parent-child after fork()). | GATE: output tracing of fork() + pipe() programs. After fork(), both processes inherit both pipe ends — each side must explicitly close the end it does not use. |
| Shared Memory | shmget() + shmat() or mmap(). Kernel maps same physical frames into multiple virtual address spaces. Zero kernel copies — fastest IPC mechanism. | Requires explicit synchronization (semaphores). Shared memory alone provides NO mutual exclusion. |
| Message Queue | msgsnd() / msgrcv(). Kernel maintains typed message queue. Sender and receiver need not run simultaneously — asynchronous. | Conceptual MCQs: message passing works between unrelated processes; no shared memory or common parent required. |
| Socket | Endpoint for network or local (Unix domain) communication. Bidirectional full-duplex. Supports TCP (stream) or UDP (datagram). | B.Tech/placement: know the TCP socket lifecycle: socket → bind → listen → accept → connect → send/recv → close. |
| Signal | Asynchronous software interrupt / notification: kill(pid, SIGTERM). Process installs handler via sigaction() or ignores the signal. | Carries events, not bulk data payload. SIGCHLD alerts parent that a child terminated. |
✎ Takeaway: Shared memory = fastest (zero kernel copies) but needs sync. Message passing = safer (kernel copies). Pipe = unidirectional, related processes only.
3.The fork() System Call
§ 2.3.fork() creates an exact clone of the calling process. Child gets a new PID, copied virtual address space (Copy-on-Write), duplicated file descriptor table, and signal masks.
Return values: Parent receives child's PID (> 0). Child receives 0. Failure returns -1 in parent only.
Total processes after k fork() calls: 2^k (original + 2^k - 1 children), assuming no process terminates prematurely.
fork(), parent and child have INDEPENDENT copies of all variables. Mutations in one never affect the other.4. fork() Output Tracing
int x=5; fork(); x++; printf("%d", x);One process exists, with x = 5 in private stack.
Two processes (parent and child) exist, each with an independent copy of x = 5.
Both independently compute x = 6 in their separate address spaces.
Both execute printf("%d", x). Output: '6' printed twice.
5. GATE Lens
Syllabus focus, recurring numerical patterns, common traps, and clearly labelled exam practice. Questions remain source-pending until an official paper and answer key are attached.
IPC and fork() appear as 1-2 mark output-tracing MCQs and architecture-matching questions.
- •Count total vs new processes after a sequence of
fork()invocations. - •Trace
printf()output afterfork()— identify which lines execute in parent vs child. - •Match IPC mechanism to requirement: fastest → shared memory, unrelated processes → socket/FIFO.
- •
fork()returns TWICE: child PID in parent and 0 in child. Unconditional code runs in BOTH! - •Shared memory provides NO automatic synchronization; omitting semaphores creates race conditions.
- •After
fork(), file descriptors are inherited — unused pipe ends must be closed in both processes to avoid hanging on EOF.
Exam Practice / Source Verification Required
1 problemHow many NEW processes are created by: fork(); fork(); fork(); in a single-process program?
Concept Check
Which IPC mechanism requires explicit synchronization to prevent race conditions?
After fork(); fork(); how many total processes exist including the original?
7. 1-Minute Panic Revision
“Pipes: unidirectional byte stream between related processes. Shared Memory: fastest IPC (zero kernel copies), requires user sync. Message Passing: kernel-managed message queues with built-in sync.”
- •Total processes after n consecutive fork() calls = 2^n.
- •Total NEW child processes created = 2^n - 1.
- •fork() return values: > 0 (Child PID) in Parent; == 0 in Child; < 0 on failure.
- •pipe(fd): fd[0] is read end, fd[1] is write end.
- •Shared memory requires explicit synchronization (semaphores/mutexes). Forgetting sync causes race conditions.
- •Unclosed pipe ends cause processes to hang indefinitely waiting for EOF on read()!
- •Variables modified in child after fork() do NOT mutate the parent's memory (independent address spaces / Copy-On-Write).
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Official slides on shared memory vs message passing architectures, POSIX shared memory, and pipes.
The seminal systems programming bible covering pipe descriptor inheritance and POSIX shm API.
Direct lecture on IPC synchronization, race conditions, and message queue primitives.
If the textbook derivation did not click immediately, consult these targeted video lessons:
Direct walkthrough of fork() return values (0 vs child PID), process tree hierarchy, and print tracing.
Detailed whiteboard lecture breaking down independent vs cooperating processes, shared buffers, and message passing.
Curated lectures on Process Lifecycle, PCB Memory Layout, Fork() System Calls & Multi-threading Models:
Rapid intuitive mastery of process state transitions and state diagram numericals.
Clear breakdown of blocking I/O behavior, thread context switches, and kernel space mapping.
Visual tree diagram derivations of fork() return values and process hierarchies.
Exam trick cases on how many times printf() executes in tricky fork() loops.