9 Deadlock
9 Deadlock
A deadlock is a condition that occurs when two or more different database tasks are waiting for each other and none
of the task is willing to give up the resources that other task needs.
It is an unwanted situation that may result when two or more transactions are each waiting for locks held by the
other to be released.
In deadlock situation, no task ever gets finished and is in waiting state forever.
Deadlocks are not good for the system.
Deadlock Conditions
Following are the deadlock conditions,
1. Mutual Exclusion
2. Hold and Wait
3. No Preemption
4. Circular Wait
In Mutual exclusion states that at least one resource cannot be used by more than one process at a time. The
resources cannot be shared between processes.
Hold and Wait states that a process is holding a resource, requesting for additional resources which are being held
by other processes in the system.
No Preemption states that a resource cannot be forcibly taken from a process. Only a process can release a resource
that is being held by it.
Circular Wait states that one process is waiting for a resource which is being held by second process and the
second process is waiting for the third process and so on and the last process is waiting for the first process. It makes
a circular chain of waiting.
1. Deadlock Prevention :
For large database, deadlock prevention method is suitable. A deadlock can be prevented if the resources are
allocated in such a way that deadlock never occur. The DBMS analyzes the operations whether they can create
deadlock situation or not, If they do, that transaction is never allowed to be executed.
There are two techniques used for deadlock prevention –
(i) Wait-Die :
In this scheme, if a transaction requests for a resource which is already held with a conflicting lock by
another transaction then the DBMS simply checks the timestamp of both transactions. It allows the older
transaction to wait until the resource is available for execution.
Suppose there are two transactions Ti and Tj and let TS(T) is a timestamp of any
transaction T. If T2 holds a lock by some other transaction and T1 is requesting for
resources held by T2 then the following actions are performed by DBMS :
Check if TS(Ti) < TS(Tj) - If Ti is the older transaction and Tj has held some resource,
then Ti is allowed to wait until the data-item is available for execution. That means if the
older transaction is waiting for a resource which is locked by the younger transaction,
then the older transaction is allowed to wait for resource until it is available.
Check if TS(Ti) < TS(Tj) - If Ti is older transaction and has held some resource and if
Tj is waiting for it, then Tj is killed and restarted later with the random delay but with the
same timestamp.
Timestamp is a way of assigning priorities to each transaction when it starts. If timestamp is lower then that
transaction has higher priority. That means oldest transaction has highest priority.
For example –
Let T1 is a transaction which requests the data item acquired by Transaction T2. Similarly T3 is a transaction
which requests the data item acquired by transaction T2.
Here TS(T1) i.e. Time stamp of T1 is less than TS(T3). In other words T1 is older than T3. Hence T1 is made to
wait while T3 is rolled back.
Suppose T1 needs a resource held by T2 and T3 also needs the resource held by T2, with TS(T1)=5, TS(T2)=8 and
TS(T3)=10, then T1 being older waits and T3 being younger dies. After the some delay, the younger transaction is
restarted but with the same timestamp.
This ultimately prevents a deadlock to occur.
To summarize
2. Deadlock Detection :
In deadlock detection mechanism, an algorithm that examines the state of the
system is invoked periodically to determine whether a deadlock has occurred or
not. If deadlock is occurrence is detected, then the system must try to recover from
it.
Deadlock detection is done using wait for graph method.
Step 2 :
We find the Read-Write pair from two different transactions reading from top to bottom. If such as pair is found
then we will add the edges between corresponding directions. For instance -
Step 3 :
As cycle is detected in the wait-for graph there is no need to further process. The deadlock is present in
this transaction scenario.
Deadlock Avoidance
Deadlock Avoidance helps in avoiding the rolling back conflicting transactions.
It is not good approach to abort a transaction when a deadlock occurs.
Rather deadlock avoidance should be used to detect any deadlock situation in advance.
Selection of victim
• Set of deadlocked transactions, must determine which transaction to roll back to break the
deadlock.
• Consider the factor minimum cost
Rollback
- once we decided that a particular transaction must be rolled back, must determine how far this
transaction should be rolled back
- Total rollback
- Partial rollback
Starvation
Ensure that a transaction can be picked as victim only a finite number of times.