Operating Systems ยท Unit 3 ยท 6 hrs
Process Deadlocks
Exam-focused notes for Process Deadlocks (Operating Systems, CSC264): what the TU syllabus asks and how it has actually been tested, with 10 solved past questions from this unit.
Process Deadlocks in short
This chapter studies deadlock: the state in which a set of processes are all permanently blocked because each one is holding a resource that another process in the set needs, and each is waiting for a resource that yet another process in the set holds. It develops the conditions under which deadlock can arise, the graph used to reason about it, and the four practical strategies operating systems use to deal with it: ignore it, prevent it, avoid it, or detect and recover from it.
What this unit covers
- Introduction
- Deadlock Characterization
- Preemptable and Non-preemptable Resources
- Resource - Allocation Graph
- Conditions for Deadlock
- Handling Deadlocks: Ostrich Algorithm, Deadlock prevention, Deadlock Avoidance, Deadlock Detection (For Single and Multiple Resource Instances), Recovery From Deadlock (Through Preemption and Rollback)
Handling Deadlocks
2081 ยท 5 marks
How do you think deadlock can be avoided? Explain. [5]
2078 ยท 10 marks
How unsafe state differs from deadlocked state? Consider the following initial state and identify whether requested is granted or denied for the given cases. What will happen if process D requests 1 resource? What will happen if process A requests 1 resource?
| Process | Has | Max |
|---|---|---|
| A | 2 | 6 |
| B | 1 | 5 |
| C | 2 | 3 |
| D | 3 | 8 |
$$\text{Free} = 2$$
[10]
Show solved answerHide answer
Unsafe State vs Deadlocked State, and Banker's Algorithm Application
Given Data
| Process | Has | Max | Need = Max โ Has |
|---|---|---|---|
| A | 2 | 6 | 4 |
| B | 1 | 5 | 4 |
| C | 2 | 3 | 1 |
| D | 3 | 8 | 5 |
Free = 2
Total resources $= (2+1+2+3) + 2 = 10$ (single resource type).
Part 1: Unsafe State vs Deadlocked State
| Aspect | Unsafe State | Deadlocked State |
|---|---|---|
| Definition | No guaranteed safe sequence exists to let all processes finish | A set of processes is permanently blocked, each waiting on a resource held by another |
| Progress | Processes may still run; deadlock has not necessarily happened | No process can proceed at all |
| Nature | A warning/potential problem | An actual, current problem |
| Relationship | Superset: not every unsafe state leads to deadlock | A deadlocked state is always unsafe (deadlock โ unsafe) |
| Handling | Banker's Algorithm avoids it by denying dangerous requests | Requires detection + recovery |
An unsafe state means the system might deadlock if unlucky future requests occur; a deadlocked state means it has already got stuck.
Part 2: Is the Initial State Safe?
Apply safety check with Free = 2. Look for a process with $\text{Need} \le \text{Free}$.
- C: Need = 1 โค 2 โ run. Free = $2 + 2 = 4$
- A: Need = 4 โค 4 โ run. Free = $4 + 2 = 6$
- B: Need = 4 โค 6 โ run. Free = $6 + 1 = 7$
- D: Need = 5 โค 7 โ run. Free = $7 + 3 = 10$
Safe sequence: C โ A โ B โ D. The initial state is SAFE.
Part 3: Process D Requests 1 Resource
- Request = 1 โค Need(D) = 5 โ
- Request = 1 โค Free = 2 โ (tentatively grant)
Tentative state: D: Has = 4, Need = 4. Free = $2 - 1 = 1$.
Safety check with Free = 1:
| Process | Need | Need โค Free? |
|---|---|---|
| A | 4 | No |
| B | 4 | No |
| C | 1 | Yes |
| D | 4 | No |
- Run C: Free = $1 + 2 = 3$
- Remaining A(4), B(4), D(4): none โค 3 โ stuck
No safe sequence exists โ UNSAFE state.
Conclusion: D's request is DENIED.
Part 4: Process A Requests 1 Resource
- Request = 1 โค Need(A) = 4 โ
- Request = 1 โค Free = 2 โ (tentatively grant)
Tentative state: A: Has = 3, Need = 3. Free = $2 - 1 = 1$.
| Process | Has | Max | Need |
|---|---|---|---|
| A | 3 | 6 | 3 |
| B | 1 | 5 | 4 |
| C | 2 | 3 | 1 |
| D | 3 | 8 | 5 |
Safety check with Free = 1:
- C: Need = 1 โค 1 โ run. Free = $1 + 2 = 3$
- A: Need = 3 โค 3 โ run. Free = $3 + 3 = 6$
- B: Need = 4 โค 6 โ run. Free = $6 + 1 = 7$
- D: Need = 5 โค 7 โ run. Free = $7 + 3 = 10$
Safe sequence: C โ A โ B โ D. The resulting state is SAFE.
Conclusion: A's request is GRANTED.
Summary
| Request | Result |
|---|---|
| Initial state | SAFE (C โ A โ B โ D) |
| D requests 1 | DENIED (leads to unsafe state) |
| A requests 1 | GRANTED (state remains safe) |
Parts 2 and 3 give an initially safe state with D's request denied, and Part 4 shows that A's request is GRANTED.
2078 ยท 5 marks
What is resource allocation graph? Explain the process of detecting deadlocks when there is single instance of each resources with suitable example? [5]
Show solved answerHide answer
Resource Allocation Graph and Deadlock Detection (Single Instance)
Resource Allocation Graph (RAG)
A Resource Allocation Graph is a directed graph used to precisely describe and detect deadlocks in a system. It consists of:
-
Vertices (V): Two types:
- P = {P1, P2, ..., Pn} - set of all active processes (represented as circles)
- R = {R1, R2, ..., Rm} - set of all resource types (represented as rectangles)
-
Edges (E): Two types:
- Request Edge: Pi โ Rj (process Pi is requesting resource Rj)
- Assignment Edge: Rj โ Pi (resource Rj is assigned to process Pi)
Key Rule: If the RAG contains no cycle, no deadlock exists. If it contains one or more cycles, a deadlock exists (when each resource has only a single instance).
Deadlock Detection with Single Instance of Each Resource
When there is only one instance of each resource type, we construct a resource allocation graph and check for cycles. Any process that is part of a cycle is deadlocked.
Example
Consider a system with 7 processes (A to G) and 6 resources (R to W) with the following state:
| Process | Holds | Wants |
|---|---|---|
| A | R | S |
| B | Nothing | T |
| C | Nothing | S |
| D | U | S and T |
| E | T | V |
| F | W | S |
| G | V | U |
Step 1: Draw the RAG
Assignment Edges (Resource โ Process):
- R โ A
- U โ D
- T โ E
- W โ F
- V โ G
Request Edges (Process โ Resource):
- A โ S
- B โ T
- C โ S
- D โ S, D โ T
- E โ V
- F โ S
- G โ U
Step 2: Trace for Cycles
Tracing the graph:
D โ T โ E โ V โ G โ U โ D (Cycle Found!)
This forms a cycle: D โ T โ E โ V โ G โ U โ D
Step 3: Identify Deadlocked Processes
The processes involved in the cycle are:
D, E, and G are deadlocked.
- Process D holds U, wants T
- Process E holds T, wants V
- Process G holds V, wants U
Each is waiting for a resource held by another in the cycle, and none can proceed.
Processes A, B, C, and F are not deadlocked (they are not part of any cycle), even though they may be waiting.
Conclusion
In a single-instance resource system, deadlock detection reduces to cycle detection in the Resource Allocation Graph. If a cycle exists, all processes in that cycle are deadlocked.
2076 ยท 5 marks
Differentiate between deadlock and starvation? Discuss the process of detecting deadlocks when there are multiple resources of each type. [5]
2080.1 ยท 5 marks
Illustrate the term safe and unsafe state in deadlock prevention with scenario. [5]
Conditions for Deadlock
2080 ยท 10 marks
Can deadlock occur in case of preemptive resources? List the conditions for deadlock. Define allocation graph with example.[10]
Introduction
2079 ยท 5 marks
Distinguish between starvation and deadlock. How does the system schedule process using multiple queues? [5]
Resource - Allocation Graph
2076 ยท 5 marks
Write short notes on: a. Linux File System b. Resource Allocation Graph [5]
Deadlock Characterization
2080.1 ยท 10 marks
How do you distinguish between deadlock and starvation? Describe. Explain working mechanism of TLB.[10]
Make Unit 3 stick
Practice CSC264 with flashcards & quizzes