What is a deadlock?

deadlock example
deadlock prevention
deadlock condition
how to avoid deadlock
characteristics of deadlock
deadlock avoidance
deadlock door
deadlock detection

When writing multi-threaded applications, one of the most common problems experienced are deadlocks.

My questions to the community are:

  1. What is a deadlock?

  2. How do you detect them?

  3. Do you handle them?

  4. And finally, how do you prevent them from occurring?

Deadlock, model, a process waits until it has received all the resources that it has requested. A deadlock is a situation faced by current operating systems in order to deal with multiple processes. This lesson will help you understand the reasons why deadlock happens, its prevention methods, and how to recover from a deadlock situation.

Let me explain a real world (not actually real) example for a deadlock situation from the crime movies. Imagine a criminal holds an hostage and against that, a cop also holds an hostage who is a friend of the criminal. In this case, criminal is not going to let the hostage go if cop won't let his friend to let go. Also the cop is not going to let the friend of criminal let go, unless the criminal releases the hostage. This is an endless untrustworthy situation, because both sides are insisting the first step from each other.

Criminal & Cop Scene

So simply, when two threads needs two different resources and each of them has the lock of the resource that the other need, it is a deadlock.

Another High Level Explanation of Deadlock : Broken Hearts

You are dating with a girl and one day after an argument, both sides are heart-broken to each other and waiting for an I-am-sorry-and-I-missed-you call. In this situation, both sides want to communicate each other if and only if one of them receives an I-am-sorry call from the other. Because that neither of each is going to start communication and waiting in a passive state, both will wait for the other to start communication which ends up in a deadlock situation.

What are the types of deadlocks?, A lock occurs when multiple processes try to access the same resource at the same time. One process loses out and must wait for the other to  Deadlock definition is - a state of inaction or neutralization resulting from the opposition of equally powerful uncompromising persons or factions : standstill. How to use deadlock in a sentence.

What is Deadlock and how to avoid it? - Saurav Singh, Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some​  In general computing, a deadlock is a situation where two different programs or processes depend on one another for completion, either because both are using the same resources or because of erroneous cues or other problems.

To define deadlock, first I would define process.

Process : As we know process is nothing but a program in execution.

Resource : To execute a program process needs some resources. Resource categories may include memory, printers, CPUs, open files, tape drives, CD-ROMS, etc.

Deadlock : Deadlock is a situation or condition when two or more processes are holding some resources and trying to acquire some more resources, and they can not release the resources until they finish there execution.

Deadlock condition or situation

In the above diagram there are two process P1 and p2 and there are two resources R1 and R2.

Resource R1 is allocated to process P1 and resource R2 is allocated to process p2. To complete execution of process P1 needs resource R2, so P1 request for R2, but R2 is already allocated to P2.

In the same way Process P2 to complete its execution needs R1, but R1 is already allocated to P1.

both the processes can not release their resource until and unless they complete their execution. So both are waiting for another resources and they will wait forever. So this is a DEADLOCK Condition.

In order for deadlock to occur, four conditions must be true.

  1. Mutual exclusion - Each resource is either currently allocated to exactly one process or it is available. (Two processes cannot simultaneously control the same resource or be in their critical section).
  2. Hold and Wait - processes currently holding resources can request new resources.
  3. No preemption - Once a process holds a resource, it cannot be taken away by another process or the kernel.
  4. Circular wait - Each process is waiting to obtain a resource which is held by another process.

and all these condition are satisfied in above diagram.

Deadlock, A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function. The earliest computer operating systems ran only one program at a time. In concurrent computing, a deadlock is a state in which each member of a group is waiting for another member, including itself, to take action, such as sending a message or more commonly releasing a lock. Deadlock is a common problem in multiprocessing systems, parallel computing, and distributed systems,

A deadlock happens when a thread is waiting for something that never occurs.

Typically, it happens when a thread is waiting on a mutex or semaphore that was never released by the previous owner.

It also frequently happens when you have a situation involving two threads and two locks like this:

Thread 1               Thread 2

Lock1->Lock();         Lock2->Lock();
WaitForLock2();        WaitForLock1();   <-- Oops!

You generally detect them because things that you expect to happen never do, or the application hangs entirely.

What is a deadlock?, A deadlock is a situation faced by current operating systems in order to deal with multiple processes. This lesson will help you understand the reasons why  Deadlock is a situation where two or more processes are waiting for each other. For example, let us assume, we have two processes P1 and P2. Now, process P1 is holding the resource R1 and is waiting for the resource R2. At the same time, the process P2 is having the resource R2 and is waiting for the resource R1.

What is deadlock?, This can lead to a situation called deadlock. A set of processes or threads is deadlocked when each process or thread is waiting for a resource to be freed which is  What is a Deadlock? Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource held by another process.

What is Deadlock? - Definition, Examples & Avoidance, Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource held by another process. Deadlocks in Operating Systems  Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. Consider an example when two trains are coming toward each other on same track and there is only one track, none of the trains can move once they are in front of each other.

Introduction to Deadlocks in Operating System, Deadlock is a situation that occurs in OS when any process enters a waiting state because another waiting process is holding the demanded  A deadlock is a state of a system in which no single process/thread is capable of executing an action. As mentioned by others, a deadlock is typically the result of a situation where each process/thread wishes to acquire a lock to a resource that is already locked by another (or even the same) process/thread.

Comments
  • You first, my dear.
  • I'm using process here as a generalisation, not specifically an OS Process. These could be threads, but could also be completely different applications, or database connections. The pattern is the same.
  • Hi, given this scenario: Thread A locks resource A and having a long process. Thread B waiting to lock resource A. CPU time usage : 20%, can you consider that a deadlock situation?
  • @rickyProgrammer no, that's just a regular lock wait, though the difference is a little academic. B waiting on slow A is a lock, B waiting for A waiting for B is a deadlock.
  • So deadlock is more of two processes with locked resources waiting for those resources to be released..
  • @rickyProgrammer it's a lock that won't become free, no matter how long you wait, because of the circular queue.
  • Shoudnt the threads belong to different processes?, can threads belonging to the same process also cause a deadlock?
  • @diabolicfreak It doesn't matter if the threads belong to the same process or not.
  • Another example from real life could be four cars coming to the crossing of two equal roads in four directions simultaneously. Every one needs to give a way to a car from the right-hand side, so no one can proceed.
  • The 3rd point to prevent a deadlock (always take the locks in the same order) is vital, which is rather easy to be forgotten in coding practice.
  • A deadlock happens when a thread is waiting for something that cannot occur.
  • I vote up to you. Your answer is more concise then above because they make confuse deadlock happen by process or thread. Some one say process, some one say thread :)
  • This describes a lock, not a deadlock.