Courses / HEC NSCT – National Skill Competency Test: Complete Preparation for Computing Graduates
9.3 Synchronization and Deadlocks
Area 9 – Operating Systems (5%) · 45 min read · 8 MCQs to check yourself
Critical section problem
When threads/processes share data, a race condition gives results that depend on timing. The critical section (code touching shared data) needs:
- Mutual exclusion – one at a time.
- Progress – a decision is made if no one is inside.
- Bounded waiting – no one waits forever.
Tools
- Mutex lock – acquire before the critical section, release after.
- Semaphore – integer with atomic
wait()(P, decrement, block if < 0) andsignal()(V, increment). Binary semaphore (0/1) ≈ mutex; counting semaphore controls a pool of N resources. - Monitor – high-level construct (Java
synchronized) with condition variables. - Classic problems: producer–consumer (bounded buffer), readers–writers, dining philosophers.
- Busy waiting = spinlock (wastes CPU).
Deadlock
A set of processes each waiting for a resource held by another. All four Coffman conditions must hold:
- Mutual exclusion 2. Hold and wait 3. No pre-emption 4. Circular wait. Handling: prevention (break one condition, e.g. order resources to stop circular wait), avoidance (Banker's algorithm grants a request only if the system stays in a safe state), detection and recovery (wait-for graph, kill/roll back a process), or ignore it (ostrich approach – common in desktop OSs). Starvation = waiting indefinitely (not necessarily a deadlock); livelock = processes keep changing state but make no progress.
Deadlock concept and necessary conditions (Gate Smashers)
This lesson ends with a quiz of 8 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 9.2 CPU Scheduling · Next: 9.4 Memory, Virtual Memory and File Systems