Deadlocks arise when there exist a hold and wait situation when multiple processes share multiple resources simultaneously

ProcessHoldWait
P112
P223
P334
P445
P551

Scenario / Example

Consider two processes that require Printer and Scanner but interchangeably. P1 requires printer first and then scanner while P2 requires scanner first and then printer.

At time T1: P1 will acquire Printer and P2 will acquire Scanner
Both of them will then further wait for their respective resource to be free and stuck in a deadlock

Condition for Deadlock

Mutual Exclusion

Only one process can use the resource at a single time unit

Hold and Wait Condition

A thread holding at least one resource is waiting to acquire additional resources held by other threads

Non-Preemptive Condition

A resource can be released only voluntarily by the thread holding it, after that thread has completed its task

Circular Wait Condition

There exists a set {T0, T1, …, Tn} of waiting threads such that TO is waiting for a resource that is held by T1, T1 is waiting for a resource that is held by T2, …, Tn-1 is waiting for a resource that is held by Tn, and Tn is waiting for a resource that is held by T0.

We can break this condition to overcome the deadlock in Dining Philosopher Problem

Resource Allocation Graph

flowchart LR
	P1((Process 1)) -- Waiting For --> R2[Resource 2];
	R1[Resource 1] -- Is Assigned --> P1((Process 1));

Allocation Edge: Arrow From Resource to Process

Requesting Edge: Arrow from Process to Resource

Claim Edge: Dotted Arrow from Process to Resource

Safe Sequences

If we do not follow any of these safe sequences then there will always be a deadlock

flowchart LR
	P1((P1)) --> R1;
	R2 --> P1
	R2 --> P2((P2));
	R1 --> P2
	P2 --> R3
	R3 --> P3((P3))
	R4

P3 P2 P1

Avoiding Deadlocks

Single Resource Instance

Resource Allocation Graph Algorithm

Conditions
  1. Cyclic Graph
  2. Single Resource Instance for each Resource

We won’t allow the cyclic graph to be made, we will avoid the requesting edge which converts the graph to a cyclic graph

Safe and Unsafe States

If we are able to find a safe sequence given the states of the processes, then we can avoid the deadlock

If free resources are less than the available needs of all of the processes, then this can result in a deadlock

Multiple Resource Instance

Banker’s Algorithm

  1. If Need[i] Available then Available = Available + Allocation[i]
Example 1

We can construct safe sequence(s) given the available resource are greater or equal than any of the current process’s need

Total Resources Available
ResourceTotalAvailable
A103
B53
C72
Allocation Matrix
ProcessAllocation AAllocation BAllocation CMax AMax BMax CNeed ANeed BNeed C
P0010753743
P1200322122
P2302902600
P3211222011
P4002433431
Example 2 - Draw Resource Allocation Graph from the Matrix

We can construct safe sequence(s) given the available resource are greater or equal than any of the current process’s need

Total Resources Available
ResourceTotal
A2
B1
C1
D2
E1
Allocation Matrix
ProcessAllocation AAllocation BAllocation CAllocation DAllocation E
P010110
P111000
P200010
P300000
Need Matrix
ProcessNeed ANeed BNeed CNeed DNeed E
P001001
P100101
P200001
P310101
flowchart TD
A; B; C; D; E
P0((P0))
P1((P1))
P2((P2))
P3((P3))

A --> P0
A --> P1
B --> P1
C --> P0
D --> P0
D --> P2

P0 --> B
P0 --> E
P1 --> C
P1 --> E
P2 --> E
P3 --> A
P3 --> C
P3 --> E