Home >>DBMS Tutorial >DBMS Deadlock
In a multi-process system, deadlock is an unwanted situation that occurs in a shared resource setting where a process is waiting for a resource held by another process indefinitely.
For example , assume a {T0, T1, T2, ..., Tn} transaction set. In order to complete its task, T0 requires resource X. T1 is the holding of resource X, and T1 is waiting for resource Y to be held by T2. Resource Z, which is held by T0, is waiting for T2 All the processes are therefore waiting for each other to release resources. None of the processes can finish their task in this situation. This situation is regarded as a deadlock.
For a system, deadlocks are not healthy. The transactions involved in the deadlock are either rolled back or restarted in the event that a system is stuck in a deadlock.
The DBMS actively inspects all the operations where transactions are about to be performed in order to prevent any deadlock situation in the system. The DBMS inspects the activities and evaluates whether a deadlock situation can be established. If it finds that there may be a deadlock situation, then it is never allowed to execute that transaction.
To predetermine a deadlock situation, deadlock prevention schemes use the time stamp ordering process of transactions.
Wait-Die Scheme
In this scheme, if a transaction requests to lock a resource (data item) that is already held by another transaction with a conflicting lock, then one of the two options may occur,
This scheme allows the older transaction to wait but kills the younger one.
Wound-Wait Scheme
In this scheme, if a transaction requests to lock a resource (data item) that is already held by some other transaction with a conflicting lock, one of the two options which occur.
This scheme allows the younger transaction to wait, but the older transaction requires the younger one to abort and release the item when an older transaction demands an item kept by a younger one.
In both instances, at a later stage, the transaction that enters the system is aborted.
Not always a practical approach is to abort a transaction. Instead, to detect any deadlock situation in advance, deadlock avoidance mechanisms can be used. Methods such as "wait-for graph" are available, but they are only appropriate for those systems where transactions are lightweight with fewer resource cases. In a bulky system, techniques of deadlock prevention may work well.
Wait-for Graph
This is a simple tool available for tracking if there could be a deadlock situation. A node is generated for each transaction that comes into the system. When a transaction Ti asks for a lock on an item, say X, which is kept by some other transaction Tj, a guided edge is created from Ti to Tj. The edge between them is dropped if Tj releases item X and Ti locks the data item.
For any transaction waiting for certain data items kept by others, the system retains this wait-for graph. If there is some cycle in the graph, the system keeps checking.
We can use any of the following two approaches here.