0% found this document useful (0 votes)
20 views4 pages

9 Deadlock

A deadlock occurs when two or more database tasks are waiting for each other, leading to a situation where none can proceed. Deadlocks can be prevented using techniques like Wait-Die and Wound-Wait, or handled through detection and recovery methods such as wait-for graphs. Recovery typically involves rolling back one or more transactions to resolve the deadlock.

Uploaded by

sahanaananthi18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

9 Deadlock

A deadlock occurs when two or more database tasks are waiting for each other, leading to a situation where none can proceed. Deadlocks can be prevented using techniques like Wait-Die and Wound-Wait, or handled through detection and recovery methods such as wait-for graphs. Recovery typically involves rolling back one or more transactions to resolve the deadlock.

Uploaded by

sahanaananthi18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

In the above diagram,


 Process P1 holds Resource R2 and waits for resource R1, while Process P2 holds resource R1 and waits for
Resource R2. So, the above process is in deadlock state.
 There is the only way to break a deadlock, is to abort one or more transactions. Once, a transaction is aborted and
rolled back, all the locks held by that transaction are released and can continue their execution. So, the DBMS
should automatically restart the aborted transactions.

Deadlock Conditions
Following are the deadlock conditions,
1. Mutual Exclusion
2. Hold and Wait
3. No Preemption
4. Circular Wait

A deadlock may occur, if all the above conditions hold true.

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.

Deadlock can be handled using two techniques –

1. Deadlock Prevention 2. Deadlock Detection and deadlock recovery

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.

(ii) Wound- wait :


 In wound wait scheme, if the older transaction requests for a resource which is held by the younger
transaction, then older transaction forces younger one to kill the transaction and release the resource. After
some delay, the younger transaction is restarted but with the same timestamp.
 If the older transaction has held a resource which is requested by the Younger transaction, then the younger
transaction is asked to wait until older releases it.

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.

Wait For Graph:


 In this method, a graph is created based on the transaction and their lock. If the created graph has a
cycle or closed loop, then there is a deadlock.
 The wait for the graph is maintained by the system for every transaction which is waiting for some data
held by the others. The system keeps checking the graph if there is any cycle in the graph.
 This graph consists of a pair G = (V, E), where V is a set of vertices and E is a set of edges.
 The set of vertices consists of all the transactions in the system.
 When transaction Ti requests a data item currently being held by transaction Tj , then the edge Ti → Tj is
inserted in the wait-for graph. This edge is removed only when transaction Tj is no longer holding a data
item needed by transaction Ti
 For example – Consider following transactions, We will draw a wait for graph
for this scenario and check for deadlock.

We will use three rules for designing the wait-for graph-


Rule 1: If T1 has Read operation and then T2 has Write operation then draw an edge
T1->T2.
Rule 2: If T1 has Write operation and then T2 has Read operation then draw an edge
T1->T2
Rule 3: If T1 has Write operation and then T2 has Write operation then draw an edge
T1->T2

Let us draw wait-for graph

Step 1 : Draw vertices for all the transactions

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.

Recovery from deadlock


- The common solution is to roll back one or more transactions to break the deadlock.
- Three action need to be taken
– Selection of victim
– Rollback
– Starvation

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.

You might also like