操作系统概念(英文).ppt_第1页
操作系统概念(英文).ppt_第2页
操作系统概念(英文).ppt_第3页
操作系统概念(英文).ppt_第4页
操作系统概念(英文).ppt_第5页
已阅读5页,还剩77页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter 7 Deadlocks,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,2,An Introduction to Chapter 7 (Two parts),Resource-allocation and Deadlock concepts of deadlocks resource-allocation models (7.1) necessary conditions for deadlock (7.2.1) descriptions of resource-allocation(7.2.2) Methodologies of deadlock handling (algorithms) principles (7.3) deadlock prevention (7.4) deadlock avoidance (7.5) deadlock detection-recovery (7.6, 7.7) Exercises!,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,3,7.0 Concepts of Deadlock,Definition a situation or system states in which a set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set, and these processes will never proceed. E.g.1 there are two tape drivers A and B in the system P1 and P2 each hold one tape drive and each needs another one. semaphores A and B corresponding to two tape drivers respectively, initialized to 1 P1 P2 wait (A); wait(B) wait (B); wait(A),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,4,P1,P2,A,B,holds A,waits for A,Fig.0.1 Deadlock Concept,Concepts of Deadlock (cont.),holds B,waits for B,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,5,E.g.2 Bridge Crossing,Concepts of Deadlock (cont.),Fig. 7.0.2,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,6,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. Refer to Appendix 7.A for more details about formal descriptions of deadlocks,Concepts of Deadlock (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,7,7.1 System Model,System model resource-allocation model Finite number of resources in systems, resource types R1, R2, . . ., Rm physical resources, e.g., CPU cycles, memory space, I/O devices logical resources, e.g., files, semaphores, monitors Each resource type Ri has Wi instances, a number of competing processes request the instances of resource types. Each process utilizes a resource as follows: request ( as a system call, in queue of blocked processes waiting for the resource) use release ( as a system call),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,8,If resources are controlled by semaphores, requesting and releasing resources are conducted by wait and signal operations. Resources can be classified as sharable resources, being used by processes concurrently non-sharable resources/critical resources, being used by processes exclusively as far as process synchronization and deadlock are concerned, non-sharable resources are often referred to,7.1 System Model (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,9,7.2 Deadlock Characterization,7.2.1 Necessary conditions for deadlock If deadlock arises, then the following four conditions hold simultaneously mutual exclusion a resource ( or a resource instance) can be used at one time by only one process hold and wait (or部分分配) 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.,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,10,circular wait there exists a set P0, P1, , 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, , Pn1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.,7.2.1 Necessary Conditions (cont.),Fig. 7.0.3,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,11,Principles of deadlock handling if deadlock occurs then (mutual exclusion) and (hold and wait ) and (no preemption) and (circular wait ) /* 等价逆否命题: if not (mutual exclusion) or not (hold and wait ) or not (no preemption) or not (circular wait ) then deadlock will not occur,7.2.1 Necessary Conditions (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,12,通过破坏四个必要条件之一,阻止死锁的发生. 实际系统中主要采取破坏hold and wait 和circular wait的方法 涉及到 死锁的预防 死锁的避免 死锁的检测与恢复,7.2.1 Necessary Conditions (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,13,7.2.2 Resource-allocation graph A system resource allocation graph is a directed graph and consists of 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. E is partitioned into two types request edge directed edge P1 Rj assignment edge directed edge Rj Pi,7.2 Deadlock Characterization (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,14,Representation of resource-allocation graph E process resource type with 4 instances,Pi requests instance of Rj Pi is holding an instance of Rj,Pi,Rj,Pi,Rj,7.2.2 Resource-Allocation Graph (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,15,E.g.1 Fig.7.2 no cycle , no deadlock,Fig.7.2,7.2.2 Resource-Allocation Graph (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,16,P1 R1 P2 R3 P3 R2P1 P2 R3 P3 R2P2,E.g.2 F.g.7.3 resource allocation graph with a deadlock two cycles in the graph,F.g.7.3,7.2.2 Resource-Allocation Graph (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,17,E.g.3 Fig.7.4 resource allocation graph with a cycle but no deadlock resource instance of R2 occupied by P4 is released to P3 after being used,Fig.7.4,7.2.2 Resource-Allocation Graph (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,18,Conclusions ! if the graph contains no cycles no deadlock in the system if graph contains a cycle if only one instance per resource type, then deadlock if several instances per resource type, possibility of deadlock.,7.2.2 Resource-Allocation Graph (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,19,7.3 Methods for Handling Deadlocks,Deadlock can be handled in several ways as follows: Ensure that the system will never enter a deadlock state deadlock prevention deadlock avoidance Allow the system to enter a deadlock state and then recover. deadlock detection and recovery Ignore the problem and pretend that deadlocks never occur in the system, i.e. OS does not handle deadlock. user or other systems are responsible for deadlock handling used by most operating systems, including UNIX/Linux (?),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,20,7.4 Deadlock Prevention,7.4.1. The principle Ensuring that at least one of the necessary conditions cannot hold by constraining the ways resources request can be made /*通过破坏四个必要条件之一,阻止死锁的发生 /*实际系统中主要采取破坏hold and wait 和circular wait的方法,.,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,21,7.4.2 Methodologies for deadlock prevention on the basis of four necessary conditions 1. With respect to Mutual Exclusion Method: mutual exclusion constraints can be relaxed for not sharable resources (e.g., read-only files), i.e. permitting several processes to access sharable resources simultaneously. However, mutual exclusion constraint must hold for nonsharable resources (e.g., printers). Conclusions cannot prevent deadlock by denying the mutual-exclusion condition,7.4 Deadlock Prevention (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,22,2. In view of Hold and Wait Method must guarantee that whenever a process requests a resource, it does not hold any other resources. Protocol 1 the process can only request and be allocated all required resources before it begins execution /* 预先全部分配所需资源 Protocol 2 the process is allowed to request resources several times during its lifetime it must release all the resources that it is currently allocated before it requests any additional resources, e.g. 2PL in DBS,7.4.2 Methodologies for Deadlock Prevention (cont.),Fig. A concurrent schedule S in DBS under 2PL protocol constraints,T1 wait(A) wait(B) read (A) read (B) signal (B) write (A) signal (A),T2 wait (B) read (B) write (B) signal (B),T1和T2并发访问数据向A和B,growing phase,shrinking phase,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,24,E.g. P253. processes and tape driver, disk files and printers. Conclusions an applicable method demerits: low resource utilization; starvation possible. 3. In view of No Preemption Principle /*允许进程抢占处于waiting态的进程所占有的资源 To ensure No Preemption does not hold, the protocol as follows is used if P1 is now holding some resources R1 and requests another resource R2, which is now occupied by P2 and cannot be immediately allocated to P1,7.4.2 Methodologies for Deadlock Prevention (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,25,for example, P2 is in the running state and using R2, then refer to Fig. 7.0.4(a) P1 turns into the waiting state and R1 held by P1 is released preempted resources R1 is added to the list of resources for which the process P3 is waiting, so the waiting process P3 may be enabled to run the resources-preempted process P1 will be restarted only when it can regain its old resources, as well as the new ones that it is requesting,7.4.2 Methodologies for Deadlock Prevention (cont.),P1,R1,P2,R2,(a) R1 is released and P1 turns into waiting state, thus the waiting process P3 will be enabled to run,P1,P2,R1,P3,Fig. 7.0.4,R2,waiting,request,request,(b) P1 preempts R1 from the waiting process P2,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,27,if P1 requests R1 that is not available, and if R1 is allocated to a waiting process P2 that is waiting for another resource R2, then P1 preempts R1 from the waiting process P2 refer to Fig. 7.0.4(b) Conclusion this method can be applied to resources whose state can be easily saved and restored later, such as CPU registers and memory,7.4.2 Methodologies for Deadlock Prevention (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,28,4. In view of Circular Wait Method to ensure this condition cannot hold impose a total ordering of all resource types require that each process requests resources in an increasing order of enumeration. /* 有序资源使用法 Implementation R=R1, R2, R3, Rm, resource ordering function F: RN e.g. F(tape driver) =1, F(disk driver) =5, F(printer) =12,7.4.2 Methodologies for Deadlock Prevention (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,29,Protocol 1 if Pi has requests or holds the instances of Ri, then afterwards it can only request resource Rj such that F(Rj) F(Ri) /* 按资源序号递增的顺序请求资源,Pi,Rj,Ri,F(Rj) F(Ri),7.4.2 Methodologies for Deadlock Prevention (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,30,Protocol 2 whenever Pi requests an instances of Rj , it must have released all resources Ri such that F(Ri) F(Rj) Conclusion in line with these two protocols, the Circular Wait condition cannot hold and the deadlock is prevented more commonly used, as the basis of deadlock avoidance and detection,Pi,Rj,Ri,F(Ri) F(Rj) F(Rk) F(Rj),Rk,7.4.2 Methodologies for Deadlock Prevention (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,31,7.5 Deadlock Avoidance,7.5.0 Principles Assuming OS resources allocator knows some additional a priori information about how resources are to be requested by processes each process declares the maximum number of resources of each type that it may need When a process requests resources, on the basis of these a priori information, the allocator uses deadlock-avoidance algorithm to examines the resource-allocation state to decide whether the current request can be satisfied or must wait to avoid a possible future deadlock. to ensure that there can never be a circular-wait condition,November 2013,Operating System Concepts - Chapter 7 Deadlocks -,32,Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. e.g. 7.5.1 Safe state Intuitively, system is in safe state if it can allocate resources to processes and still avoid deadlock When a process requests an available resource, the system must decide if immediate allocation leaves the system in a safe state.,7.5 Deadlock Avoidance (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,33,Definition formally, the system is in the safe state only if there exists a safe sequence of all processes executing sequence is safe for the current allocation state, if for each Pi, the resources that Pi can still request can be satisfied by currently available resources plus resources held by all the Pj, with ji. if the resource needs of Pi are not immediately available, then Pi can wait until all Pj have finished when Pj finished, Pi obtains needed resources, executes, returns allocated resources and terminates,7.5.1 Safe State (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,34,Note for a safe state, there may exist more than one safe process sequences Conclusion ! ( 记住 ) if a system is in a safe state no deadlocks if a system is in an unsafe state possibility of deadlock. avoidance ensure that a system will never enter an unsafe state. the safe state is a sufficient condition for non-deadlock, refer to Fig.7.5 E.g. P257 in the textbook,7.5.1 Safe State (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,35,F.g.7.5 Safe, Unsafe, Deadlock State,7.5.1 Safe State (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,36,7.5.2. Resource-allocation graph algorithm This algorithm can only be applied to the systems with only one instance of each resource type. Resource-Allocation Graph G (V,E) ( Fig.7.6/7.7) vertex: processes, resources edge: request edge, assignment edge, claim edge claim edge Pi Rj indicated that process Pi may request resource Rj; represented by a dashed line. claim edge converts to request edge, when a process requests a resource. assignment edge reconverts to a claim edge, when a resource is released by a process,7.5 Deadlock Avoidance (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,37,. Cycle-checking based deadlock avoid algorithm resources must be claimed a priori in the system, i.e. the graph can be built beforehand. the system state is safe, that is, there is no deadlock, only if there is no cycle in the resource-allocation graph the resource requests from processes are granted only if these requests do not result in the formation of a cycle in the resource-allocation graph,7.5.2 Resource-Allocation Graph Algorithm (cont.),E.g.1 Resource-allocation graph for deadlock avoidance (Fig.7.6),E.g.2 Unsafe state in resource-allocation graph (Fig.7.7),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,39,7.5.3 Bankers algorithm ! Applicable to the system with multiple instances of each resource type, but less efficient. Principles each process must declare the maximum number of instances of each resource type that it may need. when a process requests a set of resources, the system determines whether the allocation of these resources will leave the system in a safe state, and then the process may get the resources or have to wait. when a process gets all its resources, it must return/release them in a finite amount of time.,7.5 Deadlock Avoidance (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,40,Data structures for the Bankers algorithm n : number of processes, m: number of resources types. Availablem: if available j = k, there are k instances of resource type Rj available/idle Maxn, m: if Max i, j = k, then process Pi may request at most k instances of resource type Rj. Allocationn, m: if Allocationi, j = k, then Pi is currently allocated k instances of Rj. Needn, m: if Needi, j = k, then Pi may need k more instances of Rj to complete its task. Need i, j = Maxi, j Allocation i, j,7.5.3 Bankers Algorithm (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,41,Safety Algorithm (7.5.3.1) goal: to determine whether or not the system is in a safe state, i.e. to evaluate a safe system state algorithm steps Step1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available /* 系统空闲/可用/回收资源 Finish i = false for i = 1,2, , n. /*Pi是否已结束*/ Step2. Find an i such that both: /*找出1个可安全分配资源的进程 Pi*/ (a) Finish i = false (b) Needi Work if no such i exists, go to step 4.,7.5.3 Bankers Algorithm (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,42,Step3. Work = Work + Allocationi Finishi = true go to step 2 /*继续考察其它进程 Step4. If Finish i = true for all i, then the system is in a safe state. otherwise, the system is in a unsafe state,/* Pi执行; Pi结束,释放其占有的资源*/,7.5.3 Bankers Algorithm (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,43,Resource-Request Algorithm for Process Pi (7.5.3.2) Goal: to evaluate a safe resource request from a process Pi Requesti m: request vector for process Pi. Requesti j = k : process Pi wants k instances of resource type Rj. note: for process Pi , its resource requirement Needi can be satisfied through several Requesti1 , Requesti2 , , Requestik , where Requesti1 + Requesti2 + , Requestik = Needi,7.5.3 Bankers Algorithm (cont.),November 2013,Operating System Concepts - Chapter 7 Deadlocks -,44,algorithm steps when Pi makes a resource request Requesti m

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论