东南大学操作系统课件(汪芸老师版)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Chapter 7: Deadlocks
g
Agenda
•The Deadlock Problem
System Model
•System Model
•Deadlock Characterization •Methods for Handling Deadlocks
( Prevention, Avoidance, Detection and (Prevention Avoidance Detection and Recovery )
The Deadlock Problem
•A set of blocked processes each holding a resource and waiting to acquire a resource
g q
held by another process in the set. Example
•Example
–System has 2 disk drives.
–P
1and P
2
each hold one disk drive and each
needs another one.
Bridge Crossing Example
g g p
The Deadlock Problem •Example
p,
–semaphores A and B, initialized to 1
P0P1
wait (A);
wait(A);wait(B)
wait (B);wait(A)
g
Agenda
•The Deadlock Problem
System Model
•System Model
•Deadlock Characterization •Methods for Handling Deadlocks
( Prevention, Avoidance, Detection and (Prevention Avoidance Detection and Recovery )
y
System Model
•Resource types R 1, R2, . . ., R m
CPU cycles, memory space, I/O devices •Each resource type R i has W i instances.•Each process utilizes a resource as
Each process utilizes a resource as
follows:
–request
–use
–release
Deadlock Characterization
Deadlock 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.
h ld b h
Deadlock Characterization
No preemption:a resource can be released •No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task that process has completed its task.
•Circular wait:there exists a set {P 0, P 1, …, P 0} of waiting processes such that 0is waiting for a of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1is waiting for a resource that is held by P , …, P is waiting for y 2,,n –1g a resource that is held by P n , and P 0is waiting for a resource that is held by P 0.
Resource-Allocation Graph p
A set of vertices V and a set of edges E .•V is partitioned into two types:={}the set consisting of all –P = {P 1, P 2, …, P n }, the set consisting of all the processes in the system.–R = {R 1, R 2, …, R m }, the set consisting of all resource types in the system resource types in the system.
Resource-Allocation Graph
p
•request edge –directed edge P1 →R j •assignment edge –directed edge R j→P i
Resource-Allocation Graph
(Cont.)
(Cont)
•Process
Resource Type with 4 instances
•P requests instance of R P i R j
i j
•P i is holding an instance of R j P i R j is holding an instance of
Example
p
Resource Allocation Graph With A Deadlock
A Deadlock
Graph With A Cycle But No Deadlock
N D dl k
Comments
•If graph contains no cycles ⇒no
deadlock.
g p y
•If graph contains a cycle ⇒
if only one instance per resource type,
then deadlock.
then deadlock
if several instances per resource type,
possibility of deadlock
possibility of deadlock.
g
Agenda
•The Deadlock Problem
System Model
•System Model
•Deadlock Characterization •Methods for Handling Deadlocks
( Prevention, Avoidance, Detection and (Prevention Avoidance Detection and Recovery )
Methods for Handling
Deadlocks
•Ensure that the system will never enter a E th t th t ill t deadlock state.
•Allow the system to enter a deadlock state and then recover.
state and then recover
•Ignore the problem and pretend that deadlocks never occur in the system;
y p g y,
used by most operating systems, including UNIX.
Tolerance & Recovery
Detection
Prevention & Avoidance
Deadlock Prevention
•Mutual Exclusion–not required for sharable resources; must hold for
h bl t h ld f nonsharable resources.
Deadlock Prevention
•Hold and Wait–must guarantee that whenever a process requests a resource, whenever a process requests a resource it does not hold any other resources.
–Low resource utilization; starvation possible.
Deadlock Prevention
•No Preemption–
y
–Process will be restarted only when it can
regain its old resources, as well as the new ones that it is requesting.
q g
Deadlock Prevention
•Circular Wait–impose a total ordering of all resource types, and require that each
ll t d i th t h process requests resources in an increasing order of enumeration.
Deadlock Avoidance
Requires that the system has some additional
a priori information available.
•Each process declare the maximum number of
resources of each type that it may need. resources of each type that it may need •Dynamically examines the resource-allocation
state to ensure that there can never be a state to ens re that there can ne er be a circular-wait condition.
Deadlock Avoidance
•Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of resources,and the maximum demands of the processes.
Safe State
•System is in safe state if there exists a q1,2,,n
sequence <P, P, …, P> of ALL the processes is the systems such that for each P i, the resources that P i requests each P the resources that P i requests can be satisfied by currently available resources + resources held by all the P j, resources+resources held by all the with j < i.
Safe State Safe State
–If P
i resource needs are not immediately
available, then P
i can wait until all P
j
have
f inished.
–When P is finished, P
i can obtain needed
j
resources, execute, return allocated resources, and terminate.
–When P
i terminates, P
i +1
can obtain its
needed resources, and so on.
,
Comments
•If a system is in safe state ⇒no
If t i i f t t
deadlocks.
•If a system is in unsafe state ⇒possibility of deadlock.
•Avoidance ⇒ensure that a system will A id th t t ill never enter an unsafe state.
Safe, Unsafe , Deadlock State Deadlock State
Avoidance algorithms
g
•Single instance of a resource type. Use a resource-allocation graph
Use a resource allocation graph
•Multiple instances of a resource type. Use the banker’s algorithm
Resource-Allocation Graph
p
Unsafe State Unsafe State
Resource-Allocation Graph
Algorithm
•Suppose that process P i requests a resource R j
The request can be granted only if •The request can be granted only if converting the request edge to an assignment edge does not result in the assignment edge does not result in the formation of a cycle in the resource
ll ti h
allocation graph
g
Banker’s Algorithm
•Multiple instances.
M ltiple instances
p p
•Each process must a priori claim
maximum use.
•When a process requests a resource it 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 must return them in a finite amount of
time.
Data Structures
Data Structures
Let n= number of processes, and m = number of resources types. •Available:Vector of length m. If available [j]
yp j = k, there are k instances of resource type R
available.
•Max: n x m matrix. If Max [i,j] = k, then
:n x m matrix If Max i j]=then process P i may request at most k instances
f t
of resource type R j.
Data Structures
•Allocation: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k
th i tl ll t d instances of R j.j
•Need: n x m matrix. If Need[i,j] =k, then P i may need k more instances of R j to
may need more instances of to complete its task.
i j]i j]i j]
Need[i,j]= Max[i,j] –Allocation[i,j].
Safety Algorithm
y g
1.Let Work and Finish be vectors of
g,p y
length m and n, respectively. Initialize: Work = Available
Finish [i] =false for i= 0, 1, …, n-1.
2.Find any i such that both:
(a) Finish i] = false
()[]
≤Work
(b) Need
i
If no such i exists,go to step4.
If no such i exists, go to step 4.
y g
Safety Algorithm
3.Work= Work + Allocation i
Finish[i] =true
]=
go to step 2.
4.If Finish[i] == true for all i, then the 4If Fi i h]t f ll th th system is in a safe state.
for Process P
for Process
i
1.If Request i≤Need i go to step
2.
,, Otherwise, raise error condition, since process has exceeded its maximum
claim.
2.If Request i≤Available, go to step
3.
Otherwise P i must wait, since
Otherwise must wait since
resources are not available.
for Process
for Process P
i
3.Pretend to allocate requested resources to P
i by modifying the state as follows:
Available= Available –Request;
Allocation= Allocation+ Request;
i i i
Need
i =Need
i
–Request
i
;
z If safe the resources are allocated to Pi.

z If unsafe ⇒Pi must wait, and the old resource-allocation state is restored
Example
p
• 5 processes P
0 through P
4
;
3resource types:
3 resource types:
A(10 instances),
B(5instances),
C(7 instances).
Example
p
:
S h t t ti
•Snapshot at time T
Allocation Max Available
A B C A B C A B C
0 1 07 5 3 3 3 2
P
010753332
2 0 0
3 2 2
P
1
3 0 2 9 0 2
P
302902
2
2 1 1 2 2 2
P
3
P
0 0 2 4 3 3
4
Example
p
•The content of the matrix Need is defined to be Th t t f th t i N d i d fi d t b Max–Allocation.
Need
A B C
P
7 4 3
1 2 2
122
P
1
6 0 0
P
2
P
0 1 1
3
P 4 3 1
4
Example
The system is in a safe state since the •The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
safety criteria
Example: P Request (1,0,2)
p1q()
•Check that Request ≤Available (that is, (1,0,2) Check that Request Available(that is(102)≤(3,3,2) ⇒true.
Allocation Need Available
All ti N d A il bl
A B C A B C A B C
P
0 1 0 7 4 3 2 3 0
3 0 20 2 0
P
302020
1
3 0 1 6 0 0
P
2
2 1 1 0 1 1
211011
P
3
0 0 2 4 3 1
P
4
Example: P Request (1,0,2)
p1q()
•Executing safety algorithm shows that
E ti f t l ith h th t
sequence < P, P, P, P, P> satisfies
13402
safety requirement.
•Can request for (3,3,0) by P4be granted?
Can request for(330)by be granted?•Can request for (0,2,0) by P0be granted?
Deadlock Detection
•Allow system to enter deadlock state •Detection algorithm
•Recovery scheme
Single Instance of
Each Resource Type
Each Resource Type
•Maintain wait-for graph
Nodes are processes
–Nodes are processes.
–P
→P j if P i is waiting for P j.
i
y g •Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle there exists a deadlock
is a cycle, there exists a deadlock.
Single Instance of
Each Resource Type
E h R T
•An algorithm to detect a cycle in a graph requires an order of n2operations,
e e s t e u be o e t ces t e where n is the number o
f vertices in the graph.
Resource-Allocation Graph
and Wait-for Graph
d W it f G h
Resource-Allocation Graph Corresponding wait-for graph。

相关文档
最新文档