Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

Similar presentations


Presentation on theme: "Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."— Presentation transcript:

1 Operating System Concepts Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangChapter 8 Deadlocks In a multiprogramming environment Several processes may compete for a finite number of resources Several processes may compete for a finite number of resources A process requests resources If no available, the process enter a wait state. If no available, the process enter a wait state. Waiting process may never again change state Waiting process may never again change state Such a situation is called a deadlock A law passed by the Kansas legislature When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone. When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.

3 3Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

4 4Ku-Yaw ChangChapter 8 Deadlocks 8.1 System Model A process may utilize a resource in only the following sequence Request Request If this request cannot be granted immediately, the requesting process must wait. Use Use Release ReleaseExamples open and close file system calls open and close file system calls allocate and free memory system calls allocate and free memory system calls

5 5Ku-Yaw ChangChapter 8 Deadlocks Deadlock State 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. Resources Physical resources Physical resources printers, tape drives, memory space, CPU Logical resources Logical resources files, semaphores, monitors Other types of events Other types of events IPC facilities

6 6Ku-Yaw ChangChapter 8 Deadlocks Deadlock Examples Example System has 2 tape drives. System has 2 tape drives. P 1 and P 2 each hold one tape drive and each needs another one. P 1 and P 2 each hold one tape drive and each needs another one.Example semaphores A and B, initialized to 1 semaphores A and B, initialized to 1 P 0 P 1 P 0 P 1 wait (A);wait (B); wait (B);wait (A);

7 7Ku-Yaw ChangChapter 8 Deadlocks Bridge Crossing Example 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.

8 8Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

9 9Ku-Yaw ChangChapter 8 Deadlocks 8.2.1 Necessary Conditions A deadlock situation arise if the following four conditions hold simultaneously Mutual exclusion Mutual exclusion At least one resource must be held in a nonsharable mode Hold and wait Hold and wait Hold at least one resource and wait to acquire additional resources that are currently being held by other processes No preemption No preemption Resources cannot be preempted Circular wait Circular wait 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 n is waiting for a resource that is held by P 0.

10 10Ku-Yaw ChangChapter 8 Deadlocks 8.2.2 Resource-Allocation Graph System resource-allocation graph A directed graph for describing deadlocks A directed graph for describing deadlocks A set of vertices V Active processes Active processes P = {P 1, P 2, …, P n } Resource types Resource types R = {R 1, R 2, …, R n } A set of edges E A request edge A request edge P i -> R j An assignment edge An assignment edge R j -> P i PiPi PiPi RjRj RjRj 4 instances

11 11Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph Sets P, R, and E P = {P 1, P 2, P 3 } R = {R 1, R 2, R 3, R 4 } E = {P 1 ->R 1, P 2 ->R 3, R 1 ->P 2, R 2 ->P 2, R 2 ->P 1, R 3 ->P 3 } Resource instances R 1 : one instance R 2 : two instances R 3 : one instance R 4 : three instances

12 12Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph If the graph contains no cycles No process in the system is deadlocked No process in the system is deadlocked If the graph does contain a cycle A deadlock may exist A deadlock may exist

13 13Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph P 3 requests an instance of R 2 Two minimal cycles exist P 1 -> R 1 -> P 2 -> R 3 -> P 3 -> R 2 -> P 1 P 2 -> R 3 -> P 3 -> R 2 -> P 2 Processes P 1, P 2, and P 3 are deadlocked

14 14Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph A cycle exists P 1 -> R 1 -> P 3 -> R 2 -> P 1 No deadlock P 4 may release its instance of R 2 That resource can the be allocated to P 3, breaking the cycle

15 15Ku-Yaw ChangChapter 8 Deadlocks Deadlock Characterization In summary, if a resource-allocation graph Does not have a cycle Does not have a cycle The system is not in a deadlock state Have a cycle Have a cycle The system may or may not be in a deadlock state

16 16Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

17 17Ku-Yaw ChangChapter 8 Deadlocks 8.3 Methods for Handling Deadlocks Deal with the deadlock problem in one of three ways Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state Allow the system to enter a deadlock state, detect it, and recover. Allow the system to enter a deadlock state, detect it, and recover. Ignore the problem altogether, and pretend that deadlocks never occur in the system Ignore the problem altogether, and pretend that deadlocks never occur in the system Deadlocks occurs infrequently (say, once per year)

18 18Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

19 19Ku-Yaw ChangChapter 8 Deadlocks 8.4 Deadlock Prevention Ensure at least one of the four necessary conditions cannot hold Mutual Exclusion Mutual Exclusion Hold and Wait Hold and Wait No Preemption No Preemption Circular Wait Circular Wait

20 20Ku-Yaw ChangChapter 8 Deadlocks Deadlock Prevention Mutual Exclusion Must hold for nonsharable resources ( ex. a printer) Must hold for nonsharable resources ( ex. a printer) We cannot prevent deadlocks by denying the mutual- exclusion condition We cannot prevent deadlocks by denying the mutual- exclusion condition Some resources are intrinsically nonsharable Hold and Wait Whenever a process requests a resource, it does not hold any other resources Whenever a process requests a resource, it does not hold any other resources Request and be allocated all resources before execution Request resources only when the process has none Main disadvantages Main disadvantages Low resource utilization Starvation is possible

21 21Ku-Yaw ChangChapter 8 Deadlocks Deadlock Prevention 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. 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. 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. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Applied to resources whose state can be easily saved and restored Applied to resources whose state can be easily saved and restored Such as CPU registers and memory spaces No applied to printers and tape drives

22 22Ku-Yaw ChangChapter 8 Deadlocks Deadlock Prevention Circular Wait Impose a total ordering of all resource types Impose a total ordering of all resource types A one-to-one function F: R -> N N is the set of natural numbers N is the set of natural numbers require that each process requests resources in an increasing order of enumeration require that each process requests resources in an increasing order of enumeration Initially request any number of instances of a resource type R i Request instances of resource type R j if and only if F(R j )>F(R i ) Example Example F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12

23 23Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

24 24Ku-Yaw ChangChapter 8 Deadlocks 8.5 Deadlock Avoidance Possible side effects of preventing deadlocks Low device utilization Low device utilization Reduced system throughput Reduced system throughput Avoid deadlock Require additional information about how resources are to be requested Require additional information about how resources are to be requested Simplest and most useful model Simplest and most useful model Each process declare the maximum number of resources of each type that it may need Possible to ensure that the system will never enter a deadlock state

25 25Ku-Yaw ChangChapter 8 Deadlocks 8.5.1 Safe State If the system can allocate resources to each process in some order and still avoid a deadlock A safe sequence A safe sequence for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i. for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i. If P i resource needs are not immediately available, then P i can wait until all P j have finished. If P i resource needs are not immediately available, then P i can wait until all P j have finished. When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. When P i terminates, P i+1 can obtain its needed resources, and so on. When P i terminates, P i+1 can obtain its needed resources, and so on. Unsafe : no such sequence exists

26 26Ku-Yaw ChangChapter 8 Deadlocks Basic Facts If a system is in safe state no deadlocks no deadlocks If a system is in unsafe state possibility of deadlock. possibility of deadlock.Avoidance ensure that a system will never enter an unsafe state. ensure that a system will never enter an unsafe state.

27 27Ku-Yaw ChangChapter 8 Deadlocks An Example A system with 12 magnetic tape drives and 3 processes: P 0, P 1, and P 2 The sequence satisfies the safety condition. The sequence satisfies the safety condition. Maximum Needs Current Needs P0P0P0P0105 P1P1P1P142 P2P2P2P292

28 28Ku-Yaw ChangChapter 8 Deadlocks An Example Process P 2 requests and is allocated 1 more tape drive The system is no longer in a safe state. The system is no longer in a safe state. If a process requests a resource that is currently available, it may still have to wait. Maximum Needs Current Needs P0P0P0P0105 P1P1P1P142 P2P2P2P293

29 29Ku-Yaw ChangChapter 8 Deadlocks 8.5.2 Resource-Allocation Graph Algorithm Resource-allocation graph with only one instance of each resource type Used for deadlock avoidance Used for deadlock avoidance Claim edge P i -> R j (represented by a dashed line) Process P j may request resource R j Process P j may request resource R j Converts to request edge when a process requests a resource. 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. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system. Resources must be claimed a priori in the system.

30 30Ku-Yaw ChangChapter 8 Deadlocks Resource-Allocation Graph For Deadlock Avoidance

31 31Ku-Yaw ChangChapter 8 Deadlocks Unsafe State In Resource-Allocation Graph

32 32Ku-Yaw ChangChapter 8 Deadlocks 8.5.3 Banker’s Algorithm Multiple instances resource-allocation graph allocation is not applicable resource-allocation graph allocation is not applicable A new process must declare the maximum number of instances of each resource type that it may need. Cannot exceed the total number of resources in the system Cannot exceed the total number of resources in the system Determine whether the allocation of these resources will leave the system in a safe state If yes, the resources are allocated If yes, the resources are allocated If not, the process must wait until some other process releases enough resources If not, the process must wait until some other process releases enough resources

33 33Ku-Yaw ChangChapter 8 Deadlocks 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 R j available. Max: n x m matrix. If Max [ i, j ] = k, then process P i may request at most k instances of resource type R j. Allocation: n x m matrix. If Allocation[ i, j ] = k then P i is currently allocated k instances of R j. Need: n x m matrix. If Need[ i, j ] = k, then P i may need k more instances of R j to complete its task. Need [ i, j ] = Max[ i, j ] – Allocation [ i, j ]. Need [ i, j ] = Max[ i, j ] – Allocation [ i, j ].

34 34Ku-Yaw ChangChapter 8 Deadlocks Notation for the Banker’s Algorithm Let X and Y be vectors of length n. X ≤ Y if and only if X[ i ] ≤ Y[ i ] for all i =1,2, …, n X ≤ Y if and only if X[ i ] ≤ Y[ i ] for all i =1,2, …, n For example if X = (1, 7, 3, 2) and Y = (0, 3, 2, 1), then Y ≤ X if X = (1, 7, 3, 2) and Y = (0, 3, 2, 1), then Y ≤ X

35 35Ku-Yaw ChangChapter 8 Deadlocks 8.5.3.1 Safety Algorithm Find out whether or not the system is in a safe state 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work := Available Finish [ i ] := false for i = 1, 2, 3, …, n. 2.Find an i such that both: a)Finish [ i ] = false b)Need i  Work. If no such i exists, go to step 4. 3.Work = Work + Allocation i Finish[ i ] := true go to step 2. 4.If Finish [ i ] = true for all i, then the system is in a safe state.

36 36Ku-Yaw ChangChapter 8 Deadlocks 8.5.3.2 Resource-Request Algorithm Request i [ j ] = k Process P i wants k instances of resource type R j. Process P i wants k instances of resource type R j. 1.If Request i ≤ Need i, go to step 2. Otherwise, raise an error condition, since the process has exceeded its maximum claim. 2.If Request i ≤ Available, go to step 3. Otherwise P i must wait, since the resources are not available. 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available := Available – Request i ; Allocation i := Allocation i + Request i ; Need i := Need i – Request i ; If safe -> the resources are allocated to P i. If safe -> the resources are allocated to P i. If unsafe -> P i must wait, and the old resource-allocation state is restored. If unsafe -> P i must wait, and the old resource-allocation state is restored.

37 37Ku-Yaw ChangChapter 8 Deadlocks 8.5.3.3 An Illustrative Example Five processes P 0 through P 4 P 0 through P 4 Snapshot at time T 0 : Allocation M a x Available Need A B CA B C A B CA B C P 0 0 1 07 5 3 3 3 2 7 4 3 P 1 2 0 0 3 2 2 1 2 2 P 2 3 0 2 9 0 26 0 0 P 3 2 1 1 2 2 2 0 1 1 P 4 0 0 24 3 3 4 3 1 satisfies the safety criteria. satisfies the safety criteria. Three resource types A : 10 instances A : 10 instances B : 5 instances B : 5 instances C : 7 instances C : 7 instances

38 38Ku-Yaw ChangChapter 8 Deadlocks 8.5.3.3 An Illustrative Example P 1 requests One instance of resource type A One instance of resource type A Two instances of resource type C Two instances of resource type C Request 1 = (1, 0, 2) ≤ Available (3, 3, 2) Request 1 = (1, 0, 2) ≤ Available (3, 3, 2) Snapshot at time T 0 : AllocationNeed Available A B CA B C A B C P 0 0 1 07 4 3 2 3 0 P 1 3 0 2 0 2 0 P 2 3 0 2 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 24 3 1 satisfies the safety criteria. satisfies the safety criteria. Grant the request of process P 1. Grant the request of process P 1.

39 39Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

40 40Ku-Yaw ChangChapter 8 Deadlocks 8.6 Deadlock Detection Allow the system to enter deadlock state An algorithm that examines the state of the system to determine whether a deadlock has occurred An algorithm that examines the state of the system to determine whether a deadlock has occurred An algorithm to recover from the deadlock An algorithm to recover from the deadlock A detection-and-recovery scheme requires overhead Run-time costs Run-time costs Potential losses Potential losses

41 41Ku-Yaw ChangChapter 8 Deadlocks 8.6.1 Single Instance of Each Resource Type A wait-for graph : obtained from resource-allocation graph Remove the nodes of type resource Remove the nodes of type resource Collapse the appropriate edges Collapse the appropriate edges

42 42Ku-Yaw ChangChapter 8 Deadlocks 8.6.1 Single Instance of Each Resource Type A deadlock exists in the system if and only if the wait-for graph contains a cycle. To detect deadlocks Maintain the wait-for graph Maintain the wait-for graph Periodically invoke an algorithm that searches for a cycle in the graph Periodically invoke an algorithm that searches for a cycle in the graph

43 43Ku-Yaw ChangChapter 8 Deadlocks 8.6.2 Several Instances of a Resource Type The algorithm employs several time-varying data structures Available Available A vector of length m indicates the number of available resources of each type. Allocation Allocation An n x m matrix defines the number of resources of each type currently allocated to each process. Request Request An n x m matrix indicates the current request of each process. If Request [i, j] = k, then process P i is requesting k more instances of resource type R j.

44 44Ku-Yaw ChangChapter 8 Deadlocks Detection Algorithm 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: (a) Work = Available (b) For i = 1,2, …, n, if Allocation i  0, then Finish[i] = false;otherwise, Finish[i] = true. 2.Find an index i such that both: (a)Finish[i] == false (b)Request i  Work If no such i exists, go to step 4.

45 45Ku-Yaw ChangChapter 8 Deadlocks Detection Algorithm (Cont.) 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked. Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.

46 46Ku-Yaw ChangChapter 8 Deadlocks Example of Detection Algorithm Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances). Snapshot at time T 0 : AllocationRequestAvailable A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 30 0 0 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2 Sequence will result in Finish[i] = true for all i.

47 47Ku-Yaw ChangChapter 8 Deadlocks Example (Cont.) P 2 requests an additional instance of type C. Request A B C P 0 0 0 0 P 0 0 0 0 P 1 2 0 1 P 1 2 0 1 P 2 0 0 1 P 2 0 0 1 P 3 1 0 0 P 3 1 0 0 P 4 0 0 2 P 4 0 0 2 State of system? Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4. Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.

48 48Ku-Yaw ChangChapter 8 Deadlocks 8.6.3 Detection-Algorithm Usage When, and how often, to invoke depends on: How often a deadlock is likely to occur? How often a deadlock is likely to occur? How many processes will need to be rolled back? How many processes will need to be rolled back? one for each disjoint cycle The detection algorithm can be invoked When a request cannot be granted immediately When a request cannot be granted immediately Identify the specific process that caused the deadlock At arbitrary points in time At arbitrary points in time Not be able to tell which of the many deadlocked processes “caused” the deadlock

49 49Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

50 50Ku-Yaw ChangChapter 8 Deadlocks 8.7.1 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. Priority of the process. How long process has computed, and how much longer to completion. How long process has computed, and how much longer to completion. Resources the process has used. Resources the process has used. Resources process needs to complete. Resources process needs to complete. How many processes will need to be terminated. How many processes will need to be terminated. Is process interactive or batch? Is process interactive or batch?

51 51Ku-Yaw ChangChapter 8 Deadlocks 8.7.2 Resource Preemption Three issues Selecting a victim Selecting a victim To minimize cost Number of resources Number of resources Amount of time Amount of time Rollback Rollback Return to some safe state, restart process for that state The simplest solution is a total rollback Starvation Starvation Same process may always be picked as victim, include number of rollback in cost factor Include the number of rollbacks in the cost factor Include the number of rollbacks in the cost factor

52 52Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

53 53Ku-Yaw ChangChapter 8 Deadlocks Summary P.266

54 54Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

55 55Ku-Yaw ChangChapter 8 Deadlocks Exercises 8.28.48.58.128.13

56 The End


Download ppt "Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."

Similar presentations


Ads by Google