← Computer Science notes

Unit 2 · Operating Systems

Chapters 3, 4 · Deadlocks & Concurrency Control

pdcampus.in
pdcampus.in • DSSSB TGT CS
Beginner Topper
Deadlock & Concurrency Control Unit 2 • Ch 3 • 4 Conditions, Banker's, Semaphore, Monitors, RAG • Most PYQ
Beginner Story Mode Complete Notes

Imagine two friends, Ram and Shyam. Ram has pen, needs notebook. Shyam has notebook, needs pen. Both hold one and wait for other. Nobody gives. Both stuck forever. That's Deadlock - two processes waiting for resources held by each other, circular wait, no progress.

Concurrency Control is how to prevent such fights when multiple processes access shared resources. Like canteen has 1 spoon, 5 students want it - how to give safely without deadlock? Use Semaphore = Traffic Signal (red=wait, green=go), Monitor = Manager room (only one student allowed inside at a time, automatic lock).

Banker's Algorithm is like a banker who never gives loan if loan may lead to deadlock. Banker checks: If I give money to Ram, will all customers still be able to finish safely? Only gives if safe sequence exists.

Let's learn full chapter with stories so DSSSB 5 marks are fixed.

1. Deadlock - 4 Necessary Conditions
Deadlock 4 Conditions handwritten
Fig 1: 4 Conditions - Mutual Exclusion, Hold & Wait, No Preemption, Circular Wait - All 4 needed

Deadlock happens only if ALL 4 conditions happen together. If you break any one, deadlock cannot happen. That's how prevention works.

1) Mutual Exclusion: Resource can be used by only one process at a time. Like 1 pen can be used by 1 student at a time. If resource can be shared (read-only file), no deadlock. Example: Printer is mutually exclusive.

2) Hold and Wait: Process holds at least one resource and waits for another. Ram holds pen waits notebook. Shyam holds notebook waits pen.

3) No Preemption: Resource cannot be forcibly taken away. Only holder can release voluntarily. You cannot snatch pen.

4) Circular Wait: Circular chain P1 waits P2, P2 waits P3, P3 waits P1. Circle forms.

If any one broken, no deadlock. Prevention idea.

Deadlock vs Starvation vs Livelock:
- Deadlock: Stuck circular wait no progress needs 4 conditions.
- Starvation: Waits indefinitely low priority keeps losing, solved by aging.
- Livelock: Active but no progress like two people in corridor both move left then right keep moving no one passes.

Deadlock needs ALL 4: Mutual Exclusion + Hold & Wait + No Preemption + Circular Wait | Break any one → No Deadlock
2. Banker's Algorithm - Safe Sequence
Banker's Algorithm Safe Sequence handwritten
Fig 2: Banker's Algorithm - Need = Max - Allocation, Find Need ≤ Available, Safe Sequence P1→P3→P4→P0→P2

Banker checks before loan: If I give this loan, will all customers still finish safely? If yes give, else wait. That's Banker's Algorithm - avoids deadlock by staying in safe state.

Data Structures:
- Available: Free resources now [3,3,2] means 3 A 3 B 2 C free.
- Max: Max demand each process may need total P0 Max [7,5,3].
- Allocation: Currently allocated P0 [0,1,0].
- Need = Max - Allocation: Still needed [7,4,3].

Steps Safety Algorithm:
1) Work=Available Finish=false all.
2) Find i where Finish false AND Need ≤ Work.
3) If found pretend finish: Work += Allocation[i] Finish true add to safe sequence repeat.
4) All Finish true → Safe State safe sequence exists no deadlock.
5) Cannot find any Need≤Work and some Finish false → Unsafe may lead to deadlock.

Example from Fig 2:
Available [3,3,2] Need P0[7,4,3] P1[1,2,2] P2[6,0,0] P3[0,1,1] P4[4,3,1]
Start Work [3,3,2] - P1 Need [1,2,2]≤[3,3,2] run P1 Work [5,3,2] Safe P1
P3 Need [0,1,1]≤[5,3,2] run P3 Work [7,4,3] Safe P1→P3
P4 Need [4,3,1]≤[7,4,3] run P4 Work [7,4,5] Safe P1→P3→P4
P0 Need [7,4,3]≤[7,4,5] run P0 Work [7,5,5] Safe P1→P3→P4→P0
P2 Need [6,0,0]≤[7,5,5] run P2 Work [10,5,7] Safe P1→P3→P4→P0→P2 All finished Safe.

Resource Request Algorithm: When P requests Request[] - If Request>Need error, If Request>Available wait, else pretend grant Available-=Request Allocation+=Request Need-=Request run Safety if safe grant else restore deny.

Safe=sequence exists all can finish no deadlock, Unsafe=no sequence now may lead to deadlock, Deadlock=stuck circular.

Need = Max - Allocation | Safe: Find Need ≤ Available Work+=Allocation repeat | All Finish true → Safe Sequence
3. Semaphore - Traffic Signal
Semaphore Traffic Signal handwritten
Fig 3: Semaphore wait() P() down() if S>0 S-- else block, signal() V() up() S++ wake one | Binary 0/1 mutex vs Counting 0..N

Canteen has 1 spoon shared. 5 students want spoon. How to ensure only 1 uses at a time? Use Semaphore = Traffic Signal.

What is Semaphore? Integer variable with wait() and signal().
- wait() / P() / down(): If value >0 decrement allow enter critical section. If 0 process waits blocked. Red signal wait.
- signal() / V() / up(): Increment wake one waiting. Green signal go.

Types:
Binary Semaphore 0/1 mutex lock only 1 process in critical section. Counting Semaphore 0..N multiple instances e.g., 5 printers count 5.

Classic Producer-Consumer: Producer produces into buffer Consumer consumes. Buffer finite N. Need 3 semaphores: empty N (empty slots), full 0 (full slots), mutex 1 (mutual exclusion).
Producer: wait(empty) wait(mutex) produce into buffer signal(mutex) signal(full)
Consumer: wait(full) wait(mutex) consume signal(mutex) signal(empty)
Prevents overflow empty=0 wait and underflow full=0 wait.

Semaphore has value + queue waiting processes. wait() value 0 block queue, signal() wake one.
Problems: Low-level manual wait/signal forget or swap → deadlock race hard debug.

wait() = P() = down() : if S>0 S-- else block | signal() = V() = up() : S++ wake one | Binary 0/1 mutex Counting 0..N resources
4. Monitors & Deadlock Handling Strategies
Monitors Deadlock Handling handwritten
Fig 4: Monitor high-level automatic mutual exclusion vs Semaphore low-level | Prevention Avoidance Detection Recovery | RAG cycle deadlock

Monitor = Manager Room: Only one student allowed inside at a time automatic lock no manual lock needed. Condition variables wait() signal() inside monitor. Process inside can wait on condition releasing monitor so other can enter. Signal wakes waiting.

Semaphore vs Monitor: Semaphore low-level manual P/V multiple can be inside if counting risk race. Monitor high-level abstraction condition wait/signal inside only one at a time automatic mutual exclusion safer Java synchronized.

Deadlock Handling 4 Strategies:

1) Prevention - Break one of 4: Make shareable read-only, Request all at once or release all if needs new, Allow preemption snatch resource CPU memory but not printer, Number resources request increasing order break circular wait Pen=1 Notebook=2 all must request Pen first.

2) Avoidance - Banker's safe state: Only grant if stays safe. Check safe sequence before grant. If unsafe wait. Always safe no deadlock but needs Max in advance overhead.

3) Detection - RAG cycle: Allow deadlock then detect periodically. RAG process circle resource square Edge P→R request R→P assignment cycle deadlock if single instance per resource multiple instances cycle may not always deadlock. Example P1 holds R1 requests R2 P2 holds R2 requests R1 cycle P1→R2→P2→R1→P1 deadlock.

4) Recovery - Kill or preempt: Kill one or more victim low priority least work to break cycle. Preempt resource forcibly take rollback retry. Ostrich Algorithm ignore deadlock pretend never happens used in Unix deadlock rare cost high reboot.

Prevention break 1 of 4 | Avoidance Banker's safe | Detection RAG cycle | Recovery kill/preempt | RAG R→P assignment P→R request cycle ⇒ deadlock
5. DSSSB Traps & PYQ
  • Q: 4 necessary conditions? → Mutual Exclusion Hold & Wait No Preemption Circular Wait all 4 together
  • Q: Prevent deadlock? → Break any one of 4
  • Q: Banker's for? → Deadlock Avoidance safe state
  • Q: Need formula? → Need = Max - Allocation
  • Q: Safe vs Unsafe vs Deadlock? → Safe sequence exists all finish, Unsafe no sequence may lead deadlock, Deadlock stuck circular
  • Q: Safe sequence? → Find Need≤Available Work+=Allocation repeat all Finish true P1→P3→P4→P0→P2
  • Q: wait() signal()? → wait P down decrement if >0 else block, signal V up increment wake one
  • Q: Binary vs Counting? → Binary 0/1 mutex 1 process, Counting 0..N N instances
  • Q: Producer-Consumer semaphores? → empty N full 0 mutex 1 Producer wait empty mutex produce signal mutex full Consumer wait full mutex consume signal mutex empty
  • Q: Monitor vs Semaphore? → Monitor high-level auto mutual exclusion only one inside safer, Semaphore low-level manual risk
  • Q: RAG cycle means? → Single instance per resource cycle deadlock, multiple instances cycle may not always deadlock
  • Q: Deadlock handling? → Prevention break 1 Avoidance Banker's Detection RAG Recovery kill/preempt
  • Q: Ostrich? → Ignore deadlock pretend never happens Unix rare
Deadlock ALL 4 | Banker's Need=Max-Alloc Safe Need≤Available | Semaphore wait=P down signal=V up | Monitor auto | RAG cycle deadlock

Final Story Recap: Ram pen Shyam notebook hold one wait other circular wait deadlock. Needs all 4: Mutual Exclusion one at a time, Hold & Wait hold one wait other, No Preemption cannot snatch, Circular Wait circle. Break any one no deadlock. Banker checks safe: Available [3,3,2] Need=Max-Alloc find Need≤Available P1 [1,2,2]≤[3,3,2] Work [5,3,2] P3 [0,1,1]≤[5,3,2] Work [7,4,3] P4 [4,3,1]≤[7,4,3] Work [7,4,5] P0 [7,4,3]≤[7,4,5] Work [7,5,5] P2 [6,0,0]≤[7,5,5] safe P1→P3→P4→P0→P2 safe. Semaphore traffic signal wait() red S>0 S-- else block signal() green S++ wake Binary 0/1 mutex 1 spoon Counting 0..N N printers Producer-Consumer empty N full 0 mutex 1 Producer wait empty mutex produce signal mutex full Consumer wait full mutex consume signal mutex empty. Monitor manager room only one inside automatic condition wait signal safer than semaphore manual. Deadlock handling: Prevention break one shareable request all at once allow preemption number resources increasing order, Avoidance Banker's safe only grant if safe, Detection RAG P→R request R→P assignment cycle deadlock, Recovery kill victim low priority or preempt rollback, Ostrich ignore rare. If you remember 4 conditions and Need formula and wait/signal and RAG cycle DSSSB 5 marks fixed!

Topper Revision 1 Page = Full Chapter

Deadlock - 4 Conditions

  • Mutual Exclusion one at a time printer, Hold & Wait hold one wait other, No Preemption cannot snatch, Circular Wait P1→P2→P3→P1 circle
  • All 4 needed together, break any one → no deadlock
  • Starvation indefinite low priority, Livelock active but no progress moving left right

Banker's Algorithm

Need = Max - Allocation | Safety: Find Need ≤ Work Work+=Allocation repeat | All Finish true → Safe Sequence
  • Available free Max max demand Allocation currently held Need still needed
  • Safety: Work=Available Finish false all find Need≤Work Work+=Allocation Finish true repeat all true safe
  • Request: If Request>Need error If Request>Available wait else pretend grant Available-=Request Allocation+=Request Need-=Request run Safety if safe grant else restore deny
  • Safe sequence exists all can finish no deadlock, Unsafe no sequence may lead deadlock, Deadlock stuck circular

Semaphore

wait() P() down(): if S>0 S-- else block | signal() V() up(): S++ wake one | Binary 0/1 mutex | Counting 0..N resources
  • Binary 0/1 mutex lock only 1 in critical section
  • Counting 0..N N instances
  • Producer-Consumer empty N full 0 mutex 1 Producer wait empty mutex produce signal mutex full Consumer wait full mutex consume signal mutex empty
  • Queue waiting low-level manual risk deadlock if forget

Monitors & Handling

AspectSemaphoreMonitor
LevelLow-levelHigh-level
OpsManual P/VCondition wait/signal inside
AccessMultiple if countingOnly one at a time auto mutual exclusion
SafetyRisk raceSafer Java synchronized
  • Prevention: Break one of 4 shareable request all at once allow preemption number increasing order break circular
  • Avoidance: Banker's safe only grant if safe
  • Detection: RAG cycle P→R request R→P assignment cycle ⇒ deadlock if single instance
  • Recovery: Kill victim low priority least work or preempt rollback
  • Ostrich ignore deadlock Unix rare cost high

PYQ Traps:
• 4 conditions all needed
• Need = Max - Alloc
• Safe Need ≤ Available
• wait = P down signal = V up
• Binary 0/1 Counting 0..N
• Producer-Consumer empty N full 0 mutex 1
• Monitor auto mutual exclusion
• RAG cycle deadlock single instance
• Prevention break one Avoidance Banker's Detection RAG Recovery kill/preempt

© 2026 pdcampus.in · All rights reserved. · Made with ❤️ for Indian students.