OPERATING SYSTEMS/Student Engineering Notebook
Page 06·notes
PROCESSES, THREADS & CONCURRENCY—~20 MIN READ·GATE CS LENS

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

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 / AspectMechanismMechanics & GATE Significance
Unnamed PipeCreated 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 Memoryshmget() + 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 Queuemsgsnd() / 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.
SocketEndpoint 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.
SignalAsynchronous 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

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.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: After fork(), parent and child have INDEPENDENT copies of all variables. Mutations in one never affect the other.

4. fork() Output Tracing

Problem Statement:
Trace the output: int x=5; fork(); x++; printf("%d", x);
Step 1: Before fork()

One process exists, with x = 5 in private stack.

Step 2: After fork()

Two processes (parent and child) exist, each with an independent copy of x = 5.

Step 3: Both execute x++

Both independently compute x = 6 in their separate address spaces.

Step 4: Both print

Both execute printf("%d", x). Output: '6' printed twice.

Final Answer:6 printed twice.
GATE LENS · EXAM PREPARATION

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.

EXAM WEIGHTAGE & FOCUS:

IPC and fork() appear as 1-2 mark output-tracing MCQs and architecture-matching questions.

recurring exam patterns:
  • •Count total vs new processes after a sequence of fork() invocations.
  • •Trace printf() output after fork() — identify which lines execute in parent vs child.
  • •Match IPC mechanism to requirement: fastest → shared memory, unrelated processes → socket/FIFO.
⚠ common student traps:
  • •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 problem
GATE-style question · 1 Mark

How many NEW processes are created by: fork(); fork(); fork(); in a single-process program?

3
6
7
8
STUDENT PRACTICE · CONCEPT DRILL

Concept Check

Problem 1 · FOUNDATION

Which IPC mechanism requires explicit synchronization to prevent race conditions?

Problem 2 · GATE-LEVEL

After fork(); fork(); how many total processes exist including the original?

processes
RAPID REVIEW · NOTEBOOK SUMMARY

7. 1-Minute Panic Revision

core principle:

“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.”

must-remember principles:
  • •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.
critical exam pitfalls:
  • •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).
decision flow & invariants:
[ IPC MECHANISMS ARCHITECTURE SPECTRUM ] SHARED MEMORY (Fastest) MESSAGE PASSING (Safe) Process A Process B Process A Process B │ │ │ │ └───►[ Shared ]◄───┘ └──►[ Kernel Queue ]──►┘ [ Memory ] [ Message Buffer ] • Zero kernel copies • System call per message • User must synchronize • Built-in kernel synchronization • High throughput • Ideal for distributed systems
SOURCE TRANSPARENCY & CITATIONS

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.

Primary Academic Texts
Operating System Concepts (10th Edition)↗by Silberschatz, Galvin, Gagne · Chapter 3: Processes (Section 3.4: Interprocess Communication)

Official slides on shared memory vs message passing architectures, POSIX shared memory, and pipes.

ACADEMIC STANDARD
Advanced Programming in the UNIX Environment (3rd Edition)↗by W. Richard Stevens, Stephen A. Rago · Chapter 14 & 15: Advanced I/O and Interprocess Communication (Pipes, FIFOs, Shared Memory)

The seminal systems programming bible covering pipe descriptor inheritance and POSIX shm API.

ACADEMIC STANDARD
Curated University Lecture Series
NPTEL: Operating Systems↗Prof. Santanu Chattopadhyay (IIT Kharagpur) · Interprocess Communication & Shared Memory Architecture

Direct lecture on IPC synchronization, race conditions, and message queue primitives.

LECTURE SERIES
ALTERNATIVE PEDAGOGICAL PERSPECTIVES

If the textbook derivation did not click immediately, consult these targeted video lessons:

Fork System Call with Example in Operating SystemGate Smashers (Varun Singla) (13 mins)

Direct walkthrough of fork() return values (0 vs child PID), process tree hierarchy, and print tracing.

Watch Lesson↗
Inter-Process Communication (IPC)Neso Academy (14 mins)

Detailed whiteboard lecture breaking down independent vs cooperating processes, shared buffers, and message passing.

Watch Lesson↗
RECOMMENDED YOUTUBE LECTURES & EDUCATORS: MODULE 2: PROCESSES, THREADS & CONCURRENCY

Curated lectures on Process Lifecycle, PCB Memory Layout, Fork() System Calls & Multi-threading Models:

Gate Smashers · L-1.5: Process States in OS & Schedulers↗Instructor: Varun Singla · Covers: 5-State vs 7-State Process Life Cycle, Suspended states, Long/Medium/Short-term schedulers

Rapid intuitive mastery of process state transitions and state diagram numericals.

Open YouTube↗
Gate Smashers · L-1.12: User Level vs Kernel Level Threads↗Instructor: Varun Singla · Covers: User-Level Threads (ULT) vs Kernel-Level Threads (KLT), TCB structure, Concurrency models

Clear breakdown of blocking I/O behavior, thread context switches, and kernel space mapping.

Open YouTube↗
Neso Academy · fork() and exec() System Calls Walkthrough↗Instructor: Neso Academy · Covers: fork() execution trees, Return values (PID > 0, PID == 0), Zombie and Orphan processes

Visual tree diagram derivations of fork() return values and process hierarchies.

Open YouTube↗
Knowledge Gate · Process vs Thread & Fork GATE Numericals↗Instructor: Sanchit Jain · Covers: GATE PYQ derivations on nested fork() loops, shared vs private thread resources, PCB fields

Exam trick cases on how many times printf() executes in tricky fork() loops.

Open YouTube↗