You are on page 1of 48

Deadlocks

Chapter Number 7

Mukhtar Azim Spring 2016 1


What is deadlock?

• A process requests resources & waits if not


available
• If another waiting process is holding these
resources the new process will not get these
resources.
• Situation is called deadlock

Mukhtar Azim Spring 2016 2


7.1 System Model
Traffic Deadlock

R1 R2

R3 R4

Mukhtar Azim Spring 2016 3


7.1 System Model
• Resources are finite for competing process
• Resources can be partitioned into types of identical
instances.
– Memory
– CPU
– Files
– Printers etc.
• e.g., the multiprocessing systems can have multiple
instances of resource type, CPU.
• Similarly printer can have multiple instances
Mukhtar Azim Spring 2016 4
7.1 System Model

• The type definition should be adequate:

• A printer on 1st and another or 9th floor should have


different types.

• The allocation of 9th floor printer against a request


from first floor will create problem.

Mukhtar Azim Spring 2016 5


7.1 System Model
• Sequence of events for resource utilization:
– Request: wait if request cannot be granted
– Use: operate on resource, print on a
printer
– Release: resource is released after use
• Request/Release are through system calls.
– Request/release device
– Open/close file
– Allocate/free memory
Mukhtar Azim Spring 2016 6
7.1 System Model

• Request for as many resources (not exceeding


total number of resources) can be made.
• More than 2 printers cannot be requested if
there are 2.

Mukhtar Azim Spring 2016 7


7.1 System Model

• Semaphores can be use for request/release


• OS Contains
– table containing information such as
A resource is allocated/free?
Allocated to which process?

– queue of processes waiting for the resources

Mukhtar Azim Spring 2016 8


7.1 System Model

• Deadlock
– A set of processes is in deadlock if waiting for
resources that can be free by another process in set
using acquisition and release mechanism of OS.
• Physical / Logical resources
– Printer, tape , memory, CPU --- physical
– File, semaphore etc --- logical

Mukhtar Azim Spring 2016 9


7.1 System Model
• Deadlock examples
• A system has 3 tape drives
• 3 processes hold one each
• If all of these request another, all the three
are deadlocked.

• A holds printer and B holds tape.


• A requests tape and
• B requests printer, DEADLOCK
occurred.

Mukhtar Azim Spring 2016 10


7.2.1 Necessary Conditions
Deadlock can arise only if all of the following
conditions hold simultaneously:
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait:
• if {P0, P1,… Pn} = set of processes
• P0 needs resource held by P1
• P1 needs resource held by P2
• Pn needs a resource held by P0
Mukhtar Azim Spring 2016 11
7.2.2 Resource Allocation Graph
• Vertices V and Edges E
P R

• Vertices consists of nodes


– Processes
– Resources
• Arrow going outward from process towards
resource designates a request for an instance of
R
• Arrow going out from resource to a process –
R is allocated to P
Mukhtar Azim Spring 2016 12
7.2.2 Resource Allocation Graph
• A dot within the resource square
represent an instance of a resource.
• Two dots means two instances
held
• Assignment Edge starts from a
point within the square
• Request Edge points to square

Mukhtar Azim Spring 2016 13


7.2.2 Resource Allocation Graph
• Upon a request, Request Edge is inserted
• Upon allocation Assignment Edge is inserted R1 R3
• Upon release edge is deleted

• RESOURCES = ? How many instances?


• PROCESSES = ?
• EDGES = ?
P1 P2 P3
• P1 has R2, needs R1
• P2 has R1 & R2, needs R3
• P3 has R3
R4
• No cycles and no deadlock
• Single instance + cycle = deadlock
• Multiple instances + cycle may be = deadlock

Mukhtar Azim Spring 2016 R2 14


7.2.2 Resource Allocation Graph
• P1 is holding ?
R1 R3
• P2 is holding ?
• P3 is holding ?

P1 P2 P3
• How many cycles are
there?
R4
• Are P1, P2 and P3
R2
deadlocked?

Mukhtar Azim Spring 2016 15


7.2.2 Resource Allocation Graph
P2
R1

P3
• How many cycles are P1
there?
• Are the processes
deadlocked?
R2
P4

Mukhtar Azim Spring 2016 16


7.3 Method of Handling Deadlock
1. Prevent a deadlock
– Set of methods to ensure one of the conditions does not occur.
– Constraining the requesting mechanism
2. Avoid deadlock
– In advance knowledge of resources
– Request handling mechanism
3. Detect the deadlock when it occurs, then recover from it
– Examine state of system
– Recover from the state (if deadlock has occurred)
4. Ignore the problem
– Deadlock will occur
– Resources will be held by processes in deadlock
– More processes will enter deadlock
– System will have to eventually be restarted
– A high priority system not returning will cause a system to freeze ----
not a deadlock.
Mukhtar Azim Spring 2016 17
7.4 Deadlock Prevention

• Deadlocks can be prevented by ensuring that at


least on of the necessary conditions
(Exclusion, Hold and Wait, No preemption or
Circular Wait) does not hold

Mukhtar Azim Spring 2016 18


7.4.1 Mutual Exclusion

– Non-shareable resources such as Printers, Tapes etc. must


be used exclusively
– Sharable resources such as read only files don’t need
exclusion
– Sharable resources don’t participate in deadlock

– The exclusion cannot always be denied, since some


resources are intrinsically non-sharable.

Mukhtar Azim Spring 2016 19


7.4.2 Hold and wait
To ensure that Hold and wait never occurs, either:
– 1 Allocate all resources at the start
• e.g., A process is to copy data from tape to disk file, sort the disk
file and print result to printer
• Tape, disk file and printer must be requested and held
• Wastage of printer resource
– 2 Request and release
• Request tape and disk file
• Copy file & Release tape and disk file
• Request disk file and printer
• Use and then release.

Mukhtar Azim Spring 2016 20


7.4.2 Hold and wait
• Disadvantages
1. Low resource utilization
• Resources remain allocated but unutilized.
• e.g., file or tape or printer may remain
unutilized for a long period of time.
2. Starvation
• e.g., one of required resources remains allocated
to some other process.

Mukhtar Azim Spring 2016 21


7.4.3 No pre-emption
• “No pre-emption” is avoided using one of the
following protocol:
• Protocol 1
– P is holding resource R1
– P requested a resource R2
– Implicitly release R1 from process (pre-empt)
– R1 is added to list of P’s required resources
– Restart P only when R1 and R2 are available

Mukhtar Azim Spring 2016 22


No Pre-emption

Mukhtar Azim Spring 2016 23


7.4.3 No pre-emption
• Protocol 2
– P1 requests some unavailable resources
– IF, P2, waiting for other resources, is holding these
– Preempt from P2 and allocate to P1
– Otherwise P1 must wait
– If P1 is waiting, its resources must be preempted IF
required by another during this wait.
– P is started if it gets newly requested and pre-empted
resources.
• Applicable only to resources whose state can be
saved and restored (e.g., CPU registers and
memory), not to printers and tape drives.
Mukhtar Azim Spring 2016 24
7.4.4 Circular Wait
In order for circular wait not to hold we must do:
• Ordering of resources. e.g.,
• Tape 1
• Disk 5
• Printer 12
• Protocol
– Processes can allocate resources in increasing order. If
P has Ri it can allocate Rj only if Ri < Rj
– P can allocate Tape first and then Printer, not vice
versa
• If Rj (requested resource) <= Ri,
– P must have released Ri.
Mukhtar Azim Spring 2016 25
7.5 Deadlock Avoidance

• Deadlock prevention results in low resource usage.


• Alternate is to avoid deadlock
• Suppose a system has a tape and a printer
• OS might be told P1 will need Tape and then printer,
before releasing both
• & P2 will need printer and then tape, before releasing
both

Mukhtar Azim Spring 2016 26


7.5 Deadlock Avoidance

• With this knowledge we can decide which resource


should be allocated to a process and when.
• A deadlock avoidance mechanism can dynamically
examine the resource allocation state to ensure there
will not be a circular wait condition.
• Resource State = # of allocated and available
resources AND demand of processes.

Mukhtar Azim Spring 2016 27


Deadlock Avoidance Algorithms

Deadlock
avoidance
1 2
3
Safe State Banker’s RAG
Algorithm Algorithm Algorithm

Resource Request
Safety Algorithm Algorithm
3a 3b

Mukhtar Azim Spring 2017 28


7.5.1 Safe State/Unsafe State

• Safe State = if resources can be allocated in some


order and still avoid deadlock.
• Unsafe State = If there is no order in which resources
can be allocated to avoid deadlock.
• Unsafe state = NOT a deadlock state
• Deadlock state = unsafe state BUT
• Every Unsafe state might not be deadlock
state….although it can lead to one.
• THE BEHAVIOR CONTROLS unsafe state

Mukhtar Azim Spring 2016 29


Mukhtar Azim Spring 2016 30
7.5.1 Safe State/Unsafe State
Illustration Situation at t0
Pr Max Req Current
Total resources = 12 tapes P0 10 5
P1 4 2
Held resources = 9
P2 9 2
Free = 3
System is in safe state
since sequence < P1,P0, P2 > exists:
P1 can get all the 2  Total free = 7
P0 can get all the 5  Total free = 10
P2 can get all the 7  All free No deadlock
Safe Sequence = < P1,P0,P2 >
Mukhtar Azim Spring 2016 31
7.5.1 Safe State/Unsafe State
• System can go to unsafe state if:
– At t0, P2 requests and is allocated 1 more tape.
Situation now is as in table: Free = 2
– Only P1 can get required. Situation at t1
– When it returns 4 will be free. Pr Max Current
P0 10 5
– P0 may requests 5 P1 4 2
P2 9 3
– P2 may request 6
– DEADLOCK

– Mistake was in granting 1 to P2


Mukhtar Azim Spring 2016 32
Mukhtar Azim Spring 2016 33
7.5.2 Resource Allocation Graph
Algorithm
R1 R1

P1 P2 P1 P2

Claim Edge
Assignment Edge
R2 R2

Mukhtar Azim Spring 2016 34


7.5.3 Banker’s Algorithm
• Used if each resource has multiple instances
• Each process must declare maximum number of
resources it may need.
• Following data structures are used to determine the
state of the system:
Available: Total available resources
Max: Maximum resource requirement
Allocation: Resources allocated to each process

Mukhtar Azim Spring 2017 35


7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

0
Mukhtar Azim Spring 2016 36
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
3 3 2
False False False False False

Introduce two more vectors named


1 Work = available and
Finish = False
Mukhtar Azim Spring 2016 37
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
3 3 2
False False False False False
Find an i such that both

2 and
Finish[i]=False

Needi <= Work


Mukhtar Azim Spring 2016 38
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
5 3 2
False True False False False
Find an i such that both

2 and
Finish[i]=False

Needi <= Work


Mukhtar Azim Spring 2016 39
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
7 4 3
False True False True False
Find an i such that both

3 and
Finish[i]=False

Needi <= Work


Mukhtar Azim Spring 2016 40
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
7 5 3
True True False True False
Find an i such that both

4 and
Finish[i]=False

Needi <= Work


Mukhtar Azim Spring 2016 41
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
10 5 5
True True True True False
Find an i such that both

5 and
Finish[i]=False

Needi <= Work


Mukhtar Azim Spring 2016 42
7.5.3.1 Safety Algorithm
Allocation (n x m) Max (n x m) Available (m) Need (n x m)
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 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

Finish
Work (m)
P0 P1 P2 P3 P4
10 5 5
True True True True True
Since there is no i such that Finish[i]=False,

6 The state is safe.

Mukhtar Azim Spring 2016 43


7.6 Deadlock Detection
A deadlock may occur in the absence of
deadlock prevention or avoidance algorithm.

The system must provide an algorithm to:

• Detect deadlock
• Recover from it

Mukhtar Azim Spring 2016 44


7.6.1 Single Instance of Each
Resource P1 is waiting
for P2 to release
a resource that
P1 needs

Wait for graph

A deadlock exists if wait-for graph contains cycle


Mukhtar Azim Spring 2016 45
7.7 Recovery From Deadlock

1. Terminate the process


a. Abort all deadlocked processes
Problem:The aborted process might have
computed for long enough.
a. Abort one process at a time
Problem: Considerable overhead in detecting
deadlock after termination of each process.

Mukhtar Azim Spring 2016 46


7.7 Recovery From Deadlock
2. Pre-empt the resource
The following issues are needed to be addresses while
preempting
a. Selecting a victim: Cost in determining (how
much it has worked and how many resources is it
holding) which process should be preempted
b. Rollback: Cost in determining the safe state for
roll back. Information maintenance for rollback.
c. Starvation: How to find out that the same
processes will not be preempted repeaatidly
Mukhtar Azim Spring 2016 47
Mukhtar Azim Spring 2016 48

You might also like