# Deadlock prevention by killing a victim

In our operating systems class, the teacher told us about deadlocks prevention methods. One of them is that to select as a victim the process which requires the CPU time or one that requires a lot of resources, and remove this process. The next time the algorithm is called, it may be possible that again the same process becomes the victim. This causes starvation.

I'm confused: if the victim process is terminated, then how can it again become part of the deadlock?

The idea is that the process is not terminated: only the current operation is cancelled. The process reacts to the cancellation by trying again. In this model, it is assumed that each process runs something like the following pseudocode:

begin_transaction:
try:
acquire(lock_1);
acquire(lock_2);
…
release(lock_2);
release(lock_1);
with Cancelled:
goto begin_transaction


Without any form of deadlock prevention, the processes would get stuck forever. Cancelling one of the processes (in a way that releases its locks) prevents that.

This form of deadlock prevention only works if processes are written in the expected way and can cope with cancellation. It does not generalize to arbitrary programs.