![Page 1: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/1.jpg)
Deadlocks
Sarah Diesburg Operating Systems CS 3430
![Page 2: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/2.jpg)
Example 1
![Page 3: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/3.jpg)
Deadlocks
Deadlocks: Occur 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)
![Page 4: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/4.jpg)
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
![Page 5: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/5.jpg)
An Example of Deadlocks
Thread A Thread B P(x); P(y); P(y); P(x); A deadlock won’t always happen
with this code, but it might
![Page 6: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/6.jpg)
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
![Page 7: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/7.jpg)
Deadlocks, Deadlocks, Everywhere…
Round-Robin CPU scheduling cannot prevent deadlocks (or starvation) from happening
Can occur whenever there is waiting…
![Page 8: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/8.jpg)
A Classic Example of Deadlocks
Dinning lawyers (philosophers) Each needs two chopsticks to eat
![Page 9: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/9.jpg)
Dining 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…)
![Page 10: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/10.jpg)
A Dining 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]); } }
![Page 11: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/11.jpg)
A Dining 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]); } }
![Page 12: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/12.jpg)
A Dining 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]); } }
![Page 13: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/13.jpg)
Conditions for Deadlocks
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
![Page 14: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/14.jpg)
Deadlock Prevention Techniques
When encountering a deadlock All four conditions must be true
To prevent deadlocks Remove one of the four conditions
![Page 15: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/15.jpg)
Deadlock Prevention Techniques
1. Infinite resources (buy a very large disk)
![Page 16: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/16.jpg)
Deadlock Prevention Techniques
2. No sharing (independent threads)
![Page 17: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/17.jpg)
Deadlock Prevention Techniques
3. Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)
easier said than done…
![Page 18: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/18.jpg)
Deadlock Prevention Techniques
3. 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); } }
![Page 19: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/19.jpg)
Deadlock Prevention Techniques
3. Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)
semaphore chopstick[5] = {1, 1 0, 1 0, 1, 1}, s = 1 0; 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); } }
1
![Page 20: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/20.jpg)
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, 1}, s = 0; 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); } }
1 3
![Page 21: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/21.jpg)
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); } }
![Page 22: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/22.jpg)
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
![Page 23: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/23.jpg)
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); } }
1 3
![Page 24: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/24.jpg)
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]); // deadlock P(chopstick[(j + 1) % 5]; V(s); // eat P(s); V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } }
1 3
2
![Page 25: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/25.jpg)
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); // deadlock V(chopstick[(j + 1) % 5]; V(chopstick[j]); V(s); } }
1 3
2
![Page 26: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/26.jpg)
New Solution
Don’t lock around the chopsticks that are being released
![Page 27: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/27.jpg)
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 V(chopstick[(j + 1) % 5]; V(chopstick[j]); } }
![Page 28: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/28.jpg)
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 V(chopstick[(j + 1) % 5]; V(chopstick[j]); } }
1
![Page 29: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/29.jpg)
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 V(chopstick[(j + 1) % 5]; V(chopstick[j]); } }
1 3
![Page 30: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/30.jpg)
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]); // wait P(chopstick[(j + 1) % 5]; V(s); // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); } }
1 3
2
![Page 31: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/31.jpg)
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 1, 0 1}, s = 0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); // wait P(chopstick[(j + 1) % 5]; V(s); // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); } }
1 3
2
![Page 32: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/32.jpg)
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, 1, 1}, s = 0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); // wait P(chopstick[(j + 1) % 5]; V(s); // eat V(chopstick[(j + 1) % 5]; //wakup! V(chopstick[j]); } }
1
2
![Page 33: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/33.jpg)
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, 1 0, 1 0, 1}, s = 0; lawyer(int j) { while (TRUE) { P(s); P(chopstick[j]); P(chopstick[(j + 1) % 5]; V(s); // eat V(chopstick[(j + 1) % 5]; V(chopstick[j]); } }
1
2
![Page 34: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/34.jpg)
More Elaborate Solution
Allocate some counters If someone can’t pick up both
chopsticks after taking the lock, release the lock
![Page 35: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/35.jpg)
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); } }
![Page 36: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/36.jpg)
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
![Page 37: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/37.jpg)
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
![Page 38: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/38.jpg)
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
![Page 39: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/39.jpg)
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, 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)]); } }
![Page 40: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/40.jpg)
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
![Page 41: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/41.jpg)
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
![Page 42: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/42.jpg)
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 1 2
![Page 43: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/43.jpg)
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 1 2 3
![Page 44: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/44.jpg)
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 1 2 3 4
![Page 45: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/45.jpg)
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 1 2 3 4
![Page 46: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/46.jpg)
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 1 2 3 4
![Page 47: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/47.jpg)
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 1 2 3 4
![Page 48: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/48.jpg)
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)]); } }
0 1 2 3 4
![Page 49: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/49.jpg)
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)]); } }
0 1 2 3
4
![Page 50: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/50.jpg)
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)]); } }
0 1 2
3
4
![Page 51: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/51.jpg)
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)]); } }
0 1 2
3
4
![Page 52: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/52.jpg)
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 1 2
3
4
![Page 53: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/53.jpg)
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 1 2
3
4
![Page 54: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/54.jpg)
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)]); } }
0 1
2 3
4
![Page 55: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/55.jpg)
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
2 3
4
![Page 56: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/56.jpg)
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 1
2 3
4
![Page 57: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/57.jpg)
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
2 3
4
![Page 58: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/58.jpg)
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 2 3
4
![Page 59: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/59.jpg)
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
1 2 3
4
![Page 60: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/60.jpg)
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 2 3
4
![Page 61: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/61.jpg)
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
1 2 3
4
![Page 62: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/62.jpg)
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 1 2 3
4
![Page 63: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/63.jpg)
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)]); } }
0 1 2 3
4
![Page 64: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/64.jpg)
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 1 2 3
4
![Page 65: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/65.jpg)
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)]); } }
0 1 2 3
4
![Page 66: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/66.jpg)
More Deadlock Prevention Methods
5. Banker’s algorithm 6. A combination of techniques
![Page 67: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/67.jpg)
Banker’s Algorithm
The idea of Banker’s algorithm: Allows the sum of requested resources
> total resources As long as, there is some way for all
threads to finish without getting into any deadlocks
![Page 68: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/68.jpg)
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
![Page 69: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/69.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 80 MB 30 MB
P2 10 MB 10 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
![Page 70: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/70.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 80 MB 30 MB
P2 20 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36
![Page 71: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/71.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 80 MB 30 MB
P2 0 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
![Page 72: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/72.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 110 MB 0 MB
P2 0 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
P1
26
![Page 73: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/73.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 0 MB 0 MB
P2 0 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
P1
26
136
![Page 74: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/74.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 0 MB 0 MB
P2 0 MB 0 MB
P3 200 MB 0 MB
time
free RAM (MB)
46
P2
36 56
P1
26
136
P3
56
![Page 75: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/75.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 0 MB 0 MB
P2 0 MB 0 MB
P3 0 MB 0 MB
time
free RAM (MB)
46
P2
36 56
P1
26
136
P3
56
256
![Page 76: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/76.jpg)
Example 1
Total RAM 256 MB
Allocated Still needed
P1 80 MB 30 MB
P2 10 MB 10 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
P1
26
136
P3
56
256
The system is initially in a safe state, since we can run P2, P1, and then P3
![Page 77: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/77.jpg)
Example 2
Total RAM 256 MB
Allocated Still needed
P1 80 MB 60 MB
P2 10 MB 10 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
![Page 78: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/78.jpg)
Example 2
Total RAM 256 MB
Allocated Still needed
P1 80 MB 60 MB
P2 20 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36
![Page 79: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/79.jpg)
Example 2
Total RAM 256 MB
Allocated Still needed
P1 80 MB 60 MB
P2 0 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
![Page 80: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/80.jpg)
Example 2
Total RAM 256 MB
Allocated Still needed
P1 80 MB 60 MB
P2 0 MB 0 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
![Page 81: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/81.jpg)
Example 2
Total RAM 256 MB
Allocated Still needed
P1 80 MB 60 MB
P2 10 MB 10 MB
P3 120 MB 80 MB
time
free RAM (MB)
46
P2
36 56
The system is initially in an unsafe state, since we cannot find an execution sequence for P1, P2, and P3
![Page 82: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/82.jpg)
Deadlock Detection and Recovery
Scan the resource allocation graph Detect circular chains of requests Recover from the deadlock
![Page 83: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/83.jpg)
Resource Allocation Graph
Resource X Resource Y
waiting for
waiting for
Thread A owned by
Thread B owned by
![Page 84: Deadlocks - UNI Department of Computer Sciencediesburg/courses/cs3430_sp15/sessions/s...Deadlocks: Occur when threads are waiting for resources with circular dependencies Often involve](https://reader033.vdocuments.us/reader033/viewer/2022050109/5f4714984e035c73d33d8b3d/html5/thumbnails/84.jpg)
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