Vous êtes sur la page 1sur 51

Concurrency: Deadlock and

Starvation
CHAPTER 6

Objectives
2

Describe two categories of resources


List and explain the conditions for deadlock
Define strategies for deadlock problem

Define deadlock prevention and describe deadlock

prevention strategies related to each of the


conditions for deadlock
Explain the difference between deadlock
prevention and deadlock avoidance
Understand two approaches to deadlock avoidance

Objectives
3

Explain the fundamental difference in approach

between deadlock detection and deadlock


prevention or deadlock avoidance
Understand how an integrated deadlock strategy
can be designed

Topics
4

Principles of Deadlock
Resource Categories
Deadlock Strategies
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection

Integrated Deadlock Strategy

Principle of Deadlock
The permanent blocking of a set of processes that

either compete for system resources or


communicate with each other
A set of processes is deadlocked when each process
in the set is blocked awaiting an event that can only
be triggered by another blocked process in the set
Permanent
No efficient solution

Illustration of Deadlock
I need
quad C and
B

I need
quad B and
C

I need
quad A and
B

I need
quad D and
A

Potential Deadlock

Illustration of Deadlock
HALT
until D is
free

HALT
until C is
free

HALT
until B is
free

HALT
until A is
free

Actual Deadlock

Resource Categories

Reusable Resources
Can be used by one process at a

time and not depleted by that use


Processes obtain resources that
they later release for reuse by
other processes
Example of reusable resourses:

Processors, I/O channels, main


and secondary memory, files,
databases, and semaphores

Deadlock occurs if each process

holds one resource and requests


the other

Consumable
Resources
Created (produced) and

destroyed (consumed) by a
process
May take a rare combination of
events to cause deadlock
E.g. - Interrupts, signals,
messages, and information in I/O
buffers

Reusable Resources Example 1

Reusable Resources Example 1


Deadlock occurs when the multiprogramming

system interleaves the execution of two processes as


follows:

P0
P1
Q0
Q1
P2
Q2

Reusable Resources Example 2


Space is available for allocation of 200Kbytes, and

the following sequence of events occur:


P1

Request 80Kb

Request 60 Kb

P2

Request 70Kb

Request 80 Kb

Deadlock occurs if both processes progress to their

second request

Consumable Resources Example


Consider a pair of processes, in which each

process attempts to receive a message from the


other process and then send a message to the other
process:
...

P1

Receive(P2);
...

Send(P2, M1);

...

P2

Receive(P1);
...

Send(P1, M2);

Deadlock occurs if the Receive( ) is blocking

Resource Allocation Graph

Resource Allocation Graph

P1 hold Ra, Request Rb

P2 hold Rb, Request Rc


P3 hold Rc, Request Rd
P4 hold Rd, Request Ra

Conditions for Deadlock


Mutual Exclusion
Hold-and-Wait
No Preemption

Circular Wait

Only one process at a time can use a

resource

A process may hold allocated resources

while awaiting assignment of others

No resource can be forcibly removed

from a process holding it

A closed chain of processes exists, such

that each process holds at least one


resource needed by the next process in
the chain

Approaches for Deadlock


Three approaches in dealing with deadlock

Prevent Deadlock
adopt a policy that eliminates one of the conditions

Avoid Deadlock
make the appropriate dynamic choices based on the
current state of resource allocation

Detect Deadlock
attempt to detect the presence of deadlock and take
action to recover

Deadlock Prevention

To design a system in such a way that the


possibility of deadlock is excluded.
Method falling into two classes:
1.

Indirect method
prevent the occurrence of one of the three
necessary conditions
2. Direct method
to prevent the occurrence of circular wait.

Preventing the Conditions


Mutual Exclusion
MUST BE ALLOWED!!
If access to resources require ME, then ME must be
supported by the OS.
Hold-and-Wait
require that a process request all of its required resources
at one time and blocking the process until all requests can
be granted simultaneously

Preventing the Conditions


No Preemption
if a process holding certain resources is denied a further
request, that process must release its original resources
and request them again
OS may preempt a lower priority process and require it to
release its resources
Circular Wait
define a linear ordering of resource types

Deadlock Avoidance
A decision is made dynamically whether the

current resource allocation request will, if granted,


potentially lead to a deadlock
Requires knowledge of future process requests
Two approaches:
1. Resource Allocation Denial
2. Process Initiation Denial

Deadlock Avoidance Approaches

Resource Allocation Denial


do not grant an incremental resource request to a
process if this allocation might lead to deadlock

Process Initiation Denial


do not start a process if its demands might lead to
deadlock

Deadlock Avoidance Approaches


Consider a system of n processes and m different

types of resources.
Definition of the vectors and matrices:
Resource Vector= R = (R1, R2,, Rm)
Total amount of each resource in the system
Available Vector = V = (V1, V2,, Vm)
Total amount of each resource not allocated to any
process.

Deadlock Avoidance Approaches

Claim Matrix =C =

C11, C12,, C1m

C21, C22,, C2m


..
.

..
.

..
.

Cn1, Cn2,, Cnm


Cij

= requirement of process i for resource j


one row dedicated to each process
Gives the maximum requirement of each process.

Deadlock Avoidance Approaches

Allocation Matrix =A =

A11, A12,, A1m


A21, A22,, A2m
..
.

..
.

..
.

An1, An2,, Anm


Aij

= current allocation to process i of resource j


Shows current allocation of resources for each process.

Resource Allocation Denial


The strategy refer to as the bankers algorithm
State of the system reflects the current allocation

of resources to processes

consists of two vectors, Resource and Available and two


matrices, Claim and Allocation

Safe state is where there is at least one sequence

that does not result in deadlock

i.e. all the process can be run to completion

Unsafe state a state this is not safe.

Example 1
Figure 6.7a shows the state of a system consisting

of four processes and three resources.


Total amount of resources R1, R2 and R3 are 9, 3
and 6 units. (Resource Vector)
In the current state, allocations have been made to
the four processes and leaving only 1 unit of R2
and 1 unit of R3 available (Available Vector).
Is this a safe state?

Example 1

R1

R2

R3

R1

R2

R3

P1

P2

P3

R1

R2

R3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

Allocation
Matrix

Initial State

Resource Vector

Available Vector

Example 1
Steps needed to check either it is a safe state or

not:
1. Construct the Need Matrix
Need Matrix = Claim Matrix Allocation Matrix
R1

R2

R3

P1

P2

P3

P4

o it shows how many resources needed by each


program in order for the process to complete
execution.

Example 1
2. To check either the current state is save or not safe,
o Compare the content of available vector with the
need matrix.
o

Is there any process can be allocated with the available


resources and the process can run to completion.

For this example the system has 1 unit of R2 and 1


unit of R3. Based from the claim matrix P2 can
runs to completion.

Example 1
3. After a process runs to completion, it will release all
the resources to system. Construct new available
vector.
o New AV = Current Av + Allocation Matrix of the
chosen process
= 011+ 612
= 623

Example 1

R1

R2

R3

P1

P2

P3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

Allocation
Matrix

P2 Runs to Completion

R1

R2

R3

Available Vector

Example 1

R1

R2

R3

P1

P2

P3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

R1

R2

R3

Available Vector

Allocation
Matrix

P1 Runs to Completion

Example 1

R1

R2

R3

P1

P2

P3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

R1

R2

R3

Available Vector

Allocation
Matrix

P3 Runs to Completion

Example 1

R1

R2

R3

P1

P2

P3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

R1

R2

R3

Available Vector

Allocation
Matrix

P4 Runs to Completion
When all processes run to completion, the value for

available vector is equal to resource vector

Example 2
Figure 6.8a shows the state of a system consisting

of four processes and three resources.


Total amount of resources R1, R2 and R3 are 9, 3
and 6 units. (Resource Vector)
In the current state, allocations have been made to
the four processes and leaving only 1 unit of R1, 1
unit of R2 and 2 unit of R3 available (Available
Vector).
Suppose P1 make a request for 1 unit of R1 and 1
unit of R3.
Is this a safe state?

Example 2

R1

R2

R3

R1

R2

R3

P1

P2

P3

R1

R2

R3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

Allocation
Matrix

Initial State

Resource Vector

Available Vector

Example 2
R1

R2

R3

P1

P2

P3

P4

R1

R2

R3

P1

P2

P3

P4

Claim Matrix

R1

R2

R3

Available Vector

Allocation
Matrix

State after P1 request for 1 unit of R1 and R3


It is unsafe to grant additional request from P1

because it will lead to unsafe state


DEADLOCK!!!

Deadlock Avoidance Approaches


Advantage:
Does not necessarily preempt and rollback processes
compared to deadlock detection.
But has a number of restrictions:
Maximum resource requirement must be stated in
advance
Processes under consideration must be independent; no
synchronization requirements
There must be a fixed number of resources to allocate
No process may exit while holding resources

Deadlock Detection
Does not limit resource access or restrict process

actions.
Resource access are granted to process whenever
possible.
OS will periodically performs
an algorithm to check if deadlock present
an algorithm to recover from deadlock

Deadlock Detection
The deadlock check can be performed at every

resource request.
Checking at each resource request has two
advantages:
i.
ii.

Leads to early detection


Algorithm is simple
because it is based on incremental changes to the state
of the system.

Disadv: such frequent checks will consume CPU

time.

Deadlock Detection Algorithm


The Allocation matrix and Available vector will be

used.
Request matrix Q is define such that Qij represent
the amount of resources of type j requested by
process i.

Deadlock Detection Algorithm


Then the following steps are performed:
1. Mark each process that has a row in the Allocation
matrix of all zeros.
i.e. no resources are allocated to the process.

2. Initialize a temporary vector W to equal to Available


vector.
3. Find an index i such that process i is currently
unmarked and ith row of Q <= W. If no such row
found terminate the algorithm.
4. If exist, mark process i and add the corresponding row
of the Allocation matrix to W. Then return to step 3.

Deadlock Detection Algorithm


Deadlock exist if and only if there are unmarked

processes at the end of algorithm


Strategy in this algorithm is to find a process
whose resource requests can be satisfied with the
available resources and the assume that those
resources are granted and the process runs to
completion and release its resources.
Then the algorithm will look for another process.

Deadlock Detection Algorithm


This algorithm will not guarantee to prevent

deadlock, it will depend on the order in which


requested are granted.
It is only to determine either deadlock is currently
exist or not.

Example

Example
The algorithm proceeds as follows:
1.
Mark P4, because P4 has no allocated resources.
2.
Set W = ( 0 0 0 0 1 )
3.
The request of P3 is less than equal to W, so mark P3
and set W = W + ( 0 0 0 1 0)
=(00011)
4.
No other unmarked process has a row in Q is less than
or equal to W. Terminate the algorithm.
The algorithm concludes with P1 and P2

unmarked. This processes are deadlocked.

Deadlock Detection Recovery

Needed when deadlock is detected.


The following approaches are possible:
1.

Abort all deadlocked processes (one of the most


common solution adopted in OS)
2. Back up each deadlocked process to some previously
defined checkpoint, and restart all process.

Deadlock Detection Recovery


3. Successively abort deadlock processes until deadlock
no longer exists

After each abortion, need to reinvoke the deadlock detection


algorithm to see either deadlock still exists or not.

4. Successively preempt some resources from processes


and give them to other processes until deadlock no
longer exists

a process that has a resource preempted must be rolled back


prior to its acquisition of that resource.

Deadlock Detection Recovery


For approaches 3 and 4: a victim process needs to

be selected according to:


Least amount of processor time consumed so far
Least number of lines of output produced so far
Most estimated time remaining
Least total resources allocated so far
Lowest priority

Integrated Deadlock Strategy


Combine the previous approaches into the

following way:
Group resources into a number of different resource
classes
Use the linear ordering strategy (prevention of circular
wait) to prevent deadlock between resource classes
Within a resource class, use the algorithm that is most
appropriate for that class.

Integrated Deadlock Strategy


Example of resource classes:
Swappable space
Blocks of memory on secondary storage for use in
swapping processes.
Process resources
Assignable devices, such as tape drives and files
Main memory
Assignable to processes in pages and segments
Internal resources
Such as I/O channels

Vous aimerez peut-être aussi