Wiki source code of Deadlock Prevention

Version 1.3 by chrisby on 2023/11/26 19:27

Hide last authors
chrisby 1.1 1 Deadlocks can only occur if all four specific conditions are met. Therefore, strategies to prevent deadlocks focus on negating one of these conditions.
2
chrisby 1.3 3 | ------------------------ | ------------------------------------------------------------------------------------------ | --------- | ---------------- |
4 | Condition | Description | Solutions | Dangers/Problems |
5 | Mutual Exclusion / Mutex |
chrisby 1.1 6
chrisby 1.3 7 When resources can't be used by mutual thread and there are less resources than threads. | | |
8 | | | | |
9 | | | | |
10 | | | | |
chrisby 1.1 11
12 #### Lock & Wait
13
14 * Description:
15 * Once a thread acquires a resource, it will not release the resource until it has acquired all of the other resources it requires and has completed its work.
16 * Solutions:
17 * Before reservation of a resource, check its accessibility.
18 * If a resource is not accessible, release every resource and start from anew.
19 * Dangers:
20 * Starvation: A thread never achieves to reserve all required resources.
21 * Livelock: Thread gets tangled up.→ This approach is always applicable but inefficient as it causes a bad performance.
22
23 #### No preemption
24
25 * Description:
26 * A thread is unable to steal a resources reserved by another thread.
27 * Solution:
28 * A thread is allowed to ask another thread to release all of its resources (including the required one) and starting from anew. This approach is similar to the 'Lock & Wait' solution but has a better performance.
29
30 #### Circular Waiting / Deadly Embrace
31
32 * Description:
33 * When two or more threads require a resource which is already reserved by another of these threads.
34 * Example:
35 * Thread T1 has resource R1 and waits for R2 to be released.
36 * Thread T2 has resource R2 and waits for R1 to be released.
37 * Solution:
38 * All threads reserve all resources in a the same order.
39 * Problems:
40 * The order of reservation doesn't necessarily have to be the same as the order of usage. This leads to inefficiencies like reserving a resource at the beginning which is just required at the end of the task.
41 * Unnecessarily long locked resources.
42 * Order can not always be specified.