Chapter 5: Process Synchronization and Concurrency
Syllabus 9.3 — Synchronization of Concurrent Processes
Synchronization is one of the most frequently tested Operating Systems topics in university exams, PSC examinations, GATE, and technical interviews.
1. What is Concurrency?
Concurrency means multiple processes or threads make progress during overlapping periods of time. They may not execute at the exact same instant (especially on a single-core CPU), but they appear to run simultaneously because the CPU switches rapidly between them.
Example: Downloading a file, listening to Spotify, editing a Word document, and browsing Chrome — these appear simultaneous. That is concurrency.
Concurrency vs Parallelism
| Feature | Concurrency | Parallelism |
|---|---|---|
| Execution | Overlapping (interleaved) | Simultaneous |
| CPU Requirement | Can work on one CPU (via context switching) | Requires multiple cores / processors |
| Example | One CPU switching among Task A, B, C | Core 1 → Task A, Core 2 → Task B |
2. Shared Resources
Processes often share resources such as: Printer, Memory, Files, Databases, Variables. Sharing resources introduces synchronization problems.
Example: Two students printing to one printer simultaneously. Without synchronization, pages become mixed. The OS must coordinate access.
3. Race Condition
A race condition occurs when two or more processes access shared data simultaneously and the final result depends on the order of execution.
| ATM Example | |
|---|---|
| Bank Balance | 1000 |
| ATM A reads balance | 1000 |
| ATM B reads balance | 1000 |
| ATM A withdraws 500, writes | 500 |
| ATM B withdraws 500, writes | 500 (overwrites A’s update) |
| Expected balance | 0 |
📌 Definition: A race condition occurs when multiple processes access shared data concurrently and at least one modifies it, producing unpredictable results.
4. Critical Section
A critical section is the portion of a program where shared resources are accessed. Only one process should execute inside the critical section at a time.
Example: balance = balance - amount; — this line updates shared data, so it belongs to the critical section.
Structure of a Process
| Section | Purpose |
|---|---|
| Entry Section | Requests permission to enter the critical section |
| Critical Section | Accesses and modifies shared resources |
| Exit Section | Releases the resource / signals others |
| Remainder Section | Normal execution not involving shared resources |
5. Requirements of a Good Synchronization Solution
Three classic requirements that every valid critical-section solution must satisfy:
| Requirement | Definition | Example |
|---|---|---|
| 1. Mutual Exclusion | Only one process may enter the critical section at a time | One printer — only one user prints, others wait |
| 2. Progress | If no process is inside the critical section, one waiting process must be selected without unnecessary delay | System does not freeze when critical section is free |
| 3. Bounded Waiting | Every waiting process must eventually get a chance — no starvation | No process waits forever to enter |
📌 Memory Trick — MPB: M → Mutual Exclusion | P → Progress | B → Bounded Waiting
6. Synchronization Methods
A. Mutex (Mutual Exclusion Lock)
A mutex is a lock that allows only one process or thread to enter the critical section at a time.
| Aspect | Detail |
|---|---|
| Working | LOCK ↓ Critical Section ↓ UNLOCK |
| State | Binary: Locked / Unlocked |
| Ownership | Only one owner (the thread that locked it) |
| Characteristics | Simple, fast, binary only |
Example: Thread 1 LOCKs → updates data → UNLOCKs. Thread 2 waits until the lock is released.
B. Semaphore
A semaphore is an integer variable used to control access to shared resources.
| Type | Values | Use Case |
|---|---|---|
| Binary Semaphore | 0 or 1 | Works similarly to a mutex |
| Counting Semaphore | 0, 1, 2, 3, … | Allows multiple processes (e.g., N resources available) |
| Operation | Also Called | Effect |
|---|---|---|
| wait() | P(), down() | Decreases semaphore. If value becomes < 0, process waits. |
| signal() | V(), up() | Increases semaphore. Wakes a waiting process if any. |
Example: Semaphore = 1 → Thread A calls wait() → enters Critical Section → calls signal() → Thread B can now enter.
Mutex vs Semaphore
| Feature | Mutex | Semaphore |
|---|---|---|
| Type | Locking mechanism | Signaling mechanism |
| Ownership | Only the locking thread can unlock | No ownership requirement |
| Values | Binary only (0 or 1) | Binary or counting |
| Complexity | Simpler | More flexible |
| Use Case | Protecting a single shared resource | Managing multiple resource instances |
C. Monitor
A monitor is a high-level synchronization construct that automatically provides mutual exclusion. Programmers do not manually lock or unlock.
| Advantages |
|---|
| Safer — automatic mutual exclusion |
| Easier to use than semaphores |
| Less error-prone |
D. Spinlock
A spinlock repeatedly checks whether a lock is available (busy-waits) instead of putting the process to sleep.
| Advantages | Disadvantages |
|---|---|
| Very fast for very short waiting times | Wastes CPU time while spinning |
| No context switch overhead | Inefficient for long waits |
7. Busy Waiting
Busy waiting means continuously checking a condition instead of sleeping.
Example: while(lock == 1) { } — CPU keeps running this loop.
| Advantages | Disadvantages |
|---|---|
| Simple to implement | Wastes CPU cycles unnecessarily |
| No sleep/wake overhead | Poor for long waits |
8. Classical Synchronization Problems
These are famous problems used in exams to test understanding of synchronization.
| Problem | Description | Synchronization Need |
|---|---|---|
| Producer–Consumer | Producer produces data into a buffer; Consumer consumes from it | Prevent buffer overflow and underflow; coordinate access |
| Reader–Writer | Multiple readers can read simultaneously; writers require exclusive access | Allow concurrent reads; exclusive writes |
| Dining Philosophers | Philosophers alternately think and eat using shared forks | Prevent deadlock and starvation among competing processes |
📌 Memory Trick — PRD: P → Producer–Consumer | R → Reader–Writer | D → Dining Philosophers
9. Deadlock (Introduction)
Deadlock occurs when two or more processes wait indefinitely for resources held by each other, and none can proceed.
| Deadlock Example | |
|---|---|
| P1 | Needs Printer, currently holding Scanner |
| P2 | Needs Scanner, currently holding Printer |
| Result | Neither process can proceed — deadlock |
10. Starvation
A process waits indefinitely because other processes continuously receive resources first.
Example: High-priority jobs always execute; a low-priority job never gets the CPU.
11. Livelock
Processes keep changing their states in response to each other but make no real progress.
Example: Two people step left to avoid each other, then both step right, repeating forever — both are moving but neither passes.
| Feature | Deadlock | Starvation | Livelock |
|---|---|---|---|
| Movement | Nobody moves | Others move; one waits forever | Everyone moves but no progress |
| Type | Permanent waiting | Unfair scheduling | Endless futile activity |
Practical Examples
| Real-World Scenario | Synchronization Need |
|---|---|
| ATM & Shared Database | Concurrent withdrawals must be serialized |
| Printer | Only one process prints at a time |
| Railway Signal | Only one train enters a track section |
| Bank Account | Concurrent deposits/withdrawals require locking |
| Online Shopping (last item) | Two customers buying simultaneously — without sync, inventory becomes negative |
Memory Tricks
| Mnemonic | Full Form |
|---|---|
| Critical Section Requirements — MPB |
M → Mutual Exclusion P → Progress B → Bounded Waiting |
| Semaphore Operations — WS |
W → wait() = Enter (decrement) S → signal() = Leave (increment) |
| Classical Problems — PRD |
P → Producer–Consumer R → Reader–Writer D → Dining Philosophers |
Frequently Asked Exam Questions
| Question | Answer |
|---|---|
| What is a race condition? | Multiple processes accessing shared data simultaneously, leading to unpredictable results |
| What is a critical section? | Part of a program where shared resources are accessed |
| State the three critical-section requirements. | Mutual Exclusion, Progress, Bounded Waiting |
| Difference between Mutex and Semaphore? | Mutex = locking (one owner); Semaphore = signaling (no ownership, can be counting) |
| What are semaphore operations? | wait() and signal() |
| Name three classical synchronization problems. | Producer–Consumer, Reader–Writer, Dining Philosophers |
| Difference between deadlock and starvation? | Deadlock: nobody proceeds. Starvation: others proceed, one waits forever. |
Practice MCQs
-
1. Which condition occurs when multiple processes simultaneously modify shared data?
A. Deadlock B. Paging C. Race Condition D. Thrashing
✅ Answer: C — Race Condition -
2. Which synchronization requirement ensures only one process enters the critical section?
A. Progress B. Mutual Exclusion C. Scheduling D. Throughput
✅ Answer: B — Mutual Exclusion -
3. Which synchronization primitive uses wait() and signal() operations?
A. Mutex B. Semaphore C. Monitor D. PCB
✅ Answer: B — Semaphore -
4. Which synchronization construct automatically provides mutual exclusion?
A. Spinlock B. Monitor C. Semaphore D. Interrupt
✅ Answer: B — Monitor -
5. Which is NOT one of the three critical-section requirements?
A. Mutual Exclusion B. Progress C. Bounded Waiting D. Paging
✅ Answer: D — Paging