Presentation is loading. Please wait.

Presentation is loading. Please wait.

DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several.

Similar presentations


Presentation on theme: "DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several."— Presentation transcript:

1 DeadlocksDeadlocks CNS 3060

2 Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several people to stand.

3 Wait

4 Deadlock

5 Preemptable and Non-preemptable Resources A preemptable resource is one that can be temporarily taken way from the process owning it without any ill effects. A non-preemptable resource is one that cannot be taken away without causing the computation to fail.

6 Using Semaphores to Acquire and Use a Resource wait(&semaphore_1); use_resource_1; signal(&semaphore_1)

7 Using Semaphores to Acquire and Use Two Resources wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

8 Two Processes Using Semaphores to Acquire and Use Two Resources wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) Process A Process B

9 wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) Process A Process B 1 1 0 Time slice expires… semaphore 1 semaphore 2 Two Processes Using Semaphores to Acquire and Use Two Resources

10 wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) Process A Process B 1 0 0 D E A D L O C K ! semaphore 1 semaphore 2 Two Processes Using Semaphores to Acquire and Use Two Resources

11 wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) Process A Process B Can you fix it so no deadlock occurs? Two Processes Using Semaphores to Acquire and Use Two Resources

12 wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) Process A Process B Two Processes Using Semaphores to Acquire and Use Two Resources

13 Some Examples of Deadlock

14 Deadlock on a file request A System built for contractors has two files (among others) - one for suppliers - one for inventory It also has two applications (among others) - a purchasing application that orders material - a sales application that creates a supply list

15 Some Examples of Deadlock Deadlock on a file request Inventory Suppliers Purchasing Sales Purchasing accesses the supplier file to place an order for lumber. Sales accesses the inventory file to reserve parts to build a home ordered today. Purchasing now tries to access the Inventory file to verify the quantity of lumber on hand before placing the order Sales tries to access the supplier file to check the schedule of a subcontractor.

16 Some Examples of Deadlock Deadlock on a print spooler A print spooler accepts input from many users, but will not start a print job until it has a complete print file. spool print job The day that a big project is due everyone in class submits their print files at the same time. The spooler accepts pages from everyone so several page ones show up, then several page twos, and so on. Quickly the spool file fills. It cannot print anything because is has no complete jobs.

17 Four Conditions for Deadlock 1.Mutual exclusion condition : Each resource is either being used by exactly one process or it is available. 2. Hold and wait condition : Processes currently holding resources granted earlier can request additional new resources. 3.No preemption condition : Resources previously granted cannot be taken away from the process. The process must release them voluntarily. Once a process requests a resource that is not available, it cannot withdraw its request. 4.Circular wait condition: There must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain.

18 Deadlock Modeling process resource The resource has been assigned to the process. resource process The process is blocked, waiting for the resource. This is called a Resource Graph

19 Deadlock Modeling process resource process r1 r2 p1 p2 process p1 owns resource r1 process p1 is blocked waiting for resource r2 process p2 is blocked waiting for resource r1 process p2 owns resource 2 Circular Wait Condition!

20 This Process-Resource model provides a “snapshot” of the relationships between processes and resources at some instant in time. We need a model that addresses the dynamics of the relationships … i.e. what happens over time.

21 S is the set of states {s i } where each state s is represented with a resource graph representing the processes and resources in the machine. The initial state, s 0, represents the situation Where none of the resources have been allocated. In this model we are interested in state transitions. The pattern in which resources are requested, allocated, and released will determine whether or not the system is deadlocked.

22 Process A Request resource R Request resource S Do some work Release resource R Release resource S Process B Request resource S Request resource T Do some work Release resource S Release resource T Process C Request resource T Request resource R Do some work Release resource T Release resource R Assume a Round Robin Scheduling Algorithm With events occurring in the following order: Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R

23 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST

24 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST

25 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST

26 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST

27 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST

28 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST D E A D L O C K !

29 We know that the operating system is not bound to run processes in any particular order. In this case, if the operating system knew of the impending deadlock, it could have scheduled the work differently!

30 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST

31 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST potential deadlock – block this call!

32 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST potential deadlock – block this call!

33 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST potential deadlock – block this call!

34 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A B C RST potential deadlock – block this call! Blocked. blocks

35 Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R Process A does its work Process A releases resource R Process A releases resource S A B C RST potential deadlock – block this call! Blocked. blocks There is no deadlock --- Process C does its work and releases its resources Process B is now unblocked

36 Modeling Deadlock with State Diagrams

37 In a set of processes P, any process p i in P would cause a transition from state s j, if * p i requests a resource (shown as r i ) * p i is allocated a resource (shown as a i ) * p i deallocates a resource (shown as d i ) s j a i r i d i

38 s j a 3 r 4 d 1 Suppose that at state s j, some process p 2 is blocked because all of the transitions out of s j are caused by other processes. That is, process p 2 can not do anything to force a change in state.

39 If a process p i is blocked in state s j, And there is no series of state transitions which will lead to a state in which p i is unblocked, then p i is deadlocked. The state s j is called a deadlock state. s j a 3 r 4 d 3

40 A State Transition Model for One Process and Two Resources a r s 0 s 1 s 2 s 3 s 4 r a d d r – request a – allocate d - deallocate process owns 1 resource process owns 2 resources

41 A State Transition Model for Two Processes and Two Resources r0r0 s 00 s 10 s 20 s 30 s 40 r 0 – process 0 request a 0 – process 0 allocate d 0 – process 0 deallocate s 01 s 11 s 21 s 31 s 41 a0a0 r0r0 a0a0 d0d0 d0d0 r0r0 r0r0 a0a0 a0a0 d0d0 d0d0 r1r1 r1r1 r1r1 r1r1 r1r1 s 02 s 12 s 22 s 32 r0r0 r0r0 a0a0 d0d0 a1a1 a1a1 a1a1 a1a1 d1d1 d1d1 d1d1 d1d1 s 03 s 13 s 23 s 33 r0r0 r0r0 a0a0 d0d0 r1r1 r1r1 r1r1 r1r1 s 03 s 13 r0r0 a1a1 a1a1 d1d1 d1d1

42 A State Transition Model for Two Processes and Two Resources r0r0 s 00 s 10 s 20 s 30 s 40 r 0 – process 0 request a 0 – process 0 allocate d 0 – process 0 deallocate s 01 s 11 s 21 s 31 s 41 a0a0 r0r0 a0a0 d0d0 d0d0 r0r0 r0r0 a0a0 a0a0 d0d0 d0d0 r1r1 r1r1 r1r1 r1r1 r1r1 s 02 s 12 s 22 s 32 r0r0 r0r0 a0a0 d0d0 a1a1 a1a1 a1a1 a1a1 d1d1 d1d1 d1d1 d1d1 s 03 s 13 s 23 s 33 r0r0 r0r0 a0a0 d0d0 r1r1 r1r1 r1r1 r1r1 s 04 s 14 r0r0 a1a1 a1a1 d1d1 d1d1 no resources held one resource held two resources held deadlock state

43 Deadlock Strategies The Ostrich Algorithm: Ignore the problem. If you ignore it, it may ignore you. Detection and Recovery: Let deadlocks occur, detect them and take action. Dynamic avoidance: OS is careful how it allocates resources Deadlock Prevention: Structurally negate one of the conditions necessary to cause deadlock.

44 The Ostrich Algorithm Good or Bad? Many potential deadlock situations occur because almost all resources on a system are finite. For example, The number of open files that a system can manage is limited by the size of the i-node table. Suppose that 100 processes all require 100 open files before they can do their work. After each has opened 99, the i-node table becomes full. Now, none of the processes can open another file and a deadlock occurs. How often will this situation occur? Does it occur often enough that it is worth fixing? Fixing such problems is expensive and usually involves placing inconvenient restrictions in what a process can do.

45 ReminderReminder process resource The resource has been assigned to the process. resource process The process is blocked, waiting for the resource.

46 Deadlock Detection Is there a deadlock in the drawing below? A F C R S W D B E G T U W Cycles are pretty easy to find with visual inspection. Can they be found algorithmically?

47 Resource graphs are an example of a directed graph. many algorithms exist to detect cycles in a directed graph. Repeat the following steps for each node in the graph 1.Initialize a list to the empty list. Designate all arcs as unvisited 2.Add the current node to the list. Check to see if the node appears in the list more than once. If it does a cycle exists. 3.From the given node, see if there are any outgoing arcs. If so, go to step 4. Otherwise go to step 5. 4.Pick one of the outgoing nodes at random and mark it. Follow it to the new current node and go to step 2. 5.Dead end. Remove the node from the list and go back to the previous one and make it the current node. Go to step 3. If this node is the initial node, there are no cycles.

48 Deadlock Detection Is there a deadlock in the drawing below? A F C R S W D B E G T U W Start here and go left to right, top to bottom

49 A F C R S W D B E G T U W List L R

50 A F C R S W D B E G T U W R This is the only outging arc, so A is the next node A x

51 A F C R S W D B E G T U W List L R This is the only outging arc, so S is the next node A S x x

52 A F C R S W D B E G T U W List L R S has no outgoing Arcs, so back-up To A A x x

53 A F C R S W D B E G T U W List L R A has no unmarked outgoing arcs, so back-up To R x x

54 A F C R S W D B E G T U W List L R has no unmarked outgoing arcs, so we are done inspecting this node. x x

55 A F C R S W D B E G T U W List L Move to the next node and repeat the algorithm. It should be pretty obvious that this iteration stops quickly, finding no cycle.

56 A F C R S W D B E G T U W List L Move to the next node and repeat the algorithm.

57 A F C R S W D B E G T U W List L B x This is the only outgoing arc T

58 A F C R S W D B E G T U W List L B x This is the only outgoing arc T x

59 A F C R S W D B E G T U W List L Until finally at this point we have … x x x x x x B T E W G U D

60 A F C R S W D B E G T U W List L If we go this way we hit a dead end … there are no outgoing arcs x x x x x x B T E W G U D

61 A F C R S W D B E G T U W List L So we back up and look at the only unmarked arc from D. x x x x x x B T E W G U D x

62 A F C R S W D B E G T U W List L At this point, T gets added to the list. When the list is inspected, we note that it is in the list twice. Therefore a cycle exists. x x x x x x B T E W G U D T x

63 Deadlock Modeling process resource The resource has been assigned to the process. resource process The process is blocked, waiting for the resource. One way to model “units” of a resource is like this … This resource has 2 units that can be assigned.

64 Graph Reduction Given a directed resource graph 1.Find a process that is currently using a resource and not waiting for one. This process can be removed from the graph. This is because the process would eventually finish and its resources returns to the pool of available resources. 2. Find a process that’s only waiting for resource classes that aren’t fully allocated. This process isn’t contributing to deadlock since it will eventually get its resources and finish. Then its resources can be returned to the available pool. 3. Go back to step 1 until all possible connections have been removed. If there are any connections between processes and resources left When you are done, then the system will deadlock.

65 Graph Reduction Example One

66 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 P 3 is currently usingR 3 and is not waiting for any additional resources. P 1 is using R 1 and is waiting for R 2 P 2 is using R 2 and is waiting for R 3

67 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 Eventually P 3 will finish using R 3, So we can unlink the resource. It can Now be allocated to P 2

68 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 P 2 now has all of the resources it Needs, so it can run to completion. Unlink it.

69 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 R 2 can now be given to P 1 P 1 has all of the resources it needs so eventually it will complete and we can unlink it.

70 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 Since there are no connections left, there is no deadlock.

71 Graph Reduction Example Two

72 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 P 3 is currently usingR 3 and is not waiting for any additional resources. P 2 is using R 2 and is waiting for R 3 and R 1 P 1 is using R 1 and is waiting for R 2

73 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 Unlink it so that R 3 can be given to P 2 We can’t reduce the graph any further! P 2 can’t complete it’s waiting for R 1 P 1 can’t complete it’s waiting for R 2

74 Deadlock Detection with Multiple Resources of the same Type Consider an example where there are multiple processes running on a system that has 4 tape drives 2 plotters 3 scanners 1 CD Rom drive

75 Step 1: Define a vector, E, of resources that exist on the system E = (4 2 3 1) tape drives plotters scanners CDRom Drives Resources in existence Graph Reduction – an approach that can be programmed

76 E = (4 2 3 1) Step Two: Define a vector A, that contains the number of available resources of each type that are available at any instance in time. A = (A 1 A 2 A 3 A 4 ) Available resources Resources in existence

77 E = (4 2 3 1) Step Three: Define a matrix C, the current allocation matrix. Each row in the matrix represents one process in the system. The columns represent the resources of each class. Each element defines the number of that type assigned to the process. A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 This row defines the resources allocated to process 2 tape plot scan CD If C 23 were 2, it would indicate that process 2 held 2 scanners. process 1 process 2 process n

78 E = (4 2 3 1) Step Four: Define a matrix R, the request matrix. Each row in the matrix represents one process in the system. The columns represent the resources of each class. Each element then defines the number of resources of that class requested by a process. A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD This row represents the resources requested by process 2. If this element were 1, it would indicate that process 2 needs 1 plotter process 1 process 2 process n

79 E = (4 2 3 1) A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 tape plot scan CD Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD Note that The following is always true … n i=1 C ij + A j = E j tape plot scan CD tape plot scan CD

80 E = (4 2 3 1) A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 tape plot scan CD Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD The deadlock detection algorithm is based on comparing vectors. For vector A to be less than or equal to vector B, every element of A must be less than or equal to the corresponding element of B. tape plot scan CD tape plot scan CD

81 E = (4 2 3 1) A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 tape plot scan CD Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD Each process is initially unmarked. As the algorithm progresses, if a process can complete, it is marked. Any unmarked processes after executing the algorithm are deadlocked.

82 E = (4 2 3 1) A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 tape plot scan CD Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD 1.Look for an unmarked process, P i, for which the i-th row of the request matrix R is less than or equal to A. Such a process can be run to completion, because it’s resource demands can be met. Is each element of R less than the corresponding element of A?

83 E = (4 2 3 1) A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 tape plot scan CD Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD 2. If such a process is found, we assume that the process can be run to completion and it’s resources returned to the system. To account for this, add the i-th row of C to A and mark the process. Go back to step 1. ADD x

84 E = (4 2 3 1) A = (A 1 A 2 A 3 A 4 ) Resources in existence Available resources Current allocation matrix C 11 C 12 C 13 C 14 C 21 C 22 C 23 C 23 … C n1 C n2 C n3 C n4 tape plot scan CD Request matrix R 11 R 12 R 13 R 14 R 21 R 22 R 23 R 23 … R n1 R n2 R n3 R n4 tape plot scan CD 3. If no unmarked process can be found that is runnable, terminate the algorithm. If there are any unmarked processes left, these processes are deadlocked.

85 E = (4 2 3 1) A = (2 1 0 0) Resources in existence Available resources Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 tape plotscanCD Request matrix 2 0 0 1 1 0 2 1 0 0 tape plot scan CD To illustrate, let there be three processes in the system. * Process 1 has one scanner * Process 2 has 2 tape drives and a CD Rom drive * Process 3 has a plotter and 2 scanners Each process needs additional resources as shown in R. tape plotscan CD tape plotscan CD process 1 process 2 process 3

86 E = (4 2 3 1) A = (2 1 0 0) Resources in existence Available resources Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 tape plotscanCD Request matrix 2 0 0 1 1 0 2 1 0 0 tape plot scan CD To run the algorithm, look for a process whose request list can be satisfied.

87 E = (4 2 3 1) A = (2 1 0 0) Resources in existence Available resources Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 tape plotscanCD Request matrix 2 0 0 1 1 0 2 1 0 0 tape plot scan CD So … process 3 can be run and eventually return it’s resources. Then vector A would be equal to x 0 1 2 0 A = (2 2 2 0)

88 E = (4 2 3 1) A = (2 2 2 0) Resources in existence Available resources Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 tape plotscanCD Request matrix 2 0 0 1 1 0 2 1 0 0 tape plot scan CD Now process 2’s requests can be satisfied. Its resources are added back into the Available Resources vector. x

89 E = (4 2 3 1) A = (4 2 2 1) Resources in existence Available resources Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 tape plotscanCD Request matrix 2 0 0 1 1 0 2 1 0 0 tape plot scan CD Finally, Process 1 (the only one left) can get all of the resources That it needs. It runs to completion. There is no deadlock. x x

90 How often should you run a deadlock detection algorithm? Obviously, detecting deadlocks takes some system resource.

91 Recovery from Deadlock There are no good alternatives to do deadlock recovery! * Preemption: Take a resource away from a process. * Rollback: the state of a process must be checkpointed periodically. It is then possible to roll back to some state before it acquired the resource causing the deadlock. * Kill Processes: try to pick a process that can be run again from the beginning without any ill effects.

92 Deadlock Prevention Note that Deadlock prevention is different from detection.

93 1.Mutual exclusion condition : Each resource is either being used by exactly one process or it is available. 2. Hold and wait condition : Processes currently holding resources granted earlier can request additional new resources. 3.No preemption condition : Resources previously granted cannot be taken away from the process. The process must release them voluntarily. Once a process requests a resource that is not available, it cannot withdraw its request. 4.Circular wait condition: There must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain. For deadlock to occur, all of the following conditions must hold. Deadlock prevention schemes all try to make sure that at least one of these conditions is not true.

94 Attacking the Mutual Exclusion Condition If no resource were ever assigned exclusively to a single process, then there would be no deadlock problems. Is this reasonable? How might you avoid mutual exclusion on a printer? It is difficult to avoid mutual exclusion. However, you can avoid assigning a resource unless and until it is absolutely necessary. Try to make sure that as few processes as possible can lay claim to the resource.

95 Attacking the Hold and Wait Condition 1.Require a process to request all of its resources when it is first created. This would be impossible for an interactive system. However, a batch system could do this … only add a job to the ready list when all of the resources it needs are available. But, is there a problem? Poor utilization of resources ! 2. Require a process to give up all resources it has allocated to it, before it can request any new ones. This is okay for interactive systems, but it forces a process to compete for all resources, even when it needs an incremental resource.

96 Attacking the No Preemption Condition This is not very promising … it is difficult to take a resource away from a process until the process has finished with it.

97 Attacking the Circular Wait Condition There are two possible approaches: Only allow a process to have one resource at a time. Provide a global numbering system for resources. A process may have as many resources as it wants, but requests must be in numerical order. For example, a process may request a printer and then a plotter, but never a plotter and then a printer!

98 Deadlock Avoidance Deadlock avoidance schemes control state transitions. They will only allow transitions due to allocations, when they can be certain that a deadlock cannot occur due to subsequent requests.

99 Deadlock Avoidance Process A Process B Plot how each process gets the cpu

100 Process A Process B Process A requests printer

101 Process A Process B Process A requests printer Process B requests plotter

102 Process A Process B Process A requests plotter Process B requests plotter Process A requests printer Process B requests printer

103 Process A Process B Process A requests plotter Process B requests plotter Process A releases printer Process A releases plotter Process A requests printer Process B requests printer Process B releases plotter Process B releases printer

104 Process A Process B plt prt plt prt plt prt The system cannot enter any of these states, because both processes would have the printer

105 Process A Process B plt prt plt prt plt prt The system cannot enter any of these states, because both processes would have the plotter

106 Process A Process B plt prt plt prt plt prt Once you enter this section (no matter the path) you will be in a deadlock situation!

107 Process A Process B plt prt plt prt plt prt Unsafe area! Deadlock can be avoided by scheduling the processes as shown:

108 Deadlock avoidance requires that every process declare its maximum resource claim before it can begin running

109 E = (4 2 3 1) A = (2 1 0 0) Resources in existence Available resources Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 tape plotscanCD Request matrix 2 0 0 1 1 0 2 1 0 0 tape plot scan CD Consider the vectors and matrices we used earlier to look at Deadlock. We can use these to define the Resource Allocation State of a process.

110 Safe State A state is safe if the system is not deadlocked, and can allocate resources to each process (up to its maximum) in some given order and still avoid deadlock.

111 The Banker’s Algorithm The Banker’s algorithm was developed by Dijkstra in 1965 and provides a scheduling algorithm that can avoid deadlocks. It is modeled on the way a small town banker might deal with a group of customers to whom he has granted lines of credit. If granting a request leads to a safe state, the request is granted. If it does not, the request is denied. When a request is denied the customer may come back later to make the request again.

112 Consider the following case. The bank has four customers, each of whom has been granted a certain line of credit. Any customer may ask for a loan up to their maximum line of credit. The bank assumes that all of the customers will not ask for their maximum line of credit at the same time, so it does not need to have the sum of these credit lines on hand. If a customer cannot eventually get the money they need, they go out of business and their loans are never repaid. A06B05C04D07A06B05C04D07 Customer has max (line of credit) Bank has 10

113 Assume that the bank customers go about their business, and periodically get loans from the bank of some amount up to their maximum line of credit. At some point we might have a situation like that pictured below: A16B15C24D47A16B15C24D47 Customer has max Bank has 2

114 Is this a safe state or an unsafe state? A16B15C24D47A16B15C24D47 Customer has max Bank has 2

115 A16B15C24D47A16B15C24D47 Customer has max Bank has 2 The bank can loan C up to it’s full line of credit. Any other requests are denied. At some time C Pays off its loans and the bank has 4. It could then satisfy requests by B or D up to their maximum amounts.

116 However, what happens if B is granted one additional unit of credit? A16B25C24D47A16B25C24D47 Customer has max Bank has 1 Unsafe Why?

117 However, what happens if B is granted one additional unit of credit? A16B25C24D47A16B25C24D47 Customer has max Bank has 1 Unsafe If any customer walked in and demanded their full line of credit, the bank could not grant it.

118 Note that being in an unsafe state does not necessarily mean that Deadlock will occur. None of the customers may ever ask for their maximum Line of credit.

119 E = (6 3 4 2) A = (1 0 2 0) Resources in existence Available resources Current allocation matrix 3 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1 0 0 tapeplotscanCD Max request matrix 1 1 0 0 0 1 1 2 3 1 0 0 0 0 1 0 2 1 1 0 tapeplotscanCD Banker’s Algorithm for Multiple Resources … Begin at some state s j P = (5 3 2 2) Possessed Resources

120 E = (6 3 4 2) A = (1 0 2 0) Resources in existence Available resources Current allocation matrix 3 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1 0 0 tapeplotscanCD Max request matrix 1 1 0 0 0 1 1 2 3 1 0 0 0 0 1 0 2 1 1 0 tapeplotscanCD 1.Find a row in the maximum request matirx whose unmet resources are less than A. If you can’t find one, the system is deadlocked ! P = (5 3 2 2) Possessed Resources Here’s one. 2. Now assume that the process is given the resources and finishes. Mark the process as terminated and add its resources back into A

121 E = (6 3 4 2) A = (1 0 2 0) Resources in existence Available resources Current allocation matrix 3 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1 0 0 tapeplotscanCD Max request matrix 1 1 0 0 0 1 1 2 3 1 0 0 0 0 1 0 2 1 1 0 tapeplotscanCD 1.Find a row in the maximum request matirx whose unmet resources are less than A. If you can’t find one, the system is deadlocked ! P = (5 3 2 2) Possessed Resources Here’s one. 2. Now assume that the process is given the resources and finishes. Mark the process as terminated and add its resources back into A 1 1 0 1

122 E = (6 3 4 2) A = (2 1 2 1) Resources in existence Available resources Current allocation matrix 3 0 1 1 0 1 0 0 1 1 1 0 0 0 tapeplotscanCD Max request matrix 1 1 0 0 0 1 1 2 3 1 0 0 0 0 1 0 2 1 1 0 tapeplotscanCD Repeat steps 1 and 2 until all processes are marked terminated (the state is safe), or until a deadlock occurs (it is not safe). P = (5 3 2 2) Possessed Resources

123 Dijkstra’s algorithm is elegant in concept, but is almost useless in practice. Why? 1.It requires a process to know in advance what its maximum resource needs will be. 2.It assumes that the number of available resources is constant so does not account for failures. 3.It assumes that the number of processes is constant so does not account for users logging in and out of a system.

124 Sample Exam Questions

125 Suppose a system contains four processes (1, 2, 3, 4) and four single unit resources (A, B, C, D). If the following sequence of events occurs, which request (if any) is the first that will cause a deadlock to occur? Process 1 2 1 4 3 2 4 3 Resource A B B C D C A A

126 Process 1 2 1 4 3 2 4 3 Resource A B B C D C A A 1 A 2 B 4 C 3 D Deadlock

127 Assume that a system has the following resource units The current allocation matrix The maximum claim matrix 2 0 1 0 3 2 1 1 1 1 0 0 1 2 0 2 1 1 0 0 1 1 2 0 1 0 1 0 3 2 1 0 0 1 0 1 2 1 0 0 Is this state safe?

128 The current allocation matrix The maximum claim matrix 2 0 1 0 3 2 1 1 1 1 0 0 1 2 0 2 1 1 0 0 1 1 2 0 1 0 1 0 3 2 1 0 0 1 0 1 2 1 0 0 Exist The available resources must be A This request could be satisfied

129 A system is composed of four processes {p 1, p 2, p 3, p 4 } and three types of serially re-usable resources {R 1, R 2, R 3 } The number of units of the resources is. Show the resource graph that represents the system state, and reduce the graph as far as possible under the following conditions: Process p 1 holds one unit of R 1 and requests one unit of R 2 p 2 holds two units of R 2 and requests one unit of R 1 and R 3 p 3 holds one unit of R 1 and requests one unit of R 2 p 4 holds two units of R 3 and requests one unit of R 1

130 Students working at individual PCs in a computer lab all send their files to a server that spools the files on its hard disk. Under what conditions may a deadlock occur when the disk space for the print spool is limited? How can this deadlock be avoided?


Download ppt "DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several."

Similar presentations


Ads by Google