Lecture 5 - Deadlocks
Lecture 5 - Deadlocks
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
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.
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
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 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
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