48
DEADLOCK

DEADLOCK. Contents Principles of deadlock Deadlock prevention Deadlock detection

Embed Size (px)

Citation preview

Page 1: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

DEADLOCK

Page 2: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Contents Principles of deadlock

Deadlock prevention

Deadlock detection

Page 3: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Deadlock

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.

Page 4: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

R2

P1 P2

R1

Page 5: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Examples of resources: processors,I/O devices, main and secondary memory, files, emaphores…(reusable resources)

Examples of resources: processors,I/O devices, main and secondary memory, files, emaphores…(reusable resources)

Page 6: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

request (the process can be blocked) userelease

Utilization protocol

Page 7: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

P1P(mutex1);<R1>;P(mutex2);<R2>;V(mutex2);<release of R2>;V(mutex1);<release of R1>;

Page 8: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

P2P(mutex2);<R2>;P(mutex1);<R1>;V(mutex1);<release of R1>;V (mutex2);<release of R2>;

P2P(mutex2);<R2>;P(mutex1);<R1>;V(mutex1);<release of R1>;V (mutex2);<release of R2>;

Page 9: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

A deadlock situation derives from a race condition occurred to some involved processes

Page 10: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

R1, R2, … , Rm: a set of resource types

Conditions for deadlock

P1, P2, … , Pn: a set of processes

Page 11: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

A deadlock situation can arise if the following four conditions hold at the same time:

Page 12: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

mutual exclusion

hold-and-waitno preemptioncircular wait

Page 13: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

All four conditions must hold for deadlock to occur

Page 14: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

System resource allocation graph

vertices: P=(P1,P2,..,Pn)R=(R1,R2,..,Rm)

edges:request edge Pi Rjassignment edge Rj Pi

Page 15: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

R2 R3

P1 P2 P3

R1

Page 16: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

If the graph does not contains cycles, then no process is deadlocked

If the graph contain one cycle, then a deadlock may exist

Page 17: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

If each resource type has exactly one instance,

then a cycle implies that one deadlock has occurred

Page 18: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Each process involved in the cycle is deadlocked

(a cycle in the graph is a necessary and sufficient condition for the existence of a deadlock)

Each process involved in the cycle is deadlocked

(a cycle in the graph is a necessary and sufficient condition for the existence of a deadlock)

Page 19: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

If each resource type has several instances, then one cycle does not necessary imply that a deadlock occurred (the cycle is a necessary but not sufficient condition)

Page 20: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

P3

P1 P2

R1

R2

Page 21: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Methods for handling deadlockWe can use a protocol to ensure hat the system will never enter a deadlock state (deadlock prevention)

Page 22: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

We can allow the system to enter a deadlock state and then recover (detection and recovery)

Page 23: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

We can ignore the problem, and pretend that deadlocks never occur in the system

It is up to the application developer to write programs that handle deadlocks

Page 24: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Deadlock prevention

Deadlock prevention is a set of methods for ensuring that at least one of the necessary conditions can never occur

Page 25: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

mutual exclusion

It is not possible to prevent deadlocks by denying the mutual exclusion condition

Page 26: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

hold-and- waitThat condition may be prevented by requiring that each process must release all the resources currently allocated before it can request any additional resources.

Page 27: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

no preemption

If a process that it is holding same resources request another resource that cannot be immediately allocated to it, then all resources currently being held are preempted

Page 28: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

circular waitThe condition can be prevented by defining a total ordering of all resource types and by requiring that each process requests resources in an increasing order

Page 29: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

We associate an index with each resource type

Then Ri precedes Rj in the ordering if i<j

Page 30: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Two processes A and B, are deadlocked if A has acquired Ri and requests Rj, and B has acquired Rj and requests Ri

Page 31: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

That condition is impossible because it implies i<j and j<i

Page 32: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Deadlock avoidance

Deadlock-prevention algorithms prevent deadlocks by constraining the strategy on how requests can be made

Page 33: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Possible side effects of preventing deadlocks by these methods are an inefficient utilization of resources and an inefficient process execution

Page 34: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

With deadlock avoidance, a decision is made dynamically whether current resource allocation requests, if granted, would potentially lead to deadlock

Page 35: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

The resource allocation state is defined by the number of allocated and available resources and the maximum demands of processes

Page 36: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

A safe state is one in which there is at least one process execution sequence such that all processes can be run to completion (safe sequence)

Page 37: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Banker’s algorithm

When a process makes a request for a set of resources

Page 38: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

assume that the request is granted, update the system state accordingly, and then determine if the result is still a safe state.

Page 39: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

If so, grant the request, if not, block the process until it is safe to grant the request

If so, grant the request, if not, block the process until it is safe to grant the request

Page 40: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

R1R1

P2P2

t1t1 t2

t2 t3t3 t4

t4

t5t5

t6t6

t7t7

t8t8

R2R2

safestatesafestate

safe statesafe state

safestatesafestate

unreachable regionunreachable region

AA

R1R1

R2R2

P1P1

safestatesafestate

safestatesafestate

unsafe regionunsafe region

Page 41: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Deadlock detection

It requires:

Page 42: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

an algorithm that examines the state of the system to determine whether a deadlock has occurred

an algorithm to recover from the deadlock

Page 43: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Recovery

Possible approaches:

1.Abort all deadlocked processes

Page 44: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

2.Back up each deadlocked process to some previously defined check points, and restart all processes form those checkpoints

Page 45: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

3.Successively abort deadlocked processes until deadlock not longer exists

4.Successively preempt resources until deadlock not longer exists

Page 46: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

For 3 and 4 the selection criteria could be one of the following. Choose the process with the:

For 3 and 4 the selection criteria could be one of the following. Choose the process with the:

Page 47: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Least amount of consumed processor time

Least amount of consumed processor time

Least amount of produced output

Least amount of produced output

Page 48: DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection

Most estimated remaining time

Most estimated remaining time

Least total resources allocated so far

Least total resources allocated so far

Lowest priority Lowest priority