From wikipedia:
In an operating system, a deadlock occurs when a process or thread enters a waiting state because a requested system resource is held by another waiting process, which in turn is waiting for another resource held by another waiting process.
Conditions on the system and its resources that are necessary and sufficient for deadlock to possibly occur.
So violating any condition = preventing deadlock!
A good example of deadlock is the dining philosophers problem. In this problem, there are n philosophers trying to have dinner with n chopsticks. Each requires two chopsticks to eat. How can we allocate the chopsticks such that every philosopher gets to eat?
Have one authority (e.g. a mutex). Have each philosopher grab that authority and only when they have the authority can they pick up their forks and eat. They eat, put the arbitrator and the forks down and move on to the next philosopher (can be random or sequential).
Consider the case of the dining philosophers with n-chopsticks and n-philosophers. Reduce the number of philosophers currently allowed at the table to n-1 using a semaphore. Have them eat. Cycle out the philosophers.
Order the chopsticks 1..n. For each philosopher have them pick up the lower number chopstick. Then, only if they can pick up the lower chopstick, pick up the higher chopstick. Why does this work?
(Beyond 341 scope) If you want a reeallly fast solution, given many philosophers numbered 1..n.
Relax problem assumptions– let philosophers talk to each other only to ask for chopsticks.
Chopsticks can be dirty or clean. Initially all start as dirty. For each pair of philosophers, assign the chopstick between them to the philosopher with the lower id. When one wants to eat, they ask their neighbor for a chopstick. If the neighbor’s chopstick is clean (they haven’t eaten yet), they keep the chopstick. Otherwise, they clean and relinquish it.
We can model resource allocation by having resources and processes represented as vertices and use edges to show ownership of a resource. A cycle in the resource allocation graph implies that we can have deadlock (assuming other Coffman Conditions hold).
The Dining Philosophers provide a more general view of preventing deadlock, and RAG analysis can be applied to some of our previous solutions. Replace ‘philosopher’ with ‘process’ and consider how our solutions stop a cycle.