Top Banner
DEADLOCK
48

DEADLOCK

Feb 23, 2016

Download

Documents

Thành

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. P 1. P 2. R 1. R 2. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DEADLOCK

DEADLOCK

Page 2: DEADLOCK

Contents Principles of deadlock

Deadlock prevention

Deadlock detection

Page 3: DEADLOCK

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

R2

P1 P2

R1

Page 5: DEADLOCK

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

Page 6: DEADLOCK

request (the process can be blocked) userelease

Utilization protocol

Page 7: DEADLOCK

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

Page 8: DEADLOCK

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

Page 9: DEADLOCK

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

Page 10: DEADLOCK

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

Conditions for deadlock

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

Page 11: DEADLOCK

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

Page 12: DEADLOCK

mutual exclusion

hold-and-waitno preemptioncircular wait

Page 13: DEADLOCK

All four conditions must hold for deadlock to occur

Page 14: DEADLOCK

System resource allocation graphvertices:

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

edges:request edge Pi Rjassignment edge Rj Pi

Page 15: DEADLOCK

R2 R3

P1 P2 P3

R1

Page 16: DEADLOCK

If the graph does not contains cycles, then no process is deadlockedIf the graph contain one cycle, then a deadlock may exist

Page 17: DEADLOCK

If each resource type has exactly one instance,

then a cycle implies that one deadlock has occurred

Page 18: 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

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

P3

P1 P2

R1

R2

Page 21: DEADLOCK

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

Page 22: DEADLOCK

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

Page 23: DEADLOCK

We can ignore the problem, and pretend that deadlocks never occur in the systemIt is up to the application developer to write programs that handle deadlocks

Page 24: DEADLOCK

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

Page 25: DEADLOCK

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

Page 26: DEADLOCK

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

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

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

We associate an index with each resource typeThen Ri precedes Rj in the ordering if i<j

Page 30: DEADLOCK

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

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

Page 32: DEADLOCK

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

Page 33: DEADLOCK

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

Page 34: DEADLOCK

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

Page 35: DEADLOCK

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

Page 36: DEADLOCK

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

Banker’s algorithm

When a process makes a request for a set of resources

Page 38: DEADLOCK

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

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

Page 40: DEADLOCK

R1

P2

t1 t2 t3 t4

t5

t6

t7

t8

R2

safestate

safe state

safestate

unreachable region

A

R1

R2

P1

safestate

safestate

unsafe region

Page 41: DEADLOCK

Deadlock detection

It requires:

Page 42: DEADLOCK

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

RecoveryPossible approaches:

1.Abort all deadlocked processes

Page 44: DEADLOCK

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

Page 45: DEADLOCK

3.Successively abort deadlocked processes until deadlock not longer exists

4.Successively preempt resources until deadlock not longer exists

Page 46: DEADLOCK

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

Page 47: DEADLOCK

Least amount of consumed processor time

Least amount of produced output

Page 48: DEADLOCK

Most estimated remaining time

Least total resources allocated so far

Lowest priority