Sei sulla pagina 1di 47

The Deadlock Problem System Model Deadlock Characterization

Methods for Handling Deadlocks


Deadlock Prevention Deadlock Avoidance

Deadlock Detection
Recovery from Deadlock

To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks

To present a number of different methods for preventing or avoiding deadlocks in a computer system.

Suppose system has 2 tape drives. P1 and P2 each hold one tape drive and each needs another one.
Process 1 Process 2

holds

holds

Deadlock Occurs

Tape Drive 1

Tape Drive 2

I need quad C and B

I need quad B and C

I need quad D and A

I need quad A and B

HALT until D is free

HALT until C is free

HALT until A is free

HALT until B is free

Deadlock Occurs

Deadlock can be defined formally as follows:

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

A process must request a resource before using it, and must release the resource after using it. A process may request as many resources as it requires to carry out its designated task
The number of resources requested may not exceed the total number of resources available in the system

E.g a process cannot request three printers if the system only has two)

Hold and wait condition

No preemption condition.

Mutual exclusion condition

Circular wait condition.


Conditions for Resource Deadlocks

SPRING 2011 - 102

10

1st Condition

Only one process can have exclusive control of a shared Mutual exclusion resource and if any other process request that very condition particular resource then it has to wait till the time the resource is released

2nd Condition

Hold and wait condition

A process holding one resource and waiting to acquire another resource that are currently held by other process
SPRING 2011 - 102

11

3rd Condition

Resources cannot be preempted; A resource can be No preemption released only voluntarily by the process holding it, after condition. that process has completed its task.

4rth Condition

A circular chain of processes exist Circular wait in which each process holds condition. number of resources which are requested by the next process in the chain

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 P1 Rj assignment edge directed edge Rj Pi

Four conditions can be modeled by using directed graphs

The graphs have two type of nodes

Process (as circle)

Resources (as square)

Requesting a resource

Holding a resource

P1 is holding an instance of resource R2

P1 is waiting for an instance of a resource R1


P2 is holding an instance of resource R1 & R2 P2 is waiting for an instance of a resource R3 P3 is holding an instance of resource R3

P1 is holding an instance of resource R2

P1 is waiting for an instance of a resource R1


P2 is holding an instance of resource R1 & R2 P2 is waiting for an instance of a resource R3 P3 is holding an instance of resource R3 P3 is waiting for an instance of resource R2

R ESOURCE A LLOCATION G RAPH W ITH A C YCLE B UT N O D EADLOCK

if only one instance per resource type, then deadlock

cycles

no deadlock

No cycles
if several instances per resource type, possibility of deadlock

18

SPRING 2011 - 102

if only one instance per resource type, then deadlock

cycles

no deadlock

No cycles
if several instances per resource type, possibility of deadlock

20

SPRING 2011 - 102

21

Strategies for dealing with deadlocks:


1st Strategy

Just ignore the problem and pretend deadlock never happened

2nd Strategy

Prevention, negating one of the four required conditions


Dynamic avoidance allocation by careful resource

3rd Strategy

4th Strategy

Detection and recovery. Let deadlocks occur, detect them, take action.
SPRING 2011 - 102

Just ignore the problem Reasonable if

deadlocks occur very rarely cost of prevention is high

UNIX and Windows takes this approach It is a trade off between


convenience correctness

2nd strategy: Deadlock Prevention

Deadlock prevention is the process of making it logically impossible for one of the 4 deadlock conditions to hold.

Mutual exclusion Hold and Wait No preemption Circular wait


Restrain the ways resource allocation requests can be made to insure that at least one of the four necessary conditions is violated.

2nd strategy: Deadlock Prevention

Restrain the ways request can be made.


not required for sharable resources;


must hold for non sharable resources.

Must be supported by the OS

2nd strategy: Deadlock Prevention

To ensure that the hold-and-wait never occurs in the system, we must guarantee that, whenever a process requests a resource, it does not hold any other resources.

2nd strategy: Deadlock Prevention

2 Ways

Require a process to request and be allocated all its resources before it begins execution

Allow a process to request resources only when the process has none.

2nd strategy: Deadlock Prevention

Problems

No useful work possible with some resources while waiting for others Also ties up resources other processes could be using May not know required resources at start of run Process could starve

A process that needs several popular resources may have to wait indefinitely, because at least one of the resources that it needs is always allocated to some other process

2nd strategy: Deadlock Prevention

2nd strategy: Deadlock Prevention

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.

30

SPRING 2011 - 102

3rd strategy: Deadlock 31 Avoidance

This approach to the deadlock problem anticipates deadlock before it actually occurs. This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly. This method differs from deadlock prevention, which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock.

If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by being careful when resources are allocated
SPRING 2011 - 102

3rd strategy: 3rd Deadlock strategy: Avoidance

Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.
Avoid deadlock by acquiring additional information on how the resources are to be requested.

For Example:
Process A uses resource 1 and 2 and then releases them Process B uses resource 2 and then 1.

If system have this information before hand than prior to resource allocation a deadlock avoidance algorithm can determine that which process should wait so that a deadlock can be avoided in future

3rd strategy: 3rd strategy: Deadlock Deadlock Avoidance

Avoidance

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 safe sequence of all processes. Sequence <P1, P2, , Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources

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.

3rd strategy: Deadlock Avoidance

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.

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

1
5 7 1 0

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

1
5 1 0

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

1 2
5 7 0 1

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

1 2
5 7 1 0

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

3 1 2
5 7 1 0

3rd strategy: Deadlock Avoidance

A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock

A system is in Safe State only

if there exists a Safe Sequence.

For example: Suppose


system with 10 resources and following allocation

3 1 2
5 7 10 0

3rd strategy: Deadlock Avoidance

Bankers Algorithm Made by Dijkstra


Examines if an allocation leads to a safe or unsafe state Each process must a priori claim maximum use.

When a process requests a resource it may have to wait. When a process gets all its resources it must return them
in a finite amount of time.

3rd strategy: Deadlock Avoidance

Available:
Vector of length m. If available [j] = k, there are k instances of resource type Rj available.

Max:
n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.

Allocation
n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.

Need
n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task

3rd strategy: Deadlock Avoidance

Need Matrix can be calculated as

Need [i,j] = Max[i,j] Allocation [i,j].

3rd strategy: Deadlock Avoidance

Snapshot at time T0:


Allocation
ABC P0 010

Max
ABC 753

Available
ABC 332

P1 P2
P3 P4

200 302
211 002

322 902
222 433

3rd strategy: Deadlock Avoidance

The content of the matrix. Need is defined to be

Need = Max Allocation. Need


ABC P0 743

P1 P2
P3 P4

122 600
011 431

3rd strategy: Deadlock Avoidance

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.

Potrebbero piacerti anche