deadlock
play

Deadlock If you are not careful, it can lead to deadlock Today s - PDF document

Concurrency Issues Past lectures: Problem: Safely coordinate access to shared resource Solutions: Use semaphores, monitors, locks, condition variables Coordinate access within shared objects What about coordinated access across


  1. Concurrency Issues Past lectures: Ø Problem: Safely coordinate access to shared resource Ø Solutions: ❖ Use semaphores, monitors, locks, condition variables ❖ Coordinate access within shared objects What about coordinated access across multiple objects? Deadlock Ø If you are not careful, it can lead to deadlock Today ’ s lecture: Ø What is deadlock? Ø How can we address deadlock? 1 2 Deadlocks Deadlock Motivating Examples Definition Two producer processes share a buffer but use a different protocol for accessing the buffers Ready Running Head Producer1() { Producer2(){ P( emptyBuffer ) P( producerMutexLock ) Tail P( producerMutexLock ) P( emptyBuffer ) ready queue Waiting : : } } Head semaphore/ A postscript interpreter and a visualization program compete for Tail condition queues memory frames A set of processes is deadlocked when every process in the set is waiting for an event that can only be generated by some process in PS_Interpreter() { Visualize() { the set request(memory_frames, 10) request(frame_buffer, 1) < process file > < display data > Starvation vs. deadlock request(frame_buffer, 1) request(memory_frames, 20) Ø Starvation: threads wait indefinitely (e.g., because some other thread is < draw file on screen > < update display > using a resource) } } Ø Deadlock: circular waiting for resources Ø Deadlock è starvation, but not the other way 3 4 A Graph Theoretic Model of Deadlock Resource Allocation Graphs The resource allocation graph ( RAG ) Examples Basic components of any resource allocation problem A PostScript interpreter that is waiting for the frame buffer lock Ø Processes and resources and a visualization process that is waiting for memory Model the state of a computer system as a directed graph V = { PS interpret , visualization } ∪ { memory frames , frame buffer lock } Ø G = ( V , E ) Ø V = the set of vertices = { P 1 , ..., P n } ∪ { R 1 , ..., R m } P i R j Ø E = the set of edges = Visualization Memory Frames Process PostScript { edges from a resource to a process } ∪ Interpreter { edges from a process to a resource } request allocation edge edge P i P k Frame Buffer R j 5 6

  2. A Graph Theoretic Model of Deadlock A Graph Theoretic Model of Deadlock Resource allocation graphs & deadlock Resource allocation graphs & deadlock Theorem: If a resource allocation graph does not contain a cycle then Theorem: If there is only a single unit of all resources then a set of no processes are deadlocked processes are deadlocked iff there is a cycle in the resource A cycle in a RAG is a necessary condition for deadlock allocation graph Is the existence of a cycle a sufficient condition? Memory Frames Game Visualization PostScript Process Interpreter Memory Frames Visualization PostScript Process Interpreter Frame Buffer Frame Buffer 7 8 Using the Theory Dealing With Deadlock An operational definition of deadlock Deadlock prevention & avoidance Adopt some resource allocation protocol that ensures deadlock can never occur Visualization Memory Frames Process PostScript Ø Deadlock prevention/avoidance Interpreter ❖ Guarantee that deadlock will never occur ❖ Generally breaks one of the following conditions: ◆ Mutex Frame Buffer ◆ Hold-and-wait ◆ No preemption ◆ Circular wait *This is usually the weak link* A set of processes are deadlocked iff the following conditions hold Ø Deadlock detection and recovery simultaneously ❖ Admit the possibility of deadlock occurring and periodically check for it 1. Mutual exclusion is required for resource usage (serially useable) ❖ On detecting deadlock, abort 2. A process is in a “ hold-and-wait ” state ◆ Breaks the no-preemption condition 3. Preemption of resource usage is not allowed 4. Circular waiting exists (a cycle exists in the RAG ) What does the RAG for a lock look like? 9 10 Deadlock Avoidance Deadlock Detection & Recovery Resource Ordering Recovering from deadlock Recall this situation. How can we avoid it? R 1 R 2 R 3 R 4 Producer1() { Producer2(){ P( emptyBuffer ) P( producerMutexLock ) P( producerMutexLock ) P( emptyBuffer ) : : } P 1 P 2 P 3 P 4 P 5 } Eliminate circular waiting by ordering all locks (or semaphores, or resoruces). All code grabs locks in a Abort all deadlocked processes & reclaim their resources predefined order. Problems? Abort one process at a time until all cycles in the RAG Ø Maintaining global order is difficult, especially in a large project. are eliminated Ø Global order can force a client to grab a lock earlier than it Where to start? would like, tying up a resource for too long. Ø Select low priority process Ø Deadlock is a global property, but lock manipulation is local. Ø Processes with most allocation of resources Caveat: ensure that system is in consistent state (e.g., transactions) Optimization: Ø Checkpoint processes periodically; rollback processes to checkpointed state 11 12

  3. Dealing With Deadlock Dealing With Deadlock Deadlock avoidance – Banker’s Algorithm Deadlock detection & recovery Examine each resource request and determine whether or not granting the request can lead to deadlock What are some problems with the banker ’ s algorithm? Ø Very slow O(n 2 m) Define a set of vectors and matrices that characterize the Ø Too slow to run on every allocation. What else can we do? current state of all resources and processes Deadlock prevention and avoidance: Ø resource allocation state matrix Ø Develop and use resource allocation mechanisms and protocols that R 1 R 2 R 3 ... R r Alloc ij = the number of units of prohibit deadlock P 1 n 1,1 n 1,2 n 1,3 ... n 1, r resource j held by process i Deadlock detection and recovery: P 2 n 2,1 n 2,2 Ø maximum claim matrix . Ø Let the system deadlock and then deal with it n 3,1 . Max ij = the maximum number of units P 3 . ... Detect that a set of processes are deadlocked of resource j that the process i will ... ... Recover from the deadlock ever require simultaneously P p n p ,1 n p , r ... Ø available vector Avail j = the number of units of < n 1 , n 2 , n 3 , ..., n r > resource j that are unallocated 13 14

Recommend


More recommend