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

OS Lecture-8 (Deadlock)

The document discusses deadlocks in computing systems. It defines deadlocks and explains the four conditions required for deadlocks to occur: mutual exclusion, hold and wait, no preemption, and circular wait. It also discusses methods to handle deadlocks, including prevention, avoidance, detection and recovery.

Uploaded by

pkapurbe22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

OS Lecture-8 (Deadlock)

The document discusses deadlocks in computing systems. It defines deadlocks and explains the four conditions required for deadlocks to occur: mutual exclusion, hold and wait, no preemption, and circular wait. It also discusses methods to handle deadlocks, including prevention, avoidance, detection and recovery.

Uploaded by

pkapurbe22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

OS Lecture-8

Deadlocks
Introduction
Deadlock in Real Life

1. Alice, gets the oil to start cooking

4. Adam won’t leave the


pan till he gets the oil
3. Alice won’t release the
oil till she gets the pan

2. Adam, grabs the pan


Another Example
Resources
• Resource: something a process uses
– Usually limited (at least somewhat)

• Examples of computer resources:


– Printers
– Semaphores / locks
– Tables (in a database)

• Processes need access to resources in reasonable order.

• Two types of resources:


– Preemptable resources: can be taken away from a process with no ill
effects.
– Nonpreemptable resources: will cause the process to fail if taken away.
When Do Deadlocks Happen?
• Suppose
– Process 1 holds resource A and
requests resource B
Process 1 Process 2
– Process 2 holds B and requests A
A
– Both can be blocked, with neither
able to proceed B

• Deadlocks occur when … A

– Processes are granted exclusive


access to devices or software B
constructs (resources)
– Each deadlocked process needs a DEADLOCK!
resource held by another
deadlocked process
Using Resources
• Sequence of events required to use a resource:
– Request the resource
– Use the resource
– Release the resource

• Can’t use the resource if request is denied.


─ Requesting process has options:
1. Block and wait for resource
2. Continue (if possible) without it: may be able to use an alternate
resource

3. Process fails with error code

─ Some of these may be able to prevent deadlock…


Deadlock
What is a Deadlock?
• Formal definition:
“A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can cause.”

• Usually, the event is release of a currently held resource.

• In deadlock, none of the processes can


– Run
– Release resources
– Be awakened
Four Conditions for Deadlock
Deadlock can arise if four conditions hold simultaneously:

• Mutual exclusion:
– only one process at a time can use a resource.

• Hold and wait:


– a process holding at least one resource is waiting to acquire
additional resources held by other processes.

• No preemption:
– a resource can be released only voluntarily by the process holding
it, after that process has completed its task.

• Circular wait:
– there exists a set {P0, P1, …, Pn} of waiting processes such that P0
is waiting for a resource that is held by P1, P1 is waiting for a
resource that is held by P2, …, Pn–1 is waiting for a resource that is
held by Pn, and P0 is waiting for a resource that is held by P0.
Mutual exclusion
• A resource can be held by only one process at a time.

• In other words, if a process P1 is using some resource R at a


particular instant of time, then some other process P2 can't hold or
use the same resource R at that particular instant of time.
Hold and wait
• A process can hold a number of resources at a time and at the same
time, it can request for other resources that are being held by some
other process.

• For example, a process P1 can hold two resources R1 and R2 and at


the same time, it can request some resource R3 that is currently
held by process P2.
No preemption
• A resource can't be preempted from the process by another
process, forcefully.

• For example, if a process P1 is using some resource R, then some


other process P2 can't forcefully take that resource.
Circular wait
• Circular wait is a condition when the first process is waiting for the
resource held by the second process, the second process is waiting
for the resource held by the third process, and so on.
• At last, the last process is waiting for the resource held by the first
process.
• So, every process is waiting for each other to release the resource
and no one is releasing their own resource.
Resource-Allocation Graph
Resource-Allocation Graph
• A set of vertices V and a set of edges E.

• V is partitioned into two types:


– P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system

– R = {R1, R2, …, Rm}, the set consisting of all resource types in


the system

• request edge – directed edge Pi  Rj


• assignment edge – directed edge Rj  Pi
Resource-Allocation Graph (cont.)
• Process

• Resource Type with 4 instances

• Pi requests instance of Rj

Pi
Rj

• Pi is holding an instance of Rj

Pi
Rj
Example of a Resource Allocation Graph

Allocation Need
R1 R2 R3 R1 R2 R3
P1 0 1 0 1 0 0
P2 1 1 0 0 0 1
P3 0 0 1 0 0 0

No cycle  No deadlock
Resource Allocation Graph with a Deadlock

Allocation Need
R1 R2 R3 R1 R2 R3
P1 0 1 0 1 0 0
P2 1 0 0 0 0 1
P3 0 0 1 0 1 0

Cycle  Possibility of deadlock

Avail = [ 0 0 0 ]

Safe sequence = None  Deadlock


Resource Allocation Graph with a Cycle but no Deadlock

Allocation Need
R1 R2 R1 R2
P1 0 1 1 0
P2 1 0 0 0
P3 1 0 0 1
P4 0 1 0 0

Cycle  Possibility of deadlock

Avail = [ 0 0 0 ]

Safe sequence = < P2 , P4 , P1 , P3 >


 No deadlock
Basic Facts
• If graph contains no cycles  no deadlock.

• If graph contains a cycle 


– if only one instance per resource type, then deadlock.

– if several instances per resource type, possibility of deadlock.


Methods for Handling Deadlocks
1. Ensure that the system will never enter a deadlock state.
─ Deadlock prevention - prevent one of necessary conditions occurring.

─ Deadlock avoidance - monitor resource use and deny requests that


would lead to deadlock.

2. Allow the system to enter a deadlock state and then recover.


─ Deadlock detection and recovery

3. Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX.
Deadlock Prevention
Deadlock Prevention
Disallow at least one condition that is necessary for deadlock to occur.

• Disallow Mutual Exclusion: not required for sharable resources;


must hold for non-sharable resources - so this would not work.

• Disallow Hold and Wait: must guarantee that whenever a


process requests a resource, it does not hold any other
resources.
─ Require process to request and be allocated all its resources
before it begins execution, or allow process to request resources
only when the process has none.
─ Low resource utilization; starvation possible.
Deadlock Prevention (cont.)
• Disallow No Preemption:
─ If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
─ Preempted resources are added to the list of resources for which
the process is waiting.
─ Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting.

• Disallow Circular Wait: impose a total ordering of all resource


types, and require that each process requests resources in an
increasing order of enumeration. E.g.
3. Printer
1. Tape Drive
2. Disk Drive Process 1. Tape Drive
3. Printer
2. Disk Drive
4. CPU
Deadlock Avoidance
Deadlock Avoidance
Requires that the system has some additional a priori information
available.

• Simplest and most useful model requires that each process


declare the maximum number of resources of each type that it
may need.

• The deadlock-avoidance algorithm dynamically examines the


resource-allocation state to ensure that there can never be a
circular-wait condition.

• Resource-allocation state is defined by the number of available


and allocated resources, and the maximum demands of the
processes.
Safe State
• When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state

• System is in safe state if there exists a sequence <P1, P2, …, Pn> of all the
processes in the systems such that for each Pi , the resources that Pi can
still request can be satisfied by currently available resources + resources
held by all the Pj , with j < i.

• That is:
─ If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished.

─ When Pj is finished, Pi can obtain needed resources, execute,


return allocated resources, and terminate.

─ When Pi terminates, Pi+1 can obtain its needed resources, and so


on.
Safe State (cont.)

• State is safe because OS can definitely avoid deadlock


– by blocking any new requests until safe order is executed.

• This avoids circular wait condition


– Process waits until safe state is guaranteed.
Safe State Example
• Suppose there are 12 tape drives

max need allocated need


P0 10 5 5
P1 4 2 2
P2 9 2 7

• 3 drives remaining

• Safe sequence = < P1 , P0 , P2 >


Basic Facts
• If a system is in safe state  no deadlocks.

• If a system is in unsafe state  possibility of deadlock.

• Avoidance  ensure that a system will never enter an unsafe


state.
Safe, Unsafe, Deadlock State
Deadlock Avoidance Algorithms
• Single instance of a resource type
– Use a resource-allocation graph

• Multiple instances of a resource type


– Use the Banker’s algorithm
Resource-Allocation Graph Algorithm
• Claim edge Pi  Rj indicated that process Pi may request resource
Rj ; represented by a dashed line.

• Claim edge converts to request edge when a process requests a


resource.

• Request edge converted to an assignment edge when the resource


is allocated to the process.

• When a resource is released by a process, assignment edge


reconverts to a claim edge.

• Resources must be claimed a priori in the system.


Resource-Allocation Graph Algorithm (cont.)
• Suppose that process Pi requests a resource Rj.

• The request can be granted only if converting the request edge to


an assignment edge does not result in the formation of a cycle in
the resource allocation graph.

Allocate one
instance of
R2 to P2

Safe State Unsafe State


Banker’s Algorithm
• The banker’s algorithm is a resource allocation and deadlock
avoidance algorithm.

• It tests for safety by simulating the allocation for predetermined


maximum possible amounts of all resources.

• Makes an “safe-state” check to test for possible activities, before


deciding whether allocation should be allowed to continue.
Data Structures for Banker’s Algorithm
Let ‘n’ be the number of processes in the system and ‘m’ be the
number of resources types.
• Available: Vector of length m. If Available[j] = k, there are k instances
of resource type Rj available.

• Max: n  m matrix. If Max [i, j] = k, then process Pi may request at


most k instances of resource type Rj.

• Allocation: n  m matrix. If Allocation[i, j] = k then Pi is currently


allocated k instances of Rj.

• Need: n  m matrix. If Need[i, j] = k, then Pi may need k more


instances of Rj to complete its task
Need [i, j] = Max[i, j] – Allocation [i, j].

• Finish: Boolean value, either true or false. If finish[i] = true for all i
return safe else unsafe.
Safety Algorithm
1. Let Available and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available and Finish [i] = false for i = 0, 1, …, n-1
2. Find an i such that both:
a) Finish [i] == false
b) Needi ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
Go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
Resource-Request Algorithm for Process Pi
Let Requesti be the request array for process Pi. Requesti [j] = k means
process Pi wants k instances of resource type Rj.
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3.
Otherwise Pi must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;

If safe ⇒ the resources are allocated to Pi


If unsafe ⇒ Pi must wait, and the old resource allocation state is restored.
Example of Banker’s Algorithm
• 5 processes: P0 through P4
• 3 resource types: A (10 instances), B (5 instances), and C (7 instances).
• Snapshot at time T0:

Allocation Max

ABC ABC
P0 010 753
P1 200 322
P2 302 902
P3 211 222
P4 002 433

Find a safe sequence if Available (A, B, C) = (3, 3, 2)


Apply Safety Algorithm
Allocation Max Need
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Is the system in a safe state?
Step 1 of Safety Algorithm Step 2
For i = 1
m=3,n=5
Need1 = 1, 2, 2
Work = Available
Finish[1] is false and Need1 ≤ Work
Work = 3 3 2
So P1 must be kept in safe sequence
0 1 2 3 4
Finish = false false false false false Step 3
Work = Work + Allocation1
A B C
Step 2
For i = 0 Work = 5 3 2

Need0 = 7, 4, 3 0 1 2 3 4
Finish[0] is false and Need0 > Work Finish = false true false false false
So P0 must wait
Step 2 Step 2
For i = 2 For i = 4
Need2 = 6, 0, 0 Need4 = 4, 3, 1
Finish[2] is false and Need2 > Work Finish[4] is false and Need4 ≤ Work
So P2 must wait So P4 must be kept in safe sequence

Step 2 Step 3
For i = 3
Work = Work + Allocation4
Need3 = 0, 1, 1
A B C
Finish[3] is false and Need3 ≤ Work
Work = 7 4 5
So P3 must be kept in safe sequence
0 1 2 3 4
Step 3 Finish = false true false true true
Work = Work + Allocation3
A B C
Work = 7 4 3 Step 2
For i = 0
0 1 2 3 4 Need0 = 7, 4, 3
Finish = false true false true false Finish[0] is false and Need0 ≤ Work
So P0 must be kept in safe sequence
Step 3 Step 3
Work = Work + Allocation0 Work = Work + Allocation2
A B C A B C
Work = 7 5 5 Work = 10 5 7

0 1 2 3 4 0 1 2 3 4
Finish = true true false true true Finish = true true true true true

Step 2 Step 4
For i = 2
Need2 = 6, 0, 0 Finish[i] = true for 0 ≤ i < n
Finish[2] is false and Need2 ≤ Work Hence the system is in safe state
So P2 must be kept in safe sequence

The safe sequence is <P1, P3, P4, P0, P2>


Example (cont.)
• Assume P1 Request (1,0,2).
• Check that Request  Available (that is, (1,0,2)  (3,3,2) => true.
• The new state will be:
Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 301 600
P3 211 011
P4 002 431

• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2 > satisfies
safety requirement.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Deadlock Prevention Vs. Deadlock Avoidance
Deadlock Detection
and Recovery
Deadlock Detection and Recovery
• System lets the deadlocks occur.

• Tries to detect the deadlocks when they occur.

• Takes action to recover from deadlock.

• We will see different algorithms of deadlocks detection for:


– Only one resource of each type exists.

– Multiple copies of each type of resource may exist.


Deadlock Detection
Deadlock Detection: Single Instance of Each
Resource Type
• Maintain wait-for graph.

• Nodes are processes.

• Pi  Pj if Pi is waiting for Pj.

• Periodically invoke an algorithm that searches for a cycle in


the graph. If there is a cycle, there exists a deadlock!

• An algorithm to detect a cycle in a graph requires an order of


O(n2) operations, where n is the number of vertices in the
graph.
RAG and Wait-for Graph

Resource Allocation Graph Corresponding Wait-for Graph


Deadlock Detection: Multiple Resource of
Each Type
• Use Banker’s Algorithm to detect deadlock.

• Algorithm requires an order of O(mn2) operations to detect


whether the system is in deadlocked state, where m is the
number of resources and n is the number of processes.
Deadlock Recovery
Deadlock Recovery: Process Termination
• Recovery through killing the process.
• Abort all deadlocked processes.
• Abort one process at a time until no deadlock.
• A process in the cycle can be chosen as the victim.
• Restart the process.
• OK for some applications such as compiling.
• Not OK for some other applications: database record
updates.
Deadlock Recovery: Resources
• Recovery through preemption:
– Temporarily take the resource from the current owner and give
it to another process.
• Example: laser printer can be taken away in some cases

• Recovery through rollback:


– Processes are check pointed periodically.
• State of process is written to a file including the resource
allocation state.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy