Question:
What is a deadlock mean & how can i secure from it??
Shah Saud
2007-06-07 04:13:58 UTC
What is a deadlock mean & how can i secure from it??
Four answers:
Tom
2007-06-07 04:27:52 UTC
A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause (including itself).



Waiting for an event could be:



* waiting for access to a critical section

* waiting for a resource Note that it is usually a non-preemptable (resource). pre-emptable resources can be yanked away and given to another.



Conditions for Deadlock



* Mutual exclusion: resources cannot be shared.

* Hold and wait: processes request resources incrementally, and hold on to what they've got.

* No preemption: resources cannot be forcibly taken from processes.

* Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain.



Strategies for dealing with Deadlock



* ignore the problem altogether ie. ostrich algorithm it may occur very infrequently, cost of detection/prevention etc may not be worth it.

* detection and recovery

* avoidance by careful resource allocation

* prevention by structurally negating one of the four necessary conditions.



Deadlock Prevention



Difference from avoidance is that here, the system itself is build in such a way that there are no deadlocks.



Make sure atleast one of the 4 deadlock conditions is never satisfied.



This may however be even more conservative than deadlock avoidance strategy.



* Attacking Mutex condition

o never grant exclusive access. but this may not be possible for several resources.

* Attacking preemption

o not something you want to do.

* Attacking hold and wait condition

o make a process hold at the most 1 resource at a time.

o make all the requests at the beginning. All or nothing policy. If you feel, retry. eg. 2-phase locking

* Attacking circular wait

o Order all the resources.

o Make sure that the requests are issued in the correct order so that there are no cycles present in the resource graph.



Resources numbered 1 ... n. Resources can be requested only in increasing order. ie. you cannot request a resource whose no is less than any you may be holding.

Deadlock Avoidance



Avoid actions that may lead to a deadlock.



Think of it as a state machine moving from 1 state to another as each instruction is executed.

Safe State



Safe state is one where

o It is not a deadlocked state

o There is some sequence by which all requests can be satisfied.



To avoid deadlocks, we try to make only those transitions that will take you from one safe state to another. We avoid transitions to unsafe state (a state that is not deadlocked, and is not safe)



eg.

Total # of instances of resource = 12

(Max, Allocated, Still Needs)

P0 (10, 5, 5) P1 (4, 2, 2) P2 (9, 2, 7) Free = 3 - Safe

The sequence is a reducible sequence

the first state is safe.



What if P2 requests 1 more and is allocated 1 more instance?

- results in Unsafe state



So do not allow P2's request to be satisfied.





Banker's Algorithm for Deadlock Avoidance



When a request is made, check to see if after the request is satisfied, there is a (atleast one!) sequence of moves that can satisfy all the requests. ie. the new state is safe. If so, satisfy the request, else make the request wait.

How do you find if a state is safe



n process and m resources

Max[n * m]

Allocated[n * m]

Still_Needs[n * m]

Available[m]

Temp[m]

Done[n]



while () {

Temp[j]=Available[j] for all j

Find an i such that

a) Done[i] = False

b) Still_Needs[i,j] <= Temp[j]

if so {

Temp[j] += Allocated[i,j] for all j

Done[i] = TRUE}

}

else if Done[i] = TRUE for all i then state is safe

else state is unsafe

}



Detection and Recovery



Is there a deadlock currently?



One resource of each type (1 printer, 1 plotter, 1 terminal etc.)

o check if there is a cycle in the resource graph. for each node N in the graph do DFS (depth first search) of the graph with N as the root In the DFS if you come back to a node already traversed, then there is a cycle. }



Multiple resources of each type

o m resources, n processes

o Max resources in existence = [E1, E2, E3, .... Em]

o Current Allocation = C1-n,1-m

o Resources currently Available = [A1, A2, ... Am]

o Request matrix = R1-n,1-m

o Invariant = Sum(Cij) + Aj = Ej

o Define A <= B for 2 vectors, A and B, if Ai <= Bi for all i

o Overview of deadlock detection algorithm,



Check R matrix, and find a row i such at Ri < A.

If such a process is found, add Ci to A and remove process i from the system.

Keep doing this till either you have removed all processes, or you cannot remove any other process.

Whatever is remaining is deadlocked.





Basic idea, is that there is atleast 1 execution which will undeadlock the system

Recovery



o through preemption

o rollback

+ keep checkpointing periodically

+ when a deadlock is detected, see which resource is needed.

+ Take away the resource from the process currently having it.

+ Later on, you can restart this process from a check pointed state where it may need to reacquire the resource.

o killing processes

+ where possible, kill a process that can be rerun from the beginning without illeffects
manoj Ransing
2007-06-10 06:14:38 UTC
Deadlock Occurs when two or more separate processes compete for resources held by one another.

For example take two process P1,P2.

and two resourses R1,R2,R3.

P1 is using R1,R2 and P2 is using R1,R3 and

P1 is need of R3 to complete the process and

P2 is need of R2 to complete. So P1 ll compete to take R3 and P2 ll compete take R2.

But P1 ll leave R2 only on completion of it process and P2 ll leave R3 on completion of its process.

So there s lock of resources between P1 and P3.

Hence this is deadlock.



If you want to secure from the deadlock, there are several approaches. The simplest is when the process is starting check all the resources it needs and freeze those resources. But this approach is highly inactive. So what you can do is, prioritize the resources. The process will get any resource A if it already has acquired the lower prioritised resource B.

Or else you can go for deadlock detetecyion and recovery. Like banking algorithm.

Think of deadlock as the situation in Hindi movies. Raj loves Priya, Priya loves Rahul, Rahul loves SImran, SImran loves Karan, Karan loves Nisha, and Nisha loves Raj. At the end unless one of these dies, the situation doesn't resolve. Similarly in computer when the deadlock is occured, one of the process must die, to freeze the resources.
little sunbeam
2007-06-07 11:27:40 UTC
Deadlock is when two things want the same information. One has part of the information and the other has the other part and neither is going to release the information until it has the other half.

To prevent it you prevent others access to the information before starting to use any of it (place a lock on it). The you release the information after you have finished with it. Then other things can have their turn.
@nu 2
2007-06-07 11:29:30 UTC
if A is accessing one file or record. if it needs another resource which is accesing by B.

and B needs the resouce which is accessing by A.



that means...to complete the work A needs B.

and B needs A.

both are in waiting state without releasing the resource.

this is called dead lock.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...