What is Dead Lock and how to avoid

Dead Lock:

We generally use Mutex or Semaphore to avoid such things. But still we have the chance of getting deadlock condition in some cases. 


Two threads (X and Y) wants lock A & lock B both to finish the task. Thread X acquire lock A. Thread Y acquire lock B. Now Thread X is waiting for lock B and Thread Y is waiting for lock A. So these two threads are in deadlock condition or rise round condition. 

When & how dead-lock happens:

Deadlock situation can be well explained by Dinning philosophy given in Wiki. This is perhaps the best example I have seen ever. I would like to explain the same here. Even This philosophy you can find at many places where dead is discussed like stack overflow also. 

We have round table with 5 chairs. 5 people sitting on each chair. We have 5 dishes with noodles in a plate. Certainly we have placed 5 spoons right to each plate. So now if you consider you can see one spoon left to the plate and one spoon right to the plate. 5 people either think or eat the noodles. As noodles are difficult to eat with the single spoon it is required to have two spoons to eat it. If each person will take the left spoon first then all five people will wait for the right spoon. In such case, it will be the deal lock for them as all are waiting for the second spoon. 

How to avoid deal locks / Ground rules to avoid deadlocks:

• Don’t request another resource/lock while holding one resource/lock.

• Acquire the locks in proper sequence or order. 

Share on Google Plus

About Unknown

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment


Post a Comment

Related Posts Plugin for WordPress, Blogger...