Approaches to deadlock situations
Intuitively, each of the following approaches looks to eliminate one of the four Coffman conditions from our program to prevent deadlocks. Our first solution is to implement ranking among the competing resources.
Implementing ranking among resources
From both the dining philosophers problem and our Python example, we can see that the last condition of the four Coffman conditions, circular wait, is at the heart of the deadlock problem. It specifies that the different processes (or threads) in our concurrent program wait for resources held by other processes (or threads) circularly. By taking a closer look, we can see that the root cause for this condition is the order (or lack thereof) in which the processes (or threads) access the resources.
In the dining philosophers problem, each philosopher is instructed to pick up the fork on their left first, while in our Python example, the threads always try to acquire the locks with the same name before...