Operating Systems

Inter-Process Communication (IPC); Process Synchronization

C-CAT

Inter-Process Communication (IPC)

What is IPC?

IPC (Inter-Process Communication) mechanisms allow processes to communicate and synchronize.

Two Models of IPC

19.1 Shared Memory Model

  • Processes share a common memory region
  • Both processes can read and write to the shared memory
  • Fast — no OS involvement after setup
  • Risk: Need synchronization to avoid race conditions
Process A      Process B
    |               |
    +→ Shared ←+    |
       Memory         (both access same region)

APIs: shmget(), shmat(), shmdt(), shmctl() (POSIX)

19.2 Message Passing Model

  • Processes communicate by sending and receiving messages
  • OS manages the communication channel
  • Slower than shared memory (OS involvement for every message)
  • Safer — no shared state; no synchronization issues
Process A → [Message Queue/Pipe] → Process B
            (OS manages this)

APIs:

  • Pipes — pipe() (anonymous) or named pipes (FIFO)
  • Message Queues — msgget(), msgsnd(), msgrcv()
  • Sockets — socket(), send(), recv()

Signals — kill(), signal()

IPC Mechanisms Comparison

MechanismDirectionSpeedType
PipeUnidirectionalFastRelated processes only
FIFO (Named Pipe)UnidirectionalFastAny processes
Message QueueBidirectionalMediumAny processes
Shared MemoryBidirectionalFastestAny processes
SocketBidirectionalMediumSame or different machines
SignalUnidirectionalFastSimple notification

Process Synchronization

The Critical Section Problem

Critical Section: A code segment where a process accesses shared resources.

Requirements for a valid solution:

  1. Mutual Exclusion — Only one process in critical section at a time
  2. Progress — If no process is in critical section, one waiting should enter
  3. Bounded Waiting — Process should not wait indefinitely (no starvation)

Race Condition

A race condition occurs when multiple processes access and manipulate shared data concurrently and the outcome depends on execution order.

Example:

Shared: count = 5

Process A reads count=5 → count++  → count=6
Process B reads count=5 → count++  → count=6

Expected: count=7, Got: count=6  ← RACE CONDITION!

Synchronization Solutions

20.1 Mutex (Mutual Exclusion Lock)

pthread_mutex_t lock;
pthread_mutex_lock(&lock);     // Acquire lock
// Critical section
pthread_mutex_unlock(&lock);   // Release lock
  • Only one thread can hold mutex at a time
  • Other threads block waiting for the lock

20.2 Semaphore

A semaphore is an integer variable with two atomic operations:

  • wait(S) / P(S): S--; if S<0 then block
  • signal(S) / V(S): S++; if S<=0 then wake up process

Binary Semaphore (Mutex): Value is 0 or 1 Counting Semaphore: Value is N (controls N resources)

semaphore S = 1;  // Binary semaphore

// Process 1:
wait(S);          // P: S=0; enter critical section
// Critical section
signal(S);        // V: S=1; release

// Process 2:
wait(S);          // P: S=-1; blocked until P1 signals

20.3 Monitors

  • High-level synchronization construct (in Java, C#)
  • Only one thread can execute inside monitor at a time
  • Java: synchronized keyword implements monitor
synchronized(object) {
    // Critical section
}

Classic Synchronization Problems

ProblemDescription
Producer-ConsumerProducer adds to buffer; consumer removes from buffer; need to coordinate
Readers-WritersMultiple readers OK simultaneously; writers need exclusive access
Dining Philosophers5 philosophers, 5 forks; each needs 2 forks; potential deadlock

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.