Chaitu Tech Bits

What is deadlock? how to aviod deadlock ? how to prevent dead lock

Wednesday, December 15, 2010


 What is deadlock?

A: Deadlock is a situation when two or more processes are each waiting for each other to   release a resource

A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does. It is often seen in a paradox like the "chicken or the egg". The concept of a Catch 22 is similar.
When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.
— Illogical statute passed by the Kansas Legislature

Deadlock occurs when threads are unable to execute because they need more shared resources, and they are still holding shared resources while they wait.  If two or more of these threads are both doing this, they might each want 75% of system memory to do something, but each one can only get 50%.

Because they're waiting for more memory to be available, they are deadlocked.  Neither one will ever make any progress unless one is killed.

The solution is to give back shared resources when you don't have enough of them to execute.


Eliminate one of the four conditions:

            Mutual Exclusion:
            Well, if we need it then we need it.

            Hold and Wait:

Require a process to request and be allocated all its resources before it begins        
execution, or allow process to request resources only when the process has none.
May lead to low resource utilization. Starvation is a problem - a process may be held for a long time waiting for all its required resources. If it needs additional resources, it releases all of the currently held resources and then requests all of those it needs (the application needs to be aware of all of  the required resources).

No Preemption:
If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.
The state of preempted resources has to be saved and later restored. Not practical
for many types of resources (e.g. printer).

            Circular Wait:
            Impose a total ordering on all resource types.
            Require each process to request resources only in a strict increasing order.
            Resources from the same resource type have to be requested together.




Share/Bookmark

Related Posts Plugin for WordPress, Blogger...