Figure 3.1: Structure of a Process Entering and Exiting a Critical Section
Figure 3.1: Structure of a Process Entering and Exiting a Critical Section
Step 1Entry Section
Requests permission (lock) to enter the Critical Section
Step 2CRITICAL SECTION (CS)
Accesses / modifies shared variables (Only 1 process allowed inside at a time!)
Step 3Exit Section
Releases the lock so other waiting processes can enter
Step 4Remainder Section
All remaining non-shared code of the process
Explanation
- Entry code obtains permission before shared state is used.
- Exit code releases permission so another process can enter.
Figure 3.2: Binary Semaphore (Mutex) vs Counting Semaphore
Figure 3.2: Binary Semaphore (Mutex) vs Counting Semaphore
1. Binary Semaphore (Mutex Lock)
- Value can ONLY be
0or1(Initialized toS = 1) - Used to enforce strict Mutual Exclusion (only 1 process allowed in Critical Section)
S = 1means Critical Section is Free;S = 0means Locked
2. Counting Semaphore
- Value can range over an unrestricted integer domain (0 to N)
- Initialized to S = N (where N is the number of available identical resources, e.g., 5 Printers)
- Used to control access to a pool of multiple instances of a resource
Explanation
- A counting semaphore tracks several available permits and a binary semaphore tracks one.
- A mutex also has ownership semantics that a general semaphore does not require.
Figure 3.3: Classic Deadlock Between Two Processes (P1, P2) and Two Resources (R1, R2)
Figure 3.3: Classic Deadlock Between Two Processes (P1, P2) and Two Resources (R1, R2)
Process P1Holds Printer (R1)
→ Requests Scanner (R2) →
Process P2Holds Scanner (R2)
→ Requests Printer (R1) →
DEADLOCK!Circular Wait (P1 → R2 → P2 → R1 → P1)
Explanation
- Each process holds a resource that the other needs.
- Neither can advance to its release step, so the wait continues.
Figure 3.4: The 4 Necessary Conditions for Deadlock (All 4 Must Hold Simultaneously)
Figure 3.4: The 4 Necessary Conditions for Deadlock (All 4 Must Hold Simultaneously)
1. Mutual Exclusion
At least one resource is non-shareable (only 1 process can use it at a time)
2. Hold and Wait
A process is currently holding at least 1 resource AND waiting to acquire more
3. No Preemption
Resources cannot be forcibly snatched away; only released voluntarily by holder
4. Circular Wait
A closed chain of processes exists: P0 waits for P1, P1 waits for P2, ..., Pn waits for P0
Explanation
- Mutual exclusion, hold-and-wait, no preemption and circular wait must all be possible.
- Preventing any one of these conditions rules out this form of deadlock.
Figure 3.5: Vertices and Edges in a Resource-Allocation Graph (RAG)
Figure 3.5: Vertices and Edges in a Resource-Allocation Graph (RAG)
1. Request Edge (Pi → Rj)
- Arrow goes from Process Circle (Pi) → Resource Box (Rj)
- Meaning: Process Pi is asking and waiting for Resource Rj
2. Assignment Edge (Rj → Pi)
- Arrow goes from Resource Box (Rj) → Process Circle (Pi)
- Meaning: Resource Rj is currently held (owned) by Process Pi
Explanation
- A process-to-resource edge requests a resource and a resource-to-process edge assigns it.
- A cycle proves deadlock in the single-instance case.
Figure 3.6: The 4 Deadlock Handling Approaches in Operating Systems
Figure 3.6: The 4 Deadlock Handling Approaches in Operating Systems
1. Deadlock Prevention
Design rules so at least ONE of the 4 necessary conditions can never hold
2. Deadlock Avoidance
Check every request dynamically (Banker's Algorithm) to stay in a Safe State
3. Detection & Recovery
Allow deadlocks to happen, detect them periodically, and recover
4. Deadlock Ignorance
Ostrich Algorithm — Ignore rare deadlocks (Used by Windows & Linux for speed!)
Explanation
- Prevention removes a necessary condition and avoidance admits only safe requests.
- Detection permits deadlocks and triggers recovery, while some systems accept the risk without special handling.
Figure 3.7: Safe State, Unsafe State, and Deadlock State Spaces
Figure 3.7: Safe State, Unsafe State, and Deadlock State Spaces
Safe State (100% Deadlock-Free)
- A valid Safe Sequence < Pi, Pj, ... > exists
- Deadlock Avoidance algorithms (Banker's Algorithm) keep the system strictly inside this zone!
Unsafe State (Danger Zone)
- No Safe Sequence exists
- Deadlock is a subset inside the Unsafe State if processes request their maximum needs at once
Explanation
- A safe state has a sequence in which all processes can finish.
- An unsafe state lacks that guarantee but need not already be deadlocked.
Figure 3.8: Two Methods of Deadlock Detection in Operating Systems
Figure 3.8: Two Methods of Deadlock Detection in Operating Systems
Case 1: Single Instance of Each Resource
- Uses a Wait-For Graph (WFG)
- Obtained by removing Resource nodes from the Resource-Allocation Graph (RAG)
- Rule: A Deadlock exists if and only if the Wait-For Graph contains a Cycle!
Case 2: Multiple Instances of Each Resource
- Cycle in graph is NOT enough; uses a Banker's-style Detection Algorithm
- Uses matrices:
Available,Allocation, and currentRequest - Checks if Request[i] ≤ Work; any process that cannot finish is deadlocked!
Explanation
- A wait-for graph suits resources with one instance of each type.
- With multiple instances, the detector also needs allocation, request and available counts.
Figure 3.9: The Two Methods of Automatic Deadlock Recovery
Figure 3.9: The Two Methods of Automatic Deadlock Recovery
Method 1A: Abort ALL Deadlocked Processes
Instantly breaks the deadlock cycle, but loses all partial work done by every process!
Method 1B: Abort ONE Process at a Time
Kills 1 process, re-runs detection to see if cycle is broken, repeats until deadlock is gone
Method 2: Resource Preemption
Temporarily takes resources from a Victim process, rolls it back, and gives resources to others
Explanation
- Terminating a process releases its resources but loses some work.
- Preemption and rollback can recover resources when the system supports safe restoration.