1 / 14

Deadlock

Deadlock. Introduction to Parallel Programming – Part 7. Review & Objectives. Previously: Gave examples of ways that threads may contend for shared resources Describe what race conditions are and explain how to eliminate them in OpenMP code

zeheb
Download Presentation

Deadlock

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deadlock Introduction to Parallel Programming – Part 7

  2. Review & Objectives • Previously: • Gave examples of ways that threads may contend for shared resources • Describe what race conditions are and explain how to eliminate them in OpenMP code • At the end of this part you should be able to: • Define deadlock and explain ways to prevent it

  3. Locks Are Dangerous • Suppose a lock is used to guarantee mutually exclusive access to a shared variable • Imagine two threads, each with its own critical region • Thread 1 Thread 2 • a += 5; b += 5; • b += 7; a += 7; • a += b; a += b; • a += 11; b += 11;

  4. Faulty Implementation • Thread 1 Thread 2 • lock (lock_a); lock (lock_b); • a += 5; b += 5; • lock (lock_b); lock (lock_a); • b += 7; a += 7; • a += b; a += b; • unlock (lock_b); unlock (lock_a); • a += 11; b += 11; • unlock (lock_a); unlock (lock_b);

  5. Faulty Implementation What happens if threads are at this point at the same time? • Thread 1 Thread 2 • lock (lock_a); lock (lock_b); • a += 5; b += 5; • lock (lock_b); lock (lock_a); • b += 7; a += 7; • a += b; a += b; • unlock (lock_b); unlock (lock_a); • a += 11; b += 11; • unlock (lock_a); unlock (lock_b);

  6. Deadlock • A situation involving two or more threads (processes) in which no thread may proceed because each is waiting for a resource held by another • Can be represented by a resource allocation graph • A graph of deadlock contains a cycle wants lock_b held by Thread 1 Thread 2 lock_a held by wants

  7. More on Deadlocks • A program exhibits a global deadlock if every thread is blocked • A program exhibits local deadlock if only some of the threads in the program are blocked • A deadlock is another example of a nondeterministic behavior exhibited by a parallel program • Adding debugging output to detect source of deadlock can change timing and reduce chance of deadlock occurring

  8. Four Conditions for Deadlock • Mutually exclusive access to a resource • Threads hold onto resources they have while they wait for additional resources • Resources cannot be taken away from threads • Cycle in resource allocation graph

  9. Preventing Deadlock • Eliminate one of four necessary conditions • Don’t allow mutually exclusive access to resource • Don’t allow threads to wait while holding resources • Allow resources to be taken away from threads • Make sure request allocation graph cannot have a cycle

  10. Deadlock Prevention Strategies

  11. Correct Implementation • Thread 1 Thread 2 • lock (lock_a); lock (lock_a); • a += 5; lock (lock_b); • lock (lock_b); b += 5; • b += 7; a += 7; • a += b; a += b; • unlock (lock_b); unlock (lock_a); • a += 11; b += 11; • unlock (lock_a); unlock (lock_b); Threads must lock lock_a before lock_b

  12. Another Problem with Locks • Every call to function lock should be matched with a call to unlock, representing the start and the end of the critical section • A program may be syntactically correct (i.e., may compile) without having matching calls • A programmer may forget the unlock call or may pass the wrong argument to unlock • A thread that never releases a shared resource creates a deadlock

  13. References • Andrea C. Arpaci-Dusseau and Remzi H. Arpaci-Dusseau, “Deadlock”, CS 537, Introduction to Operating Systems, Computer Sciences Department, University of Wisconsin-Madison. • Jim Beveridge and Robert Wiener, Multithreading Applications in Win32®, Addison-Wesley (1997). • Richard H. Carver and Kuo-Chung Tai, Modern Multithreading: Implementing, Testing, and Debugging Java and C++/Pthreads/ Win32 Programs, Wiley-Interscience (2006). • Michael J. Quinn, Parallel Programming in C with MPI and OpenMP, McGraw-Hill (2004). • Brent E. Rector and Joseph M. Newcomer, Win32 Programming, Addison-Wesley (1997). • N. Wirth, Programming in Modula-2, Springer (1985).

More Related