# Deadlock prevention

## Definition

From the definition:

* deadlock => mutual exclusion **and** hold and wait **and** no preemption **and** circular wait.

Which is equivalent to:

* **not** mutual exclusion **or** **not** hold and wait **or** **not** no preemption **or not** circular wait => **not** deadlock.

So, if in the solution of a concurrent problem at least one of the necessary conditions can never hold, there is no possibility of deadlock.

This is called **deadlock prevention**:

* the prevention lies on the application side.

## Denying the necessary conditions

Denying the **mutual exclusion** condition is only possible if resources are shareable at the same time.

* Otherwise race conditions can occur.

Denying the **preemption** condition is only possible if resources are preemptable.

* Which is often not the case.

Thus, in general, only the other conditions (**hold-and-wait** and **circular wait**) are used to implement deadlock prevention.

In the dining-philosopher problem, the forks are not shareable at the same time.

In the dining-philosopher problem, a fork cannot be taken away from whoever has it.

Denying the **hold-and-wait** condition can be done if a process requests all required resources at once.

* In this solution, starvation can occur.
* **Aging** mechanisms are often used to solve starvation.
