Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.

Similar presentations


Presentation on theme: "Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot."— Presentation transcript:

1 Deadlocks

2  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot be taken away from its current thread without failing the computation (e.g., storage allocated to a file)

3 Deadlocks  Deadlocks that involve preemptable resources (e.g., CPU) can usually be resolved by reallocation of resources  Starvation: a thread waits indefinitely  A deadlock implies starvation Starvation can also caused by livelocks.

4 An Example of Deadlocks Thread AThread B P(x);P(y); P(y);P(x);  A deadlock won’t always happen with this code, but it might

5 Deadlocks, Deadlocks, Everywhere…  Can happen with any kind of resource Among multiple resources  Cannot be resolved for each resource independently A thread can grab all the memory The other grabs all the disk space Each thread may need to wait for the other to release

6 Deadlocks, Deadlocks, Everywhere…  Round-Robin CPU scheduling cannot prevent deadlocks (or starvation) from happening  Can occur whenever there is waiting…

7 A Classic Example of Deadlocks  Dinning lawyers (philosophers)  Each needs two chopsticks to eat

8 Dinning Lawyers  If each first grabs the chopstick on their right before the one on their left, and all grab at the same time, we have a deadlock (Personally, I prefer to starve than share chopsticks…)

9 A Dinning Lawyer Implementation semaphore chopstick[5] = {1, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[j]); P(chopstick[(j + 1) % 5]; // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); }

10 A Dinning Lawyer Implementation // chopstick[5] = {0, 0, 0, 0, 0}; lawyer(int j) { while (TRUE) { P(chopstick[j]); P(chopstick[(j + 1) % 5]; // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); }

11 A Dinning Lawyer Implementation // chopstick[5] = {0, 0, 0, 0, 0}; lawyer(int j) { while (TRUE) { P(chopstick[j]); P(chopstick[(j + 1) % 5]; // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); }

12 Necessary Conditions for deadlock  Four necessary (but not sufficient) conditions Limited access (lock-protected resources) No preemption (if someone has the resource, it cannot be taken away) Wait while holding (holding a resource while requesting and waiting for the next resource) Circular chain of requests

13 Dealing with deadlock  Deadlock prevention Make sure that at least one of the conditions for deadlock does not hold. Prevent deadlock from happening  Deadlock avoidance Make sure the system is always in a safe state.  Deadlock detection and recovery Let it be. Deal with it only when bad things (deadlock) happens.  The chance for deadlock to happen is not big.

14 Deadlock Prevention Techniques  Involves removing one of the four conditions

15 Deadlock Prevention Techniques 1. Limited resource  Virtualize the physical limited resource to make infinite instances  Example: printer pool

16 Deadlock Prevention Techniques 2. No preemption  Make the source preempt-able  CPU sharing, copy memory to disk  Not always possible for all resources

17 Deadlock Prevention Techniques  Wait while holding Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) easier said than done…

18 Deadlock Prevention Techniques  Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) semaphore chopstick[5] = {1, 1, 1, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); }

19 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 1, 1, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); }

20 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 1  0, 1  0, 1, 1}, s = 1  0  1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 1

21 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 0, 0, 1  0, 1  0}, s = 1  0  1; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 13

22 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 0, 0, 0, 0}, s = 1  0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 13 2

23 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // chopstick[5] = {1, 0, 0, 0, 0}, s = 0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } 13 2

24 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) int counter[5] = {1, 1, 1, 1, 1}; semaphore chopstick[5] = {1, 1, 1, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); }

25 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // counter[5] = {1, 0, 0, 1, 1}; chopstick[5] = {1, 0, 0, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); } (1, 2) 1

26 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // counter[5] = {1, 0, 0, 1, 1}; chopstick[5] = {1, 0, 0, 1, 1}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); } (1, 2) 1 () 2

27 Deadlock Prevention Techniques 3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time) // counter[5] = {1, 0, 0, 0, 0}; chopstick[5] = {1, 0, 0, 0, 0}, s = 1; lawyer(int j) { while (TRUE) { P(s); // if both counters j and (j + 1) % 5 > 0, decrement counters // and grab chopstick[j] and chopstick[(j + 1) % 5] V(s); // if holding both chopsticks, eat P(s); // release chopsticks and increment counters as needed V(s); } (1, 2) 1 (3, 4) 3

28 Deadlock Prevention Techniques 4. Circular chain of requests Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)) // chopstick[5] = {1, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); }

29 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {1  0, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0

30 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 1  0, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 1

31 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 1  0, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 12

32 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 1  0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 123

33 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 1234

34 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 1234

35 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 01 234

36 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 012 34

37 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1  0}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0123 4

38 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 0}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 012 3 4

39 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 0  1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 012 3 4

40 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0  1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 012 3 4

41 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 1  0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 012 3 4

42 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 01 2 3 4

43 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 0  1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 01 2 3 4

44 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0  1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 01 23 4

45 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 1  0, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 01 23 4

46 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 1 23 4

47 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 0  1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 1 23 4

48 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0  1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 123 4

49 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 1  0, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 123 4

50 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 123 4

51 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 0  1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0 123 4

52 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0  1, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0123 4

53 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {1  0, 1, 1, 1, 1}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0123 4

54 Deadlock Prevention Techniques 4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y) // chopstick[5] = {0, 1, 1, 1, 1  0}; lawyer(int j) { while (TRUE) { P(chopstick[min(j, (j + 1) % 5)]); P(chopstick[max(j, (j + 1) % 5)]); // eat V(chopstick[max(j, (j + 1) % 5)]); V(chopstick[min(j, (j + 1) % 5)]); } 0123 4

55 Deadlock avoidance: Banker’s Algorithm  The idea of Banker’s algorithm: Allows the sum of requested resources > total resources  Before granting any resources, make sure that there is some way for all threads to finish without getting into any deadlocks.

56 Banker’s Algorithm  Banker’s algorithm: A thread states its maximum resource needs in advance The OS allocates resource dynamically as needed. A thread waits if granting its request would lead to deadlocks A request can be granted if some sequential ordering of threads is deadlock free (in safe state after granting) A request will not be granted if the state is unsafe after granting.

57 Example 1 (safe state)  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46

58 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 20 MB0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36

59 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56

60 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 110 MB0 MB P2P2 P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56 P1P1 26

61 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 0 MB P2P2 P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56 P1P1 26 136

62 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 0 MB P2P2 P3P3 200 MB0 MB time free RAM (MB) 46 P2P2 36 56 P1P1 26 136 P3P3 56

63 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 0 MB P2P2 P3P3 time free RAM (MB) 46 P2P2 36 56 P1P1 26 136 P3P3 56 256

64 Example 1  Total RAM 256 MB AllocatedStill needed P1P1 80 MB30 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56 P1P1 26 136 P3P3 56 256 The system is initially in a safe state, since we can run P 2, P 1, and then P 3

65 Example 2 (unsafe state)  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46

66 Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 20 MB0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36

67 Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56

68 Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 0 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56

69 Example 2  Total RAM 256 MB AllocatedStill needed P1P1 80 MB60 MB P2P2 10 MB P3P3 120 MB80 MB time free RAM (MB) 46 P2P2 36 56 The system is initially in an unsafe state, since we cannot find an execution sequence for P 1, P 2, and P 3

70 Banker’s algorithm  Every time a request is granted, the banker makes sure that the system is in a safe state.

71 Deadlock Detection and Recovery  Scan the resource allocation graph  Detect circular chains of requests  Recover from the deadlock

72 Resource allocation graph  Nodes: A node for each thread A node for each resource  Edges: Between a thread and a resource Resource -> thread if the thread owns the resource Thread -> resource if the thread waits for the resource

73 Resource Allocation Graph Resource X Resource Y waiting for Thread A owned by Thread B owned by

74 Detecting deadlock with resource allocation graph  A deadlock involves the resources and threads that form a circle in the resource allocation graph.

75 Once A Cycle is Detected…  Some possible actions Kill a thread and force it to give up resources  Remaining system may be in an inconsistent state Rollback actions of a deadlocked thread  Not always possible (a file maybe half- way through modifications)  Need checkpointing, or taking snapshots of system states from time to time

76 Questions  Which technique allows most concurrrency, deadlock prevention, deadlock avoidance, or deadlock detection and recovery?  Which technique is most effective in dealing with deadlocks?  Which technique is in windows?


Download ppt "Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot."

Similar presentations


Ads by Google