Download presentation
Presentation is loading. Please wait.
Published byJob McCoy Modified over 8 years ago
1
Operating Systems Chapter 7 Deadlock Hung Q. Ngo KyungHee University Spring 2009 http://uclab.khu.ac.kr/lectures/2009-1-os.html
2
Chapter 7.2 Operating SystemsHung Q. Ngo, KHU Spring’09
3
Chapter 7.3 Operating SystemsHung Q. Ngo, KHU Spring’09 Goals for Today Discussion of Deadlocks –Conditions for its occurrence –Solutions for breaking and avoiding deadlock Preventing Deadlock Note: Some slides and/or pictures in the following are adapted from slides ©2005 Silberschatz, Galvin, and Gagne Note: Some slides and/or pictures in the following are adapted from slides ©2005 Silberschatz, Galvin, and Gagne. Many slides generated from my lecture notes by Kubiatowicz.
4
Chapter 7.4 Operating SystemsHung Q. Ngo, KHU Spring’09 Resources – passive entities needed by threads to do their work –CPU time, disk space, memory Two types of resources: –Preemptable – can take it away »CPU, Embedded security chip –Non-preemptable – must leave it with the thread »Disk space, chunk of virtual address space »Mutual exclusion – the right to enter a critical section Resources may require exclusive access or may be sharable –Read-only files are typically sharable –Printers are not sharable during time of printing One of the major tasks of an operating system is to manage resources Resources
5
Chapter 7.5 Operating SystemsHung Q. Ngo, KHU Spring’09 Starvation vs Deadlock Starvation vs. Deadlock –Starvation: thread waits indefinitely »Example, low-priority thread waiting for resources constantly in use by high-priority threads –Deadlock: circular waiting for resources »Thread A owns Res 1 and is waiting for Res 2 Thread B owns Res 2 and is waiting for Res 1 –Deadlock Starvation but not vice versa »Starvation can end (but doesn’t have to) »Deadlock can’t end without external intervention Res 2 Res 1 Thread B Thread A Wait For Wait For Owned By Owned By
6
Chapter 7.6 Operating SystemsHung Q. Ngo, KHU Spring’09 Conditions for Deadlock Deadlock not always deterministic – Example 2 mutexes: Thread AThread B x.P();y.P(); y.P();x.P(); y.V();x.V(); x.V();y.V(); –Deadlock won’t always happen with this code »Have to have exactly the right timing (“wrong” timing?) »So you release a piece of software, and you tested it, and there it is, controlling a nuclear power plant… Deadlocks occur with multiple resources –Means you can’t decompose the problem –Can’t solve deadlock for each resource independently Example: System with 2 disk drives and two threads –Each thread needs 2 disk drives to function –Each thread gets one disk and waits for another one
7
Chapter 7.7 Operating SystemsHung Q. Ngo, KHU Spring’09 Bridge Crossing Example Each segment of road can be viewed as a resource –Car must own the segment under them –Must acquire segment that they are moving into For bridge: must acquire both halves –Traffic only in one direction at a time –Problem occurs when two cars in opposite directions on bridge: each acquires one segment and needs next 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 Starvation is possible –East-going traffic really fast no one goes west
8
Chapter 7.8 Operating SystemsHung Q. Ngo, KHU Spring’09 Train Example (Wormhole-Routed Network) Circular dependency (Deadlock!) –Each train wants to turn right –Blocked by other trains –Similar problem to multiprocessor networks Fix? Imagine grid extends in all four directions –Force ordering of channels (tracks) »Protocol: Always go east-west first, then north-south –Called “dimension ordering” (X then Y) Disallowed By Rule
9
Chapter 7.9 Operating SystemsHung Q. Ngo, KHU Spring’09 Dining Lawyers Problem Five chopsticks/Five lawyers (really cheap restaurant) –Free-for all: Lawyer will grab any one they can –Need two chopsticks to eat What if all grab at same time? –Deadlock! How to fix deadlock? –Make one of them give up a chopstick (Hah!) –Eventually everyone will get chance to eat How to prevent deadlock? –Never let lawyer take last chopstick if no hungry lawyer has two chopsticks afterwards
10
Chapter 7.10 Operating SystemsHung Q. Ngo, KHU Spring’09 Four requirements for Deadlock Mutual exclusion –Only one thread at a time can use a resource. Hold and wait –Thread holding at least one resource is waiting to acquire additional resources held by other threads No preemption –Resources are released only voluntarily by the thread holding the resource, after thread is finished with it Circular wait –There exists a set {T 1, …, T n } of waiting threads »T 1 is waiting for a resource that is held by T 2 »T 2 is waiting for a resource that is held by T 3 »…»… »T n is waiting for a resource that is held by T 1
11
Chapter 7.11 Operating SystemsHung Q. Ngo, KHU Spring’09 Symbols Resource-Allocation Graph System Model –A set of Threads T 1, T 2,..., T n –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 thread utilizes a resource as follows: »Request() / Use() / Release() Resource-Allocation Graph: –V is partitioned into two types: »T = {T 1, T 2, …, T n }, the set threads in the system. »R = {R 1, R 2, …, R m }, the set of resource types in system –request edge – directed edge T 1 R j –assignment edge – directed edge R j T i R1R1 R2R2 T1T1 T2T2
12
Chapter 7.12 Operating SystemsHung Q. Ngo, KHU Spring’09 Resource Allocation Graph Examples T1T1 T2T2 T3T3 R1R1 R2R2 R3R3 R4R4 Simple Resource Allocation Graph T1T1 T2T2 T3T3 R1R1 R2R2 R3R3 R4R4 Allocation Graph With Deadlock T1T1 T2T2 T3T3 R2R2 R1R1 T4T4 Allocation Graph With Cycle, but No Deadlock Recall: –request edge – directed edge T 1 R j –assignment edge – directed edge R j T i
13
Chapter 7.13 Operating SystemsHung Q. Ngo, KHU Spring’09 Methods for Handling Deadlocks Allow system to enter deadlock and then recover –Requires deadlock detection algorithm –Some technique for forcibly preempting resources and/or terminating tasks Ensure that system will never enter a deadlock –Need to monitor all lock acquisitions –Selectively deny those that might lead to deadlock Ignore the problem and pretend that deadlocks never occur in the system –Used by most operating systems, including UNIX
14
Chapter 7.14 Operating SystemsHung Q. Ngo, KHU Spring’09 T1T1 T2T2 T3T3 R2R2 R1R1 T4T4 Deadlock Detection Algorithm Only one of each type of resource look for loops More General Deadlock Detection Algorithm –Let [X] represent an m-ary vector of non-negative integers (quantities of resources of each type): [FreeResources]: Current free resources each type [Request X ]: Current requests from thread X [Alloc X ]: Current resources held by thread X –See if tasks can eventually terminate on their own [Avail] = [FreeResources] Add all nodes to UNFINISHED do { done = true Foreach node in UNFINISHED { if ([Request node ] <= [Avail]) { remove node from UNFINISHED [Avail] = [Avail] + [Alloc node ] done = false } } } until(done) –Nodes left in UNFINISHED deadlocked
15
Chapter 7.15 Operating SystemsHung Q. Ngo, KHU Spring’09 What to do when detect deadlock? Terminate thread, force it to give up resources –In Bridge example, Godzilla picks up a car, hurls it into the river. Deadlock solved! –Shoot a dining lawyer –But, not always possible – killing a thread holding a mutex leaves world inconsistent Preempt resources without killing off thread –Take away resources from thread temporarily –Doesn’t always fit with semantics of computation Roll back actions of deadlocked threads –Hit the rewind button on TiVo, pretend last few minutes never happened –For bridge example, make one car roll backwards (may require others behind him) –Common technique in databases (transactions) –Of course, if you restart in exactly the same way, may reenter deadlock once again Many operating systems use other options
16
Chapter 7.16 Operating SystemsHung Q. Ngo, KHU Spring’09 Techniques for Preventing Deadlock Infinite resources –Include enough resources so that no one ever runs out of resources. Doesn’t have to be infinite, just large –Give illusion of infinite resources (e.g. virtual memory) –Examples: »Han river bridge with 12,000 lanes. Never wait! »Infinite disk space (not realistic yet?) No Sharing of resources (totally independent threads) –Not very realistic Don’t allow waiting –How the phone company avoids deadlock »Call to your Mom in Busan, works its way through the phone lines, but if blocked get busy signal. –Technique used in Ethernet/some multiprocessor nets »Everyone speaks at once. On collision, back off and retry –Inefficient, since have to keep retrying »Consider: driving to Busan; when hit traffic jam, suddenly you’re transported back home and told to retry!
17
Chapter 7.17 Operating SystemsHung Q. Ngo, KHU Spring’09 Techniques for Preventing Deadlock (con’t) Make all threads request everything they’ll need at the beginning. –Problem: Predicting future is hard, tend to over- estimate resources –Example: »If need 2 chopsticks, request both at same time »Don’t leave home until we know no one is using any intersection between here and where you want to go; only one car on the Han river bridge at a time Force all threads to request resources in a particular order preventing any cyclic use of resources –Thus, preventing deadlock –Example (x.P, y.P, z.P,…) »Make tasks request disk, then memory, then…
18
Chapter 7.18 Operating SystemsHung Q. Ngo, KHU Spring’09 Review: Train Example (Wormhole-Routed Network) Circular dependency (Deadlock!) –Each train wants to turn right –Blocked by other trains –Similar problem to multiprocessor networks Fix? Imagine grid extends in all four directions –Force ordering of channels (tracks) »Protocol: Always go east-west first, then north-south –Called “dimension ordering” (X then Y) Disallowed By Rule
19
Chapter 7.19 Operating SystemsHung Q. Ngo, KHU Spring’09 Deadlock Avoidance 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. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Requires that the system has some additional a priori information available.
20
Chapter 7.20 Operating SystemsHung Q. Ngo, KHU Spring’09 Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. 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 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. –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.
21
Chapter 7.21 Operating SystemsHung Q. Ngo, KHU Spring’09 Basic Facts 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.
22
Chapter 7.22 Operating SystemsHung Q. Ngo, KHU Spring’09 Banker’s Algorithm Multiple instances. Each process must a priori claim maximum use. 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 time.
23
Chapter 7.23 Operating SystemsHung Q. Ngo, KHU Spring’09 Data Structures for the Banker’s Algorithm 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]. Let n = number of processes, and m = number of resources types.
24
Chapter 7.24 Operating SystemsHung Q. Ngo, KHU Spring’09 Safety Algorithm 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i - 1,3, …, n. 2.Find and 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.
25
Chapter 7.25 Operating SystemsHung Q. Ngo, KHU Spring’09 Resource-Request Algorithm for Process P i Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j. 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 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 ; l If safe the resources are allocated to Pi. l If unsafe Pi must wait, and the old resource- allocation state is restored
26
Chapter 7.26 Operating SystemsHung Q. Ngo, KHU Spring’09 Example of Banker’s Algorithm 5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5instances, and C (7 instances). Snapshot at time T 0 : AllocationMaxAvailable A B CA B C A B C P 0 0 1 07 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 P 4 0 0 24 3 3
27
Chapter 7.27 Operating SystemsHung Q. Ngo, KHU Spring’09 Example (Cont.) The content of the matrix. Need is defined to be Max – Allocation. Need A B C P 0 7 4 3 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1 The system is in a safe state since the sequence satisfies safety criteria.
28
Chapter 7.28 Operating SystemsHung Q. Ngo, KHU Spring’09 Example P 1 Request (1,0,2) (Cont.) Check that Request Available (that is, (1,0,2) (3,3,2) true. AllocationNeedAvailable A B CA B CA B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 20 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Executing safety algorithm shows that sequence satisfies safety requirement. Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?
29
Chapter 7.29 Operating SystemsHung Q. Ngo, KHU Spring’09 Banker’s Algorithm Example Banker’s algorithm with dining lawyers –“Safe” (won’t cause deadlock) if when try to grab chopstick either: »Not last chopstick »Is last chopstick but someone will have two afterwards –What if k-handed lawyers? Don’t allow if: »It’s the last one, no one would have k »It’s 2 nd to last, and no one would have k-1 »It’s 3 rd to last, and no one would have k-2 »…»…
30
Chapter 7.30 Operating SystemsHung Q. Ngo, KHU Spring’09 Summary Starvation vs. Deadlock –Starvation: thread waits indefinitely –Deadlock: circular waiting for resources Four conditions for deadlocks –Mutual exclusion »Only one thread at a time can use a resource –Hold and wait »Thread holding at least one resource is waiting to acquire additional resources held by other threads –No preemption »Resources are released only voluntarily by the threads –Circular wait » set {T 1, …, T n } of threads with a cyclic waiting pattern
31
Chapter 7.31 Operating SystemsHung Q. Ngo, KHU Spring’09 Summary (2) Techniques for addressing Deadlock –Allow system to enter deadlock and then recover –Ensure that system will never enter a deadlock –Ignore the problem and pretend that deadlocks never occur in the system Deadlock detection –Attempts to assess whether waiting graph can ever make progress Deadlock prevention –Assess, for each allocation, whether it has the potential to lead to deadlock
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.