0% found this document useful (0 votes)
3 views

Lecture 5 - Deadlocks

The document discusses deadlocks in operating systems, outlining the conditions necessary for their occurrence, such as mutual exclusion, hold and wait, non-preemption, and circular wait. It also covers methods for addressing deadlocks, including prevention, avoidance, and detection/recovery, with a focus on Dijkstra's Banker’s Algorithm for deadlock avoidance. Additionally, the document provides examples and explanations of resource allocation graphs and the implications of safe and unsafe states in resource management.

Uploaded by

Arnold Masmini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 5 - Deadlocks

The document discusses deadlocks in operating systems, outlining the conditions necessary for their occurrence, such as mutual exclusion, hold and wait, non-preemption, and circular wait. It also covers methods for addressing deadlocks, including prevention, avoidance, and detection/recovery, with a focus on Dijkstra's Banker’s Algorithm for deadlock avoidance. Additionally, the document provides examples and explanations of resource allocation graphs and the implications of safe and unsafe states in resource management.

Uploaded by

Arnold Masmini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

CS 334: Principles of Operating

System

Lecture 5: Deadlocks
Outline
• Introduction
• Conditions for occurrence of deadlocks
• Deadlock prevention methods
• Deadlock avoidance methods
• Deadlock detection and recovery
• Starvation
Introduction
• A running process needs to access and use different types
of resources, for example keyboard, mouse, display, printer
etc.
• The resources are always limited compared to the number
of processes, so they are shared among the processes
• The shared resources have to be used in a mutually
exclusive manner otherwise mistakes will be introduced in
programs’ output

Process X Process X

Process Y Process Y

Mutual Exclusive Access to Resources


Introduction
Recap: Resources are allocated to processes in three steps
described below
i. Request: A process request for a resource through a system
call to the kernel. The OS checks the status of the resource
and allocates it to the process if it is free. Otherwise, the
requesting process will be blocked and wait until the resource
is free.
ii. Use: If a process gets a resource, the kernel changes the
status of the resource to allocated. The process uses the
resource in a mutually exclusive manner.
iii. Release: After its use, a process releases the resource again
through a system call. The kernel can now allocate this
resource to other blocked processes or change the status of
the resource to free.
Introduction
• A deadlock is a situation where a set of processes is
blocked because each process is holding a resource and
waiting for another resource held by other process.
• Deadlocks are common in multi-processing systems where
several processes share a specific type of mutually
exclusive resource

An example of a deadlock showing a traffic jam caused by two cars (processes)


trying to access and use a narrow bridge (limited resources) at the same time
Introduction
• Consider two processes P1 and P2 and two resources a file and a
printer
• At time 0, P1 holds the file while P2 has the printer
• At time 1, P1 requests for the printer, since it is held by P2 it is
blocked.
• At time 2, P2 requests for the file since P1 holds it, it is blocked.
• Both processes are now in the blocked queue waiting for a
resource held by the other process.
• The wait can continue to infinite if there is no intervention to
remove the deadlock.

File

Printer
Modelling Deadlocks
• Deadlocks can be complex to represent in an environment with
multiple processes
• A tool known as a Resource Allocation Graph (RAG), is used to
simplify the representation of deadlocks in a system.
• A RAG is a directed graph which represents deadlocks using the
states of processes and resources
• Below are some RAG notations and rules:
i. Processes are represented as circular nodes.
ii. Resources are represented with a rectangle node.
iii. The resource node contains some dots to
represent the number of instances of that
resource type. The number of dots is equal to the
number of instances.
iv. An edge, from a process to a resource, indicates
that the process has requested this resource but it
has not been allocated. This is known as a request
edge.
v. An edge, from a resource to a process, indicates
that this resource type has been allocated to the
process. This is known as an assignment edge.
Modelling Deadlocks
Example 5.1
Conditions for Deadlocks
There are 4 conditions necessary for deadlocks to occur.

i. Mutual Exclusion
The allocated resources are not shareable. If any other process
demands the allocated resources, it has to wait until the
executing process releases it.

Process X

Process Y
Conditions for Deadlocks
The following 4 conditions are necessary for deadlocks to occur.

ii. Hold and Wait


The system supports hold and wait, that is, the requesting
process already holds a resource and waits for other resources
that are currently being used by the other process.
Conditions for Deadlocks
iii. Non-Pre-emption
• Resources that are assigned to the other processes cannot be
pre-empted.
• The requesting process has to wait until the process holding
this resource releases it willingly.

With no pre-emption PA requesting for R1 With pre-emption PA releases R2


results to a deadlock
Conditions for Deadlocks
iv. Circular wait
• A series of processes that are waiting for each other in a chain
system must exist.
• Each process in the chain waits for a resource detained by
another process in the circular chain.

Every process holds a resource needed by another process


Conditions for Deadlocks
Example 5.2

✓ P5 may release the


instance of R1, because it
does not wait for any
other resource.
✓ The released R1 will be
allocated to P4,
consequently, the cycle
will break hence no
circular wait
✓ No deadlock
Addressing Deadlocks
The following approaches are used to address deadlocks
i. Deadlock Prevention: This method is based on preventing
any of the four necessary conditions for deadlock to occur.

ii. Deadlock Avoidance: Deadlock avoidance is implemented


by collecting and analysing status information on processes
and resources. The information is then used to avoid
deadlocks for example by delaying a process from executing
if its future request may produce a deadlock.

iii. Deadlock Detection and Recovery: If a system does not


employ either prevention or avoidance methods, then a
deadlock may occur. This approach provides a mechanism to
detect and recover from the deadlock situation.
Deadlock Prevention
i. Prevent Mutual Exclusive Access
• Sharable resources such as read-only files can be accessed
without mutual exclusion, no need to implement locks for these
processes.
• Disadvantage: Most resources are non-sharable that is they
require exclusive access by one process at a time to prevent
unexpected outputs.

ii. Prevent Hold and Wait


Two methods can be used to prevent Hold and Wait
• A process should request for all the required resources in
advance, and is not allowed to execute until it gets all the
resources
• A process can only request for a resource if it does not hold any
other resource
• Disadvantage: Reduced system efficiency due to longer waiting
times and low resource utilization
Deadlock Prevention
iii. Prevent No Pre-emption Condition
• if a process, holding a resource(s) wants an additional resource
that is not free, then the resources that it currently hold are pre-
empted. After some time, the process can request for the old
resources and the other required resources to re-start.
• Disadvantage: Can result to processes continuously changing
their state in response to each other without making any real
progress (Livelock).

iv. Prevent Circular Wait


• One method to prevent circular wait is by giving an order for
process to acquire resources.
• E.g. If there are two process p1 and p2 and two resources r1 and
r2 then resource acquiring order can be fixed that the process
first need to acquire resource r1 and then resource r2. In that
way the process that acquired r1 will be allowed to acquire r2 ,
other process needs to wait until r1 is free.
Deadlock Avoidance
• Less stringent than deadlock prevention methods
• If all four conditions for deadlock are met, deadlock can still
be avoided by using additional information about how
resources are to be requested.
• The most common method is that each process must declare
the maximum number of resources of each type that it may
need.
• The processes may demand new resources in an arbitrary
order.
• The resource allocator uses a deadlock avoidance algorithm
to evaluate each new resource application and grants the
allocation only if it leaves the system in a safe state.
• The system is in a safe state, if the resource allocator can
guarantee that all processes will be completed.
Deadlock Avoidance
• Deadlock avoidance algorithm uses prior information on
processes and resources to check in advance requests that
may give rise to deadlocks.
• If a request for a resource shall give rise to a deadlock, the
request is denied.
• The system status is categorised to be in either safe state
(does not lead to deadlock) or unsafe state (can lead to
deadlock)
• The avoidance algorithm ensure that, at all times the system
is in the safe state.
• When there is a new request, the algorithm is run to check
whether the requested resource changes the state of the
system.
• If the resource will lead to an unsafe state, the request is
rejected, otherwise it is granted.
Deadlock Avoidance
Safe and unsafe states
Consider a system with 3 running processes P0, P1 and P2 and 12
units of a resource, R

The state is safe because there are 2 available


resources which can be used by P1 to
execute and release 6 resources which can
be used by P0 and P2 to execute to completion

The state is unsafe state because there is


1 available resources which cannot be used
by any process to execute to the end
Deadlock Avoidance
A typical deadlock avoidance algorithm is described below
i. Establish the following parameters
✓ Maximum demand for all the processes
✓ All the resources
✓ Resources currently allocated
✓ Potential future requests for the processes.

ii. Check the feasibility for all new requests. A resource


request is feasible if
✓ Requesting process demand <= Declared maximum
demand
✓ All resources allocation <= Total resources available
✓ The request will not prevent potential future allocations

iii. Requests that are not feasible are rejected and must wait.
Deadlock Avoidance
Example 5.3
Consider a system with four processes using a resource R1 with
15 instances. Each process declared its maximum demand for
resources as shown in the table below. Analyse the safety of the
system at time instants t1, t2 and t3.
Deadlock Avoidance
Example 5.3 Solution
Checklist t1 t2 t3

A process demand should ✓ ✗ ✓


not exceed its maximum P2: 2+3=5 > 4 P3: 3+1=4 < 6
demand
The sum of all allocations ✓ ✗ ✓
should not exceed the total 6+2+3+2=13 < 15 6+5+3+2=16 > 15 6+2+4+2=14 < 15
number of resources
Can additional demands ✓ ✗
below maximum demand be There are 2 idle resources There is only 1 idle
satisfied? i. P2 is allocated the 2 idle resource left which can
resources, executes to the not satisfy additional
end and releases 4 resources. request from any
ii. P1 is allocated 3 resources, process
executes to the end and
releases 9 resources. Now we
have 10 resources which is
enough for P3 and P4
Safe state: All requests are Unsafe state: P2 Unsafe state: P3 request
granted request is rejected is rejected
Deadlock Avoidance
Dijkstra’s Banker’s Algorithm
• Dijkstra Banker’s algorithm is a deadlock avoidance algorithm
that tests for safety by simulating the allocation for the
predetermined maximum possible amounts of all resources
before deciding whether allocation should be allowed to
continue.
• The algorithm takes the analogy of a bank, when a customer
requests for a loan.
• The banker has to make sure that after giving a loan, he can
still satisfy all customers withdrawals from their deposits
• The Banker’s algorithm maintains some data that is used to
check whether request to resources maintains the safe state
of the system.
• If it does, the request can be granted; otherwise, the process
must wait until there are sufficient resources are available.
Deadlock Avoidance
Dijkstra’s Banker’s Algorithm
The banker’s algorithm has two parts.
i. Safety Test that checks the current state of the system
ii. Resource request handling part that verifies whether the
requested resources affect the safe state. If it does, the
request is denied.
Deadlock Avoidance
Dijkstra’s Banker’s Algorithm
Data used to implement the Banker's algorithm

Data Annotation Description

Total resources in a system Total_Res[i] = k i is a resource type

Maximum demand of each Max[i, j] = k i is a process


process j is a resource type
Number of resources of each Alloc[i, j] = k i is a process
type currently allocated to j is a resource type
each process
Number of available resources Av[i] = k i is a resource type
of each type Av[i] = Total_Res[i] – ∑all processes
Alloc[i]
Remaining resource need of a Need[i, j] = k i is a process
process Need[i, j] = Max[i, j] – Alloc[i, j] j is a resource type
Request for a process Req[i, j] = k i is a process
j is a resource type
Deadlock Avoidance
Dijkstra’s Banker’s Algorithm
1. Safety Test Algorithm
1. Loop through all the process
for(i=1; i<=p; i++){
1.1 Initialize the current process states as false
Process_status[i] = false;
1.2 Check for processes whose needs is less than available resources
for(j=1; j<=r; j++){
If(Need[i,j] ≤ Av[j] and Process_status[i] =false)
{
1.2.1 Set process status as true
Process_status[i] = true
1.2.2 After execution, updatethe new resource availability
Av[j] = Av[j]+ Alloc[i,j]
1.2.3 Save the process number in SafeString[]
SafeString[n]=i
}
}
}
2. if(Process_status[i] == true) for all processes, then the system is in safe state.
Print SafeString.
3. Else, the system is in unsafe state
Deadlock Avoidance
Dijkstra’s Banker’s Algorithm
2. Resource Request Handling Algorithm
Let Req be an array that store number of resources (j) requested by processes (i)
1. Check if number of requested resources does not exceed the declared needs
if(Req[i,j] > Need[i,j])
The request is not a feasible and is rejected.
2. Check if the number of requested resources doesnot exceed available resources
elseif (Req[i,j] > Av[j])
Resources are not available, so the process must wait. Otherwise,
3. Perform safety test
else {
3.1 Temporarily update the states
Av[j] = Av[j] – Req[i,j]
Alloc[i,j] = Alloc[i,j] + Req[i,j]
Need[i,j] = Need[i,j] − Req[i,j]
3.2 Execute the Safety Test Algorithm.
if (state is safe){
3.2.1 Allocate resource to the process
3.2.2 Change the states in actual }
else{
3.2.1 Do not allocate the resource to the process.
3.2.2 Keep the states unchanged}
}
Deadlock Avoidance
Example 5.4
Consider a system with the following information. Determine
whether the system is in a safe state.
Deadlock Avoidance
Example 5.4 Solution
i. Establish available resources
Av i = TotalRes i − ෍ 𝐴𝑙𝑙𝑜𝑐 𝑖
𝑎𝑙𝑙 𝑝𝑟𝑜𝑐𝑒𝑠𝑠𝑒𝑠
ii. Establish the future needs of each process
Need[i,j]=Max[i,j] – Alloc[i,j]
Process Max Alloc Need=Max - Alloc

R1(15) R2(8) R3(8) R1(3) R2(3) R3(2) R1 R2 R3

P1 5 6 3 2 1 0 3 5 3

P2 6 5 6 3 2 3 5 3 3

P3 4 9 2 3 0 2 1 9 0

P4 7 4 3 3 2 0 2 2 3

P5 4 3 3 1 0 1 3 3 2
Deadlock Avoidance
Example 5.4 Solution
Using Bankers Algorithm
1. Initialize process_status as false for all processes
2. Check for a process whose need is less than the availability
[3 3 2], P5 is found
2.1 Change the process_status of P5 to true
2.2 Compute new availability after P5 has completed executing
Av = [3 3 2] + [1 0 1] = [4 3 3]
2.3. Go back to step 2
The table below summarizes the result after executing the algorithm
Process Found New Availability New process_status Safe_string []

P5 [3 3 2] + [1 0 1] = [4 3 3] Process_status[5]=True P5

P4 [4 3 3] + [3 2 0] = [7 5 3] Process_status[4]=True P5, P4

P1 [7 5 3] + [2 1 0] = [9 6 3] Process_status[1]=True P5, P4, P1

P2 [9 6 3] + [3 2 3] = [12 8 6] Process_status[2]=True P5, P4, P1, P2

P3 need [1 9 0] > availability Process_status[3]=False P5, P4, P1, P2


[12 8 6]
3. The system is not in safe state since P3 future needs cannot be met
Deadlock Detection & Recovery
• If neither deadlock prevention nor deadlock avoidance
algorithms have been implemented in the system then
deadlock detection and recovery can be used to prevent
deadlocks

• Consist of two steps:


i. Detection: Examine the state of the system to detect for
deadlocks
ii. Recovery: If deadlock is found apply the recovery
mechanism.
Deadlock Detection
• To detect deadlocks, an algorithm, similar to the banker’s
avoidance algorithm, is used.
• The main difference is, while the avoidance algorithm checks
for processes whose needs do not exceed the available
resources, the detection algorithm checks for processes
whose requested resources do not exceed the available
resources
• The next example illustrates the detection algorithm
Deadlock Detection
Example 5.5
Consider a system with the following information. Determine
whether the system is in deadlock situation.
Deadlock Detection
Example 5.5 Solution
i. Establish available resources
Av i = TotalRes i − ෍ 𝐴𝑙𝑙𝑜𝑐 𝑖
𝑎𝑙𝑙 𝑝𝑟𝑜𝑐𝑒𝑠𝑠𝑒𝑠

Process Total resources Current Allocation Requests

R1(5) R2(6) R3(4) R1(1) R2(0) R3(1) R1 R2 R3

P1 1 0 2 1 0 0

P2 1 1 0 4 0 2

P3 1 1 0 0 1 2

P4 0 2 1 2 1 0

P5 1 2 0 3 1 4
Deadlock Detection
Example 5.5 Solution
Using the Detection Algorithm
1. Initialize process_status as false for all processes
2. Check for a process whose number of requested resources is less
than the availability
[1 0 1], P1 is found
2.1 Change the process_status of P1 to true
2.2 Compute new availability after P1 has completed executing
Av = [1 0 1] + [1 0 2] = [2 0 3]
2.3. Go back to step 2
The table
Process Foundbelow summarizes
New Availability the result
Newafter executing theSafe_string
process_status algorithm []

P1 [1 0 1] + [1 0 2] = [2 0 3] Process_status[1]=True P1

P2 [2 0 3] cant fulfil request [4 0 2] Process_status[2]=False P1

P3 [2 0 3] cant fulfil request [0 1 2] Process_status[3]=False P1

P4 [2 0 3] cant fulfil request [2 1 0] Process_status[4]=False P1

P5 [2 0 3] cant fulfil request [3 1 4] Process_status[5]=False P1

3. The system is not safe since some process_status = False


Deadlock Detection
Example 5.6
Consider a system represented by a RAG below where resources
[R1,R2] are [1,2]. Determine whether the system is in deadlock
situation.
Deadlock Detection
Example 5.6 Solution
Step1: Construct a resource allocation and request matrix

Step 2: check if all requests can be fulfilled without exceeding availability


✓ Available resources = total resources - allocated resources = [1,2]– [1,2]=[0,0]
✓ Process P3 dose not request for additional resources, so it can execute and release
Its resources
✓ New available resources = [0,1], process P1 can use these resources to execute and
Release its resources
✓ New available resources = [1,1], process P2 can use these resources to execute to the end
Deadlock Detection
Class Work
Consider a system represented by a RAG below where resources
[R1,R2] are [1,2]. Determine whether the system is in deadlock
situation.

R1
Recovery from Deadlock
• Deadlock detection aims to find out which process has caused
the deadlock and resolve it.
• An OS can resolve deadlocks in different ways such as
i. Abortion of processes
ii. Resource pre-emption
iii. Rollback
Recovery from Deadlock
1. Abortion of Processes
• This method aborts some processes to recover from
deadlock
• The following factors are used in choosing the process to
be aborted
i. The resources held by the process should be the cause
of the deadlock
ii. Processes with a long remaining execution time are
aborted first
iii. Processes with low priority are aborted first
iv. Batch processes are aborted first compared to
interactive processes
Recovery from Deadlock
2. Resource pre-emption
• In this method, resources are pre-empted from one of the
processes in the deadlock, and then given to other
processes
• The following factors are used in choosing the process to
be pre-empted
i. The resources held by the process should be the cause
of the deadlock
ii. Processes with a long remaining execution time are pre-
empted first
Recovery from Deadlock
3. Rollback
• The rollback method uses checkpoints to recover from a
deadlock.
• The checkpoints save the state of processes and
resources at a specified point in time.
• When a deadlock is detected, the process causing the
deadlock is rolled back to a safe checkpoint and its
resources are allocated to other processes.
• After the deadlock is resolved, the process which was
rolled back restarts from the checkpoint.
Deadlock vs Starvation
• Starvation occurs when a process cannot execute due to
lack of CPU or resources.
• The main difference is, with deadlock, once it occurs
there is no possibility for execution while with starvation
there is a possibility for execution though not guaranteed
• Starvation can happen in many scenarios example,
i. A low-priority process not getting a chance to
execute due to the presence of high-priority
processes.
ii. The same process being chosen for abortion or
rollback every time as part of deadlock recovery.
• One way to solve starvation is by temporarily increasing
the priority of low-priority processes, a process called
aging
Class Work
1

Investigate the system for a deadlock condition

3. Describe deadlock ignorance


Home Work
1. Go to https://teach-sim.com/tutorials/
2. Perform HW 4: Investigating Deadlocks
3. Upload the results (1 per group) in the LMS

You might also like