Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Deadlock

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

Objectives and Outline

Objectives Outline
• To develop a description of • The Deadlock Problem
deadlocks, which prevent sets of • System Model
concurrent processes from completing • Deadlock Characterization
their tasks • Methods for Handling Deadlocks
• Deadlock Prevention
• To present a number of different • Deadlock Avoidance
methods for preventing or avoiding
• Deadlock Detection
deadlocks in a computer system
• Recovery from Deadlock

1
The Deadlock Problem

• A set of blocked processes each holding a resource and waiting to acquire a


resource held by another process in the set
• Example
– System has 2 disk drives
– P1 and P2 each hold one disk drive and each needs another one

2
Bridge Crossing Example

section1 section2

• Traffic only in one direction


• Each section of a bridge can be viewed as a resource
• If a deadlock occurs, it can be resolved if one car backs up (preempt resources
and rollback)
• Several cars may have to be backed up if a deadlock occurs
• Starvation is possible
• Note – Most OSes do not prevent or deal with deadlocks

3
System Model

• Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O devices
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
– request (may cause the process to wait)
– use
– release

4
Deadlock Characterization

• Deadlocks can arise if four conditions hold simultaneously


– Mutual exclusion: only one process at a time can use a resource
– Hold and wait: a process holding at least one resource is waiting to
acquire additional resources held by other processes
– No preemption: a resource can be released only voluntarily by the process
holding it, after that process has completed its task
– Circular wait: there exists a set {P0, P1, …,PN, P0} of waiting processes
such that P0 is waiting for a resource that is held by P1, P1 is waiting for a
resource that is held by P2, …, Pn–1 is waiting for a resource that is held by
Pn, and Pn is waiting for a resource that is held by P0.

5
Resource-Allocation Graph

A set of vertices V and a set of edges E.

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

6
Resource-Allocation Graph (Cont.)

• Process

• Resource Type with 4 instances

• Pi requests instance of Rj
Pi
Rj

• Pi is holding an instance of Rj

Pi
Rj

7
Resource Allocation Graph With A
Deadlock

There is a cycle
and
Deadlock

8
Graph With A Cycle But No Deadlock

There is a cycle
but
No Deadlock

9
Basic Facts

• If graph contains no cycles  no deadlock

• If graph contains a cycle 


– if only one instance per resource type, then deadlock
• Call it Single Instance System
– if multiple instances per resource type, possibility of deadlock
• Call it Multiple Instance System

10
Methods for Handling Deadlocks

• Ensure that the system will never enter a deadlock state


– Deadlock Prevention or Deadlock Avoidance methods

• Allow the system to enter a deadlock state and then recover


– Deadlock Detection needed

• Ignore the problem and pretend that deadlocks never occur in the system;
– Used by most operating systems, including UNIX
– OS does not bother with deadlocks that can occur in applications

11
Deadlock Prevention

Basic Principle: Restrain the ways requests can be made

• Mutual Exclusion – not required for sharable resources; must hold for
nonsharable resources
– Example: spooling for printer
• Hold and Wait – must guarantee that whenever a process requests a
resource, it does not hold any other resources
– Require process to request and allocated all its resources at beginning
– Allow process to request resources only when it has none

– Low resource utilization; starvation possible

12
Deadlock Prevention (Cont.)

• No Preemption –
– A process holding resources makes request: if request cannot be granted,
release (preempt) the held resources, and try again later.
– 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 was requesting.

– Example: memory as a resource needed by a program

• Circular Wait – impose a total ordering of all resource types, and require that
each process requests resources in an increasing order

13
Deadlock Prevention (Cont.)

• All resources are ordered and assigned an integer number


– A process can request resources in increasing order of enumeration

Resources

R1 R2 R3 R4 R5

can only request and allocate in this order

Example:
Process 1 Process 2 Process 3
Request R2 Request R1 Request R3
Request R4 Request R2 Request R4
Request R3

14
Deadlock Avoidance

Basic Principle: Requires that the system has some additional


a priori information available

• Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
to hold simultaneously. (maximum demand)

• The deadlock-avoidance algorithm dynamically examines the resource-


allocation state to ensure that there can never be a circular-wait condition.

• Resource-allocation state is defined by the number of available and allocated


resources, and the maximum demands of the processes

15
Safe state

• When a process requests an available resource, system must decide if


immediate allocation leaves the system in a safe state

• A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock.

• We are considering a worst-case situation here. Even in the worst case


(processes request up their maximum at the moment), we don’t have deadlock
in a safe state.

16
Safe state

• More formally: A system state is safe if there exists a safe sequence of all
processes (<P1, P2, …, Pn>) such that for each Pi, the resources that Pi can
still request can be satisfied by
currently available resources + resources held by all processes earlier in
the sequence (all Pj, with j < i)
• That is:
– If Pi resource needs are not immediately available, then Pi can wait until all
Pj (j < i) have finished
– When Pj 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.

17
Basic Facts

• 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.

18
Safe, Unsafe , Deadlock State

19
Avoidance Algorithms

• Single instance of a resource type


– Use a resource-allocation graph

• Multiple instances of a resource type


– Use the banker’s algorithm

20
Resource-Allocation Graph Scheme

• Claim edge Pi  Rj indicates that process Pi may request resource Rj;


represented by a dashed line

• Claim edge is converted to a request edge when a process requests a


resource
• Request edge is converted to an assignment edge when the resource is
allocated to the process
• When a resource is released by a process, assignment edge is reconverted to
a claim edge

21
Resource-Allocation Graph

22
Resource-Allocation Graph

P2 requests R2; should we allocate?


23
Unsafe State In Resource-Allocation Graph

24
Resource-Allocation Graph Algorithm

• Suppose that process Pi requests a resource Rj

• The request can be granted only if converting the request edge to an


assignment edge does not result in the formation of a cycle in the resource
allocation graph.

25
Banker’s Algorithm

• Multiple instances


– System will know maximum demand of each process

• 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

26
Simple Example

• Assume a resource A (printer) has 5 instances.


• There are 2 processes: P1, P2
• Max demand is: 5 5
• Current Allocation is: 3 0

• Need is: ….?


• Available is: …?

Is the current state safe?

27
Simple Example

• Assume P2 request 1 instance and it is granted. Is the new state safe?


• Current Allocation will be : 3 1

• Need is: …..


• Available is: …..

28
Data Structures for the Banker’s Algorithm

Let n = number of processes, and


m = number of resources types.

• Available: Vector of length m. If Available[j] == k, there are k instances of


resource type Rj at the time deadlock avoidance algorithms is run.

• 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
Need[i,j] = Max[i,j] – Allocation[i,j]
29
Banker’s Algorithm
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0
P1 200 P1 322 P0
P1
P2 302 P2 902 P2 P1
P3 211 P3 4 22 P3 P2
P4 002 P4 533 P4 P3
P4
Total
725
30
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0
P1
P2 302 P2 902 P2 P1
P3 211 P3 4 22 P3 P2
P4 002 P4 533 P4 P3
P4
Total
725
31
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
Remaining needs, P0
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1
P2 302 P2 902 P2 P1
P3 211 P3 4 22 P3 P2
P4 002 P4 533 P4 P3
P4
Total
725
32
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 Remaining needs, P1
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2
P4 002 P4 533 P4 P3
P4
Total
725
33
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 Remaining needs, P2
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P4 P3
P4
Total
725
34
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
Remaining needs, P3
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P4 P3 211
P4
Total
725
35
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 Remaining needs, P4
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P4 P3 211
P4 531
Total
725
36
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 P1 successfully Release
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,
37
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,
38
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 P3 successfully Release
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,P3,
39
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,P3
40
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 P4 successfully Release
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,P3, P4,
41
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 745 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,P3, P4,
42
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 P0 successfully Release
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 745 P2 600
P4 002 P4 533 P3 211
P4
P4 531
Total
725 P1,P3, P4, P0,
43
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 745 P2 600
P4 002 P4 533 P3 211
P4 755
P4 531
Total
725 P1,P3, P4, P0,
44
An example system state
Available
Existing ABC Initially Available == Existing
ABC
All 10 5 7 P2 successfully Release
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 745 P2 600
P4 002 P4 533 P3 211
P4 755
P4 531
Total
725 P1,P3, P4, P0, P2
45
An example system state

Existing Initially Available == Existing


ABC
All system tate at some t (may change)
10 5 7
resources
in the
Available = Allocation - total
system
Need = Max - Allocation
Allocation Max need Available Remaining
ABC ABC ABC Need
P0 010 ABC
P0 753 P0 332
P1 200 P1 322 P0 743
P1 532
P2 302 P2 902 P2 743 P1 122
P3 211 P3 4 22 P3 745 P2 600
P4 002 P4 533 P3 211
P4 755
P4 531
Total
Available
725 ABC
P1,P3, P4, P0, P2 46 10 5 7
Deadlock Detection

• Allow system to enter deadlock state

• Detection algorithm

• Recovery scheme

47
Single Instance of Each Resource Type

48
References

• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 7th, 8th, 9th editions, Wiley.

REFERENCES
• Operating System Concepts, Silberschatz et al. Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum.

49
50

You might also like