Sunteți pe pagina 1din 39

Deadlock and Starvation

Fred Kuhns
(fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk)

Department of Computer Science and Engineering


Washington University in St. Louis

Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Introduction to deadlock
• Typical scenario for a process P:
– request exclusive access to some resource R
– process P uses the resource
– P then releases or returns the resource R to the system.

• If two processes request R, one must block to ensure sequential


access to R

• “a set of processes is in a deadlock state when every process in the


set is waiting for an event that can be caused only by another
process in the set”, Silberschatz et. al., “Operating Systems
Concepts”

• There is no efficient solution

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 2


Reusable/Consumable Resources
• Reusable resource:
– Description:
• 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:
• Processor time, I/O channels, main and secondary memory, files, databases,
and semaphores

• Consumable resources
– Description:
• Created (produced) and destroyed (consumed) by a process
– Examples:
• Interrupts, signals, messages, and information in I/O buffers

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 3


Bridge Crossing Example

• Traffic only in one direction.

• Each section of a bridge can be viewed as a resource.

• Deadlock can occur if two cars enter the bridge from opposite sides
– 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
– timid driver hesitates to enter bridge

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 4


Consumable Resource Example
• Deadlock occurs if receive is blocking

P1 P2
... ...
Receive(P2); Receive(P1);
... ...
Send(P2); Send(P1);

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 5


Necessary Conditions for Deadlock
• 1) Mutual exclusion:
– One process holds a resource in a non-sharable mode.
– Other processes requesting resource must wait for resource to be
released.

• 2) Hold-and-wait:
– A process must hold at least one allocated resource while awaiting one or
more resources held by other processes.

• 3) No preemption:
– Resources not forcibly removed from a process holding it, the holding
process must voluntarily released it.

• 4) Circular wait
– a closed chain of processes exists, such that each process holds at least
one resource needed by the next process in the chain.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 6


System Model - Reusable Resources
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices

• Each resource type Ri has Wi instances.

• At any given time the system has N processes,


designated by Pi

• Each process utilizes a resource as follows:


– request – block if resource is not available
– use – process “owns” resource, but may elect to share with other
processes
– release – may result in a blocked processes being woken up

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 7


Resource-Allocation Graph
• A set of Vertices V and a set of Edges E

• V is partitioned into:
– P = {P1, P2, …, Pn}, all processes in the system.
– R = {R1, R2, …, Rm}, all resource types in the system.

• Request edge – directed edge Pi → Rj. Indicates Pi is


waiting for an instance of resource type Rj

• Assignment edge – directed edge Rj→ Pi. Indicates that


an instance of resource type Rj has been allocated to Pi.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 8


Resource-Allocation Graph

P
• Process Pi

• Resource Type Rj with 4 instances


Pi
Rj
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
R1 R2

• Deadlock P1 P2
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 9
Example of a Resource Allocation Graph

R1 R2 R1 R2 R1 R2

P1 P2 P3 P1 P2 P3 P1 P2 P3

R3 R4 R3 R4 R3 R4

Initial: P3 requests R3 P2 releases R1,


No Deadlock (Deadlock) P3 requests R3
(No Deadlock:
Why not?)
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 10
Basic Facts
• If graph contains no cycles ⇒
– no deadlock.

• If graph contains a cycle ⇒


– if only one instance per resource type, then deadlock.
– if several instances per resource type, possibility of deadlock.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 11


Approaches to Deadlock Handling
• Ensure system never enters a deadlocked state: Use protocol to
prevent or avoid deadlock:
– deadlock prevention scheme - ensure that at least one of the necessary
conditions cannot hold.
– deadlock avoidance scheme - requires the OS know in advance the
resource usage requirements of all processes. Then for each request the
OS decides if it could lead to a deadlock before granting.
• Allow the system to enter a deadlock state and then recover
(detection and recovery).
– system implements an algorithm for deadlock detection, if so then
recover
• Assume deadlocks never occur in the system
– used by most operating systems, including UNIX.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 12


Deadlock Prevention
• At least 1 necessary condition does not hold
– Mutual Exclusion: not required for sharable resources; must hold for non-
sharable resources.

– Hold-and-Wait: can not request new when holding resources.


• Protocol 1: request all resources before it begins execution
• Protocol 2: request resources only when the process has none.
• Low resource utilization; starvation possible.

– No Preemption: preempt resources from processes


• Protocol 1: If a request can not be satisfied then preempt all resources held and
block
• Protocol 2: Preempt only if resources are needed by another running process
• Requires resource state to be easily restored

– Circular Wait: Block any request that results in a cycle.


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

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 13


More on Preventing Circular Wait
• Define F such that F: R -> N (i.e. each resource type is
assigned a unique integer.

• Assume process holds resource Ri

• Protocol 1: Request resource Rj iff F(Rj) > R(Ri).


– Must request multiple instances of same type at same time

• Protocol 2: Request resource Rj iff it has released all Ri


such that F(Ri) ≥ R(Rj).

• Prove that there can not be a circular wait.


Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 14
Deadlock Avoidance
• Requires that system have a priori information

• Simplest and most useful model requires that each process


declare the maximum number of resources of each type that it
may need.

• 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 resources
– number of allocated resources,
– maximum demands of the processes.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 15


Safe State
• Safe State: System can allocate resources to existing
processes in some order that prevents deadlock. i.e.
there is a safe sequence of all processes whose
allocation requests can be satisfied.

• Sequence <P1, P2, …, Pn> is safe if for each Pi, the


resources that Pi can still request can be satisfied by
currently available resources + resources held by all
the Pj, with j < i.
– 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.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 16
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.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 17


Resource-Allocation Graph Algorithm
• Must have no more than one instance of each resource
type.

• Claim edge Pi → Rj indicates process Pj may request


resource Rj; represented by a dashed line.

• Claim edge converts to request edge when a process


requests a resource.

• When a resource is released by a process, assignment


edge reconverts to a claim edge.

• Resources must be claimed a priori in the system.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 18


Resource-Allocation Graph:Deadlock Avoidance

Resource Allocation graph P2 requests R2,


Unsafe state (cycle)
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 19
Some Definitions

Definitions
Resource = R = (R1, R2, ..., Rm) Available = V = (V1, V2, ..., Vm)
Relationships
M11 M12 ... M1m A11 A12 ... A1m n
M21 M22 ... M2m A21 A22 ... A2m 1) Ri = Vi + Σ Aki, for all i
Max = M = Allocation = A =
... ... k=1
Mn1 Mn2 ... Mnm An1 An2 ... Anm 2) Mki ≤ Ri, for all k, i
3) Aki ≤ Mki, for all k, i
N11 N12 ... N1m
N21 N22 ... N2m
Need = N = Max - Allocation =
...
Nn1 Nn2 ... Nnm

• If X and Y are vectors then X < Y iff X[i] < Y[i] for all i
• Row i of a matrix corresponds to process Pi. So Mi is the set of
maximum allocations for process i.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 20
Deadlock Avoidance Policies
• Deadlock Avoidance Policy – Process Initiation Denial
– Start if the max claim of all process can be met (including new
process)
– Start a new process Pn+1 only if the below inequality holds

n
Ri ≥ M(n+1)i + Σ Mki for all i
k=1

• Resource Allocation Denial: Banker’s Algorithm


– Each process must a priori claim maximum use (claims) which may not
exceed the total number of resources
– 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.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 21


Safety Algorithm
• Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i - 1,3, …, n.

• Find an i (that is, a Pi) such that both:


Finish [i] == false // Pi has not completed
Needi ≤ Work // Pi can allocate remaining Resources
If no such i exists, go to step 4.

• Set Work = Work + Allocationi // assume Pi returns resources


Set Finish[i] = True // Pi completes
go to step 2.

• If Finish [i] == True for all i, then the system is in a safe state.
Otherwise it is unsafe.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 22
Resource-Request Algorithm for Pi

If Requesti [j] = k,then process Pi wants k instances of


resource type Rj.
– If Requesti ≤ Needi,, then go to step 2.
Otherwise, raise error condition, (exceeded claim).

– If Requesti ≤ Available, then go to step 3.


Otherwise Pi must wait, resources not available.

– Pretend to allocate requested resources to Pi:


Available = Available - Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
• safe ⇒ the resources are allocated to Pi.
• unsafe ⇒ Pi must wait, and the old resource-allocation state is
restored
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 23
Example of Banker’s Algorithm
• 5 processes P0 through P4;
3 resource types A (10), B (5), and C (7).
• Snapshot at time T0:
Alloc Max Avail Need
ABC ABC ABC ABC
P0 010 753 332 743
P1 200 322 122
P2 302 902 600
P3 211 222 011
P4 002 433 431

• Is the system in a safe state?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 24


Checking for Safe State
• 5 processes P0 through P4;
3 resource types A (10), B (5), and C (7).
• Snapshot at time T0:
Alloc Max Avail Need
ABC ABC ABC ABC
P0 010 753 332 743
P1 200 322 122
P2 302 902 600
P3 211 222 011
P4 002 433 431
• Work = (3 3 2)
• Allocate P1, Need1 (1 2 2) <= Work (3 3 2); Work = Work + (2 0 0) = (5 3 2)
• Allocate P3, Need3 (0 1 1) <= Work (5 3 2); Work = Work + (2 1 1) = (7 4 3)
• Allocate P4, Need4 (4 3 1) <= Work (7 4 3); Work = Work + (0 0 2) = (7 4 5)
• Allocate P2, Need2 (6 0 0) <= Work (7 4 5); Work = Work + (3 0 2) = (10 4 7)
• Allocate P0, Need0 (7 4 3) <= Work (10 4 7); Work = Work + (0 1 0) = (10 5 7)
All Fred
finished so Safe.
Kuhns (01/17/09) CS422 – Operating Systems Concepts 25
Example P1 Request (1,0,2)
• Check that Request ≤ Available
– (that is, (1,0,2) ≤ (3,3,2) ⇒ true: Request[i] ≤ Avail[i])
Request1 Alloc Max Avail Need
(1,0,2) A B C ABC ABC ABC
PAlloc
0 010 753 332 743
1 Need1
P(3,0,2)
1 200 322 Avail 1 2 2 (0,2,0)
(2,3,0)
P2 302 902 600
P3 211 222 011
P4 002 433 431
• Executing safety algorithm shows that sequence <P1, P3,
P4, P0, P2> satisfies safety requirement.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 26


Deadlock Detection
• Allow system to enter deadlock state

• Detection algorithm

• Recovery scheme

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 27


Single Instance each Resource Type: Wait-for graph
P0
P0
R1 R2
P1 P2 P3
P1 P2 P3

R3 R4 P4

P4
Wait-For
Resource Allocation Graph
Graph
• Maintain wait-for graph
– Nodes are processes.
– Pi → Pj if Pi is waiting for Pj.
• Periodically invoke an algorithm that searches for a cycle in the
graph.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 28
Several Instances ea Resource Type
• Available: A vector of length m indicates the number of available
resources of each type.

• Allocation: An n x m matrix defines the number of resources of each


type currently allocated to each process.

• Request: An n x m matrix indicates the current request of each


process. If Request [ij] = k, then process Pi is requesting k more
instances of resource type. Rj.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 29


Detection Algorithm
• Work and Finish are vectors of length m and n, respectively.
Initialize:
– Work = Available
– if Allocationi ≠ 0, then Finish[i] = false else Finish[i] = true.

• Find an index i such that both:


– Finish[i] == false // has allocated resources
– Requesti ≤ Work // request can be filled
– If no such i exists, go to step 3
5. Find P that can finish
– Work = Work + Allocationi;
– Finish[i] = true;
– go to step 2
• If Finish[i] == false, for some i, 1 ≤ i ≤ n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 30


Example of Detection Algorithm
• Five processes P0 through P4
• three resource types A (7), B (2), and C (6).
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 31


Step 0
Total Resource {A = 7, B = 2, C = 6}
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002
Step 0:
Work = Available = {0, 0, 0}
Finish = {false, false, false, false, false}

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 32


Steps 1, 2 and 3
Allocation Request Steps 1 & 2:
ABC ABC • Request0 < work // {0 0 0}
Work = {0 1 0}
P0 010 000 Finish = {true, false, false, false, false}
P1 200 202 • Request2 < work // {0 1 0}
P2 303 000 Work = {3 1 3}
Finish = {true, false, true, false, false}
P3 211 100 • Request1 < work // {3 1 3}
P4 002 002 Work = {5 1 3}
Finish = {true, true, true, false, false}
• Request3 < work // {5 1 3}
Work = {7 2 4}
Step 3: Finish = {true, true, true, true, false}
• No deadlock since all elements of • Request4 < work // {7 2 4}
Finish = true Work = {7 2 6}
Finish = {true, true, true, true, true}

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 33


New Request from P2
• P2 requests an additional instance of type C
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 001
P3 211 100
P4 002 002
• State of system?
– Can reclaim resources held by process P0, but insufficient resources
to fulfill other processes; requests.
– Deadlock exists, consisting of processes P1, P2, P3, and P4.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 34


Steps 1, 2 and 3 with new P2 request
Allocation Request Steps 1 & 2:
ABC ABC • Request0 < work // {0 0 0}
P0 010 000 Work = {0 1 0}
Finish = {true, false, false, false, false}
P1 200 202
• No other Processes with Request < work.
P2 303 000
P3 211 100
P4 002 002

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 35


Detection-Algorithm Usage
• When, and how often, to invoke depends on:
– How often a deadlock is likely to occur?
– How many processes will need to be rolled back?
• one for each disjoint cycle

• If detection algorithm is invoked arbitrarily, there


may be many cycles in the resource graph and so we
would not be able to tell which of the many deadlocked
processes “caused” the deadlock.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 36


Recovery: Process Termination
• Abort all deadlocked processes.
• Abort one process at a time until the deadlock cycle is
eliminated.
• In which order should we choose to abort?
– Priority of the process.
– How long process has computed, and how much longer to
completion.
– Resources the process has used.
– Resources process needs to complete.
– How many processes will need to be terminated.
– Is process interactive or batch?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 37


Recovery: Resource Preemption
• Selecting a victim – minimize cost.

• Rollback – return to some safe state, restart process


for that state.

• Starvation – same process may always be picked as


victim, include number of rollback in cost factor.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 38


Combined Approach
• Combine the three basic approaches
– prevention
– avoidance
– detection
allowing the use of the optimal approach for each of resources in
the system.

• Partition resources into hierarchically ordered classes.

• Use most appropriate technique for handling deadlocks within


each class.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 39

S-ar putea să vă placă și