Unit 2 · Operating Systems
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.
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.
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.
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.
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.
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!
Deadlock - 4 Conditions
Banker's Algorithm
Semaphore
Monitors & Handling
| Aspect | Semaphore | Monitor |
|---|---|---|
| Level | Low-level | High-level |
| Ops | Manual P/V | Condition wait/signal inside |
| Access | Multiple if counting | Only one at a time auto mutual exclusion |
| Safety | Risk race | Safer Java synchronized |
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