Deadlock Management in Multi-Process Systems

In a multi-process system, a deadlock is an undesirable situation that occurs in a shared resource environment. It happens when a process waits indefinitely for a resource held by another process.

For example, consider a set of transactions {T0, T1, T2, …,Tn}. T0 needs resource X, which is held by T1. T1 waits for resource Y, held by T2. T2 waits for resource Z, held by T0. All processes are waiting for each other, and none can finish. This is a deadlock.

Deadlocks are detrimental to a system. When a system is in a deadlock, the involved transactions are either rolled back or restarted.

Deadlock Prevention

To prevent deadlocks, the DBMS rigorously inspects all operations before transactions execute. It analyzes if they could lead to a deadlock. If a potential deadlock is detected, the transaction is not allowed to execute.

Some deadlock prevention schemes use timestamp ordering of transactions to predict potential deadlocks.

Wait-Die Scheme

In this scheme, if a transaction requests a lock on a resource already held by another transaction with a conflicting lock, one of two things happens:

  • If TS(Ti) < TS(Tj) (Ti is older than Tj), Ti waits until the resource is available.

  • If TS(Ti) > TS(Tj) (Ti is younger than Tj), Ti dies and is restarted later with a random delay but the same timestamp.

This scheme allows older transactions to wait but kills younger ones.

Wound-Wait Scheme

In this scheme, if a transaction requests a lock on a resource already held by another transaction with a conflicting lock, one of two things happens:

  • If TS(Ti) < TS(Tj), Ti forces Tj to roll back (Ti wounds Tj). Tj is restarted later with a random delay but the same timestamp.

  • If TS(Ti) > TS(Tj), Ti waits until the resource is available.

This scheme allows younger transactions to wait. When an older transaction requests a resource held by a younger one, the older transaction forces the younger one to abort and release the resource.

In both cases, the transaction that enters the system later is aborted.

Deadlock Avoidance

Aborting a transaction is not always practical. Deadlock avoidance mechanisms can detect potential deadlocks in advance. Methods like the wait-for graph are suitable for systems with lightweight transactions and fewer resource instances. In larger systems, deadlock prevention techniques may be more effective.

Wait-for Graph

This method tracks potential deadlocks. A node is created for each transaction. When transaction Ti requests a lock on item X held by Tj, a directed edge is created from Ti to Tj. When Tj releases X, the edge is dropped, and Ti locks the item.

The system maintains this graph for every transaction waiting for resources. It checks for cycles in the graph.

Wait-for Graph

Two approaches can be used:

  • First, do not allow requests for items already locked by another transaction. This can cause starvation, where a transaction waits indefinitely for a resource.

  • Second, roll back one of the transactions. It’s not always feasible to roll back the younger transaction, as it may be more important. An algorithm selects a transaction to abort, known as the victim, and this process is called victim selection.