chapter 8 deadlock questions
play

Chapter 8: Deadlock Questions? What is a deadlock? CSCI [4|6]730 - PDF document

Chapter 8: Deadlock Questions? What is a deadlock? CSCI [4|6]730 What causes a deadlock? Operating Systems How do you deal with (potential) deadlocks? Deadlock 2 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock: What


  1. Chapter 8: Deadlock Questions? � � What is a deadlock? CSCI [4|6]730 � � What causes a deadlock? Operating Systems � � How do you deal with (potential) deadlocks? Deadlock 2 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock: What is a deadlock? Example: Two Threads? No problem -- � I don’t back � I do! � up for idiots � � � Two threads access two shared variables, A and B » � Variable A is protected by lock a » � Variable B by lock b � � How to add lock and unlock statements? Thread Maria Thread Tucker Deitel & Deitel anecdote lock(a); lock(b) � � All are waiting for a resource that is held by another A += 10; B += 10; waiting entity. Since all are waiting, none can provide lock(b); lock(a); Does this B += 20; A += 20; any of the things being waited for. work? � � Example: narrow bridge (resource) -- A += B; A += B; unlock(b) unlock(a); » � if a deadlock occurs, resolved if one car back up A += 30 B += 30 (preempts resource and rollback). unlock(a) unlock(b); 3 4 Maria Hybinette, UGA Maria Hybinette, UGA Example: Maria & Tucker Representing Deadlock � � Two common ways of representing deadlock: Maria gets lock a Maria waits for lock b » � Vertices (circles or rectangles) Thread Maria – � threads (or processes) in system Tucker gets lock b Tucker waits lock b – � resources [types] (e.g., locks, semaphores, printers) » � Edges: indicates (`waiting for’ or `wants’) or `held by’ Thread Tucker direction of the edge determines the type. Wait-For Graph � Resource Allocation Graph � Time Thread Maria Thread Tucker lock(a); lock(b) wants � held by � A += 10; B += 10; “waiting for” � R 1 � lock(b); lock(a); B += 20; A += 20; T 1 � T 2 � T 1 � T 2 � A += B; A += B; unlock(b) unlock(a); R 2 � A += 30 B += 30 unlock(a) unlock(b); 5 6 held by � wants � Maria Hybinette, UGA Maria Hybinette, UGA

  2. Conditions for Deadlock Handling Deadlock Ignore 1. � � � Mutual exclusion: » � Easiest and most common approach (e.g., UNIX). Deadlock prevention » � Resource cannot be shared 2. � Ostrich algorithm » � Requests are delayed until resource is released » � Ensure deadlock does not happen » � Ensure at least one of 4 conditions does not occur � � Hold and wait: 1. � Hold&Wait, No Preemption, Circularity, Mutual Exclusion » � Thread holds one resource while waits for another 2. � System build so deadlock cannot happen � � No preemption: Deadlock detection and recovery 3. � » � previously granted resources cannot forcibly be taken away » � Allow deadlocks, but detect when occur � � Circular wait: » � Recover and continue » � Circular dependencies exist in “waits-for” or “resource- Deadlock avoidance allocation” graphs 4. � » � Ensure deadlock does not happen » � Each is waiting for a resource held by next member of the chain. » � Use information about resource requests to dynamically avoid unsafe situations All for conditions must hold simultaneously � 7 8 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock Prevention: Deadlock Prevention Mutual Exclusion Mutual exclusion Mutual exclusion Hold and wait Hold and wait � � Approach � � No mutual exclusion --> Make No preemption No preemption » � Ensure 1 of 4 conditions cannot occur resource sharable ; examples: Circular wait Circular wait » � Negate each of the 4 conditions » � Read-only files � � No single approach is appropriate (or » � Printer daemon needs exclusive access to the printer, there is only one possible) for all circumstances printer daemon -- uses spooling. 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock Prevention Deadlock Prevention Hold and Wait Hold and Wait Mutual exclusion Mutual exclusion Hold and wait Hold and wait Two Approaches: Two Approaches: � � � � No preemption No preemption Circular wait Circular wait 1. � Only request resources when it does 2. � Atomically acquire all resources at not hold other resources once – � release resources before requesting » � Example: Single lock to protect all new ones (other variations - e.g., release access Thread Maria Thread Tucker to one variable earlier) lock(a); lock(b) B += 10; A += 10; Thread Maria Thread Tucker Unlock(b); unlock(a) lock(b); lock(a); lock(AB) A += 20; lock(AB); B += 20; A += 10; B += 10; unlock(b) unlock(a); A += 20; lock(b) B += 20; lock(a) B += 30 B += 30 A += 30 A += 30 unlock(AB); unlock(AB) unlock(a) unlock(b); 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Deadlock Prevention Deadlock Prevention Hold and Wait No Preemption Mutual exclusion Mutual exclusion Hold and wait Hold and wait Summary the Two Approaches: Two Approaches: � � � � No preemption No preemption 1. � Only request resources when it does not hold 1. � Preempt requestors resource Circular wait Circular wait other resources – � Example: B is holding some resources and then requests additional resources that are 2. � Atomically acquire all resources at once held by other threads, then B releases all its Problems: � � resources (and start over) » � Low resource utilization: ties up resources 2. � Preempt holders resource other processes could be using – � Example: A waiting for something held by B, then take resource away from B and give them to A (B » � May not know required resources before run starts over). » � Starvation: A thread that need popular Not possible if resource cannot be saved � � resources may wait forever and restored » � Can’t take away a lock without causing problems Only works for some resources (e.g., CPU � � and memory) 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock Prevention Circular Wait Condition Deadlock Detection & Recovery Mutual exclusion Hold and wait � � Impose ordering on resources 1. � Allow system to enter deadlock state No preemption » � Give all resources a ranking; must Circular wait 2. � Detection algorithm acquire highest ranked resource first 3. � Recovery scheme 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock Detection Single Instance of Each Resource Type Depth first search (example) � � Maintain wait-for graph For each node in the graph: » � Nodes are processes. L = {empty list} and Nodes = {unvisited}; » � removes resource nodes and collapsing edges current node = initial node ; » � P i � P j if P i is waiting for P j . while( current node is not the initial node twice ) � � Periodically invoke an algorithm that searches for a L.enqueue(current node); // add to node to end of L cycle in the graph. if( current node is in L twice ) � � An algorithm to detect a cycle in a graph requires an there is a cycle � cycle and return order of n 2 operations, where n is the number of if( there is an unmarked arc ) vertices in the graph. mark the arc as visited and use destination as new current node else go back to previous node Back to initial node there is no cycle 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

  4. Deadlock detection (1 resource of each) Example: Deadlock Detection � � Do a depth-first-search on the resource � � Do a depth-first-search on the resource allocation graph allocation graph D, E, G ? Initialize a list to the empty list, designate arcs as ‘unvisited’ are deadlocked A, C, F ? are not deadlocked because S can be allocated to either and then the other two can take turn to complete T 19 20 Maria Hybinette, UGA Maria Hybinette, UGA Example: Deadlock Detection Example: Deadlock Detection � � Do a depth-first-search on the resource � � Do a depth-first-search on the resource allocation graph allocation graph T T 21 22 Maria Hybinette, UGA Maria Hybinette, UGA Deadlock Detection with Example: Deadlock Detection Multiple Resources � � Do a depth-first-search on the resource � � Theorem: If a graph does not contain a cycle allocation graph then no processes are deadlocked » � A cycle in a RAG is a necessary condition for deadlock » � Is it a sufficient condition? Printers waiting holding T CD-WR holding waiting 23 24 holding Maria Hybinette, UGA Maria Hybinette, UGA

  5. Deadlock Detection Algorithm: Multiple Resource Instances Example Doesn’t � � Is there a possible allocation sequence of resources Change so that each process can complete? What I have (now!) What I am requesting now � � Available: Indicates the number of available resources of each type (m) � � Allocation: Number of resources of each type currently allocated (nxm) � � Request: current requests of each thread (nxm) 25 26 » � If Request [ i j ] = k , then process P i is requesting k more instances of type. R j . Maria Hybinette, UGA Maria Hybinette, UGA Detection algorithm Detection algorithm A marked process means it can run to completion 1. Look for an unmarked process Pi, for which the i th row of R (need) is less than or equal to A 2. If such a process is found, add the i-th row of C to A, mark the process and go back to step 1 3. If no such process exists the algorithm terminates If all marked, no deadlock 27 28 Maria Hybinette, UGA Maria Hybinette, UGA Detection algorithm Detection algorithm 29 30 Maria Hybinette, UGA Maria Hybinette, UGA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend