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
| Mechanism | Direction | Speed | Type |
|---|---|---|---|
| Pipe | Unidirectional | Fast | Related processes only |
| FIFO (Named Pipe) | Unidirectional | Fast | Any processes |
| Message Queue | Bidirectional | Medium | Any processes |
| Shared Memory | Bidirectional | Fastest | Any processes |
| Socket | Bidirectional | Medium | Same or different machines |
| Signal | Unidirectional | Fast | Simple notification |
Process Synchronization
The Critical Section Problem
Critical Section: A code segment where a process accesses shared resources.
Requirements for a valid solution:
- Mutual Exclusion — Only one process in critical section at a time
- Progress — If no process is in critical section, one waiting should enter
- 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:
synchronizedkeyword implements monitor
synchronized(object) {
// Critical section
}
Classic Synchronization Problems
| Problem | Description |
|---|---|
| Producer-Consumer | Producer adds to buffer; consumer removes from buffer; need to coordinate |
| Readers-Writers | Multiple readers OK simultaneously; writers need exclusive access |
| Dining Philosophers | 5 philosophers, 5 forks; each needs 2 forks; potential deadlock |
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
Introduction to C Programming; C Program Structure; Data Types and Variables
C Programming
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.