3 Process Deadlocks

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.

Process Deadlocks: notes

Introduction

A deadlock is a situation in which two or more processes are blocked indefinitely, each waiting for a resource held by another process in the same waiting cycle, so that none of them can ever proceed. To reason about this generally, anything a process can acquire, use and later release is called a resource: this can be a hardware device, such as a printer or a Blu-ray drive, or a piece of information, such as a locked record in a database. A computer offers many kinds of resource that a process might request over its lifetime, and deadlock can arise around any of them once two or more processes start competing for several resources each.

Deadlock Characterization

Deadlock is characterized, and diagnosed, in two complementary ways: by checking whether a fixed list of necessary conditions all hold at once for a set of waiting processes, and by drawing a resource allocation graph of who holds and who is waiting for what, since a cycle in that graph (under conditions made precise below) is the visual signature of deadlock. Both views describe the same underlying situation: characterizing deadlock is what makes it possible to design systems that prevent it, avoid it, or detect it once it has already happened.

Preemptable and Non-preemptable Resources

Resources are classified by whether they can be taken away from the process holding them. A preemptable resource can be taken from its current owner and given back later without harming the process, such as main memory, which can be swapped out and restored. A non-preemptable resource cannot be taken away while it is in use without causing the operation to fail, such as a Blu-ray recorder in the middle of burning a disc. Whether a given resource is preemptable can depend on context, but deadlock in the strict sense is only a risk with non-preemptable resources, since a preemptable resource can always be reclaimed to break a cycle of waiting.

Resource - Allocation Graph

A resource allocation graph is a directed graph with two kinds of vertex: processes, drawn as circles, and resources, drawn as rectangles (with a dot for every instance if the resource type has more than one). Two kinds of edge appear on the graph. A request edge $P_i \to R_j$ means process $P_i$ has requested and is waiting for an instance of resource $R_j$. An assignment edge $R_j \to P_i$ means an instance of resource $R_j$ has already been allocated to process $P_i$. A request edge always points at the resource's rectangle, not at a specific dot inside it, but an assignment edge originates from a specific instance (dot); when a resource instance becomes available and is granted to a waiting process, its request edge is converted into an assignment edge.

If every resource type in the graph has exactly one instance, a cycle in the graph is both a necessary and a sufficient condition for deadlock: every process on the cycle is waiting for a resource held by the next process on the cycle, so none can proceed. Consider four processes $P_1, P_2, P_3, P_4$ and four single-instance resources $R_1, R_2, R_3, R_4$, with $P_1$ holding $R_1$ and requesting $R_2$, $P_2$ holding $R_2$ and requesting $R_3$, $P_3$ holding $R_3$ and requesting $R_4$, and $P_4$ holding $R_4$ and requesting $R_1$. The edges $P_1 \to R_2 \to P_2 \to R_3 \to P_3 \to R_4 \to P_4 \to R_1 \to P_1$ form a single cycle through all four processes, so all four are deadlocked: each is waiting forever for a resource that will only be released once its holder receives a resource that only the next process in the cycle can release. If a resource type has multiple instances, however, a cycle is only necessary, not sufficient, for deadlock, because the resource a process is waiting for might still be released by some other instance holder outside the cycle.

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]

Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. No process can proceed, release, or be preempted. --- For a deadlock to occur, all four of the following conditions must hold si...

Open this answer on its own page โ†’

2079 ยท 10 marks

Banker's Algorithm Analysis

All files reside in one common directory shared by all users. Advantages: Simple to implement, easy to search, fast access. Disadvantages: All file names must be unique (naming collision), unsuitable for multiple users, becomes unmanageable as file count grows, no grouping of related files. A Mas...

Open this answer on its own page โ†’

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?

ProcessHasMax
A26
B15
C23
D38

$$\text{Free} = 2$$

[10]

Show solved answer

Unsafe State vs Deadlocked State, and Banker's Algorithm Application

Given Data

ProcessHasMaxNeed = Max โˆ’ Has
A264
B154
C231
D385

Free = 2

Total resources $= (2+1+2+3) + 2 = 10$ (single resource type).


Part 1: Unsafe State vs Deadlocked State

AspectUnsafe StateDeadlocked State
DefinitionNo guaranteed safe sequence exists to let all processes finishA set of processes is permanently blocked, each waiting on a resource held by another
ProgressProcesses may still run; deadlock has not necessarily happenedNo process can proceed at all
NatureA warning/potential problemAn actual, current problem
RelationshipSuperset: not every unsafe state leads to deadlockA deadlocked state is always unsafe (deadlock โІ unsafe)
HandlingBanker's Algorithm avoids it by denying dangerous requestsRequires 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

  1. Request = 1 โ‰ค Need(D) = 5 โœ“
  2. Request = 1 โ‰ค Free = 2 โœ“ (tentatively grant)

Tentative state: D: Has = 4, Need = 4. Free = $2 - 1 = 1$.

Safety check with Free = 1:

ProcessNeedNeed โ‰ค Free?
A4No
B4No
C1Yes
D4No
  • 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

  1. Request = 1 โ‰ค Need(A) = 4 โœ“
  2. Request = 1 โ‰ค Free = 2 โœ“ (tentatively grant)

Tentative state: A: Has = 3, Need = 3. Free = $2 - 1 = 1$.

ProcessHasMaxNeed
A363
B154
C231
D385

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

RequestResult
Initial stateSAFE (C โ†’ A โ†’ B โ†’ D)
D requests 1DENIED (leads to unsafe state)
A requests 1GRANTED (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.

Open this answer on its own page โ†’

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 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:

ProcessHoldsWants
ARS
BNothingT
CNothingS
DUS and T
ETV
FWS
GVU

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.

Open this answer on its own page โ†’

2076 ยท 5 marks

Differentiate between deadlock and starvation? Discuss the process of detecting deadlocks when there are multiple resources of each type. [5]

--- Aspect Deadlock Starvation --------- Definition A situation where a set of processes are permanently blocked, each holding a resource and waiting for a resource held by another process in the set A situation where a process waits indefinitely because other processes continuously get priority ...

Open this answer on its own page โ†’

2080.1 ยท 5 marks

Illustrate the term safe and unsafe state in deadlock prevention with scenario. [5]

The concept of safe and unsafe states is central to deadlock avoidance (specifically the Banker's Algorithm), where the OS checks whether granting a resource request will lead to a safe or unsafe state before actually granting it. : "What the algorithm does is check to see if granting the request...

Open this answer on its own page โ†’

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]

--- No, deadlock cannot occur in the case of preemptive resources. One of the four necessary conditions for deadlock is No Preemption, which states: "A process acquiring a resource cannot be preempted in between to release the acquired resource. Instead, the process itself must voluntarily releas...

Open this answer on its own page โ†’

Introduction

2079 ยท 5 marks

Distinguish between starvation and deadlock. How does the system schedule process using multiple queues? [5]

--- Basis Deadlock Starvation --------- Definition A situation where a set of processes are blocked, each waiting for a resource held by another process in the same set, so none can proceed. A situation where a process waits indefinitely because other processes continuously get the resources it n...

Open this answer on its own page โ†’

Resource - Allocation Graph

2076 ยท 5 marks

Write short notes on: a. Linux File System b. Resource Allocation Graph [5]

--- The Linux File System refers to how a Linux-based computer organizes, stores, and manages system files. It is basically a combination of directories (folders) that serve as placeholders for addresses of other files. - In Linux, there is no distinction between a file and a directory, because a...

Open this answer on its own page โ†’

Deadlock Characterization

2080.1 ยท 10 marks

How do you distinguish between deadlock and starvation? Describe. Explain working mechanism of TLB.[10]

--- Definition: Deadlock is a situation in a multiprogramming environment where a set of processes are blocked permanently because each process in the set is holding a resource and waiting to acquire a resource held by another process in the same set. None of the processes can proceed, and the sy...

Open this answer on its own page โ†’