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

FeatureConcurrencyParallelism
ExecutionOverlapping (interleaved)Simultaneous
CPU RequirementCan work on one CPU (via context switching)Requires multiple cores / processors
ExampleOne CPU switching among Task A, B, CCore 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 Balance1000
ATM A reads balance1000
ATM B reads balance1000
ATM A withdraws 500, writes500
ATM B withdraws 500, writes500 (overwrites A’s update)
Expected balance0

📌 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

SectionPurpose
Entry SectionRequests permission to enter the critical section
Critical SectionAccesses and modifies shared resources
Exit SectionReleases the resource / signals others
Remainder SectionNormal execution not involving shared resources

5. Requirements of a Good Synchronization Solution

Three classic requirements that every valid critical-section solution must satisfy:

RequirementDefinitionExample
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.

AspectDetail
WorkingLOCK ↓ Critical Section ↓ UNLOCK
StateBinary: Locked / Unlocked
OwnershipOnly one owner (the thread that locked it)
CharacteristicsSimple, 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.

TypeValuesUse Case
Binary Semaphore0 or 1Works similarly to a mutex
Counting Semaphore0, 1, 2, 3, …Allows multiple processes (e.g., N resources available)
OperationAlso CalledEffect
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

FeatureMutexSemaphore
TypeLocking mechanismSignaling mechanism
OwnershipOnly the locking thread can unlockNo ownership requirement
ValuesBinary only (0 or 1)Binary or counting
ComplexitySimplerMore flexible
Use CaseProtecting a single shared resourceManaging 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.

AdvantagesDisadvantages
Very fast for very short waiting timesWastes CPU time while spinning
No context switch overheadInefficient 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.

AdvantagesDisadvantages
Simple to implementWastes CPU cycles unnecessarily
No sleep/wake overheadPoor for long waits

8. Classical Synchronization Problems

These are famous problems used in exams to test understanding of synchronization.

ProblemDescriptionSynchronization 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
P1Needs Printer, currently holding Scanner
P2Needs Scanner, currently holding Printer
ResultNeither 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.

FeatureDeadlockStarvationLivelock
MovementNobody movesOthers move; one waits foreverEveryone moves but no progress
TypePermanent waitingUnfair schedulingEndless futile activity

Practical Examples

Real-World ScenarioSynchronization Need
ATM & Shared DatabaseConcurrent withdrawals must be serialized
PrinterOnly one process prints at a time
Railway SignalOnly one train enters a track section
Bank AccountConcurrent deposits/withdrawals require locking
Online Shopping (last item)Two customers buying simultaneously — without sync, inventory becomes negative

Memory Tricks

MnemonicFull 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

QuestionAnswer
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