Download presentation
Presentation is loading. Please wait.
Published byLeslie Chandler Modified over 9 years ago
1
1 张惠娟 副教授 Ms.zhj@163.com 实用操作系统概念
2
2 内容框架 概述 体系结构 进程管理 内存管理 文件管理 外设管理
3
3 内容 Ch4: 进程 Ch5: 线程 Ch6: CPU 调度 Ch7 :进程同步 Ch8 :死锁
4
4 Ch 8: Deadlocks Deadlock Problem System Model Deadlock Characterization Resource-Allocation Graph Methods for Handling Deadlocks
5
5 The Deadlock Problem Reason Example
6
6 Reason A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. 资源竞争,是进程对资源要求在时间上的 冲突。 竞争可能产生死锁,但不一定会死锁。取 决于各个进程推进的速度和对资源请求的 顺序。 The Deadlock Problem
7
7 Example Example1 System has 2 tape drives. P 1 and P 2 each hold one tape drive and each needs another one. The Deadlock Problem
8
8 Example 2 semaphores A and B, initialized to 1 P 0 P 1 wait (A);wait(B) wait (B);wait(A) The Deadlock Problem
9
9 Example 3 : Bridge Crossing Example The Deadlock Problem
10
10 System Model Resource types R 1, R 2,..., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances. Each process utilizes a resource as follows: request use release
11
11 Deadlock Characterization 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.
12
12 Circular wait there exists a set {P 0, P 1, …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n – 1 is waiting for a resource that is held by P n, and P 0 is waiting for a resource that is held by P 0. Deadlock can arise if four conditions hold simultaneously Deadlock Characterization
13
13 Resource-Allocation Graph
14
14 Resource Allocation Graph With A Deadlock
15
15 Resource Allocation Graph With A Cycle But No Deadlock
16
16 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. Resource-Allocation Graph
17
17 Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Deadlock Recovery Combined Approach
18
18 Deadlock Prevention Mutual Exclusion not required for sharable resources; must hold for nonsharable resources. Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. Methods for Handling Deadlocks
19
19 Hold and Wait must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. Low resource utilization; starvation possible. Methods for Handling Deadlocks
20
20 No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. 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 is requesting. Methods for Handling Deadlocks
21
21 Deadlock Avoidance 有关概念 资源分配图算法 银行家算法(了解) Methods for Handling Deadlocks
22
22 有关概念 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. Methods for Handling Deadlocks
23
23 Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. Methods for Handling Deadlocks
24
24 System is in safe state if there exists a safe sequence of all processes. Sequence is safe if for each P i, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the P j, with j<I. Methods for Handling Deadlocks
25
25 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. Methods for Handling Deadlocks
26
26
27
27 资源分配图算法 Claim edge P i R j indicated that process P j may request resource R j ; 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. Methods for Handling Deadlocks
28
28 Resource-Allocation Graph For Deadlock Avoidance
29
29 Unsafe State In A Resource-Allocation Graph
30
30 Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme Methods for Handling Deadlocks
31
31 P2 P1 P2 P1 P2 (1) (2) (3)
32
32 Deadlock Recovery : Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. Methods for Handling Deadlocks
33
33 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? Methods for Handling Deadlocks
34
34 Deadlock Recovery : Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process fro that state. Starvation – same process may always be picked as victim, include number of rollback in cost factor. Methods for Handling Deadlocks
35
35 Combined Approach Combine the three basic approaches prevention avoidance detection Methods for Handling Deadlocks
36
36 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. Methods for Handling Deadlocks
37
37 小结 死锁产生的原因和条件 解除死锁的方法 进程-资源检测法
38
38 练习 简化下面进程-资源图,并说明是 否会产生死锁?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.