Definition of a Deadlock
In computing, a deadlock is a state in which two processess are each waiting for the other to finish an action, e.g. for a resource to be freed up.
What is a Deadlock?
A deadlock is a condition in which two processes are each waiting for the other to give up a resource, thereby preventing both processes from continuing. The similarity between the word “dead” and the word “lock” is not coincidental.
The simplest form of a deadlock is when two processes each have a lock on a resource and each process is waiting for the other process to release its lock. If neither process ever releases its lock, then the two processes will be deadlocked forever.
In order for a deadlock to occur, four conditions must be met:
Mutual exclusion: Only one process can use a resource at any given time.
Hold and wait: A process can hold on to a resource while waiting to acquire another resource.
No preemption: A process cannot forcibly remove a resource from another process.
Circular wait: There must be a cycle; Processes must be waiting for resources that are held by other processes, which are themselves waiting for resources held by other processes.
What causes a Deadlock?
A deadlock is a situation where two or more threads are blocked forever, waiting for each other.
Thread 1 is waiting for a resource that thread 2 has. Thread 2 is waiting for a resource that thread 1 has. They’re both waiting for each other to release the resources they have!
This can happen when multiple threads try to lock the same resources in different orders. For example, imagine Thread 1 locks Resource A, then tries to lock Resource B. At the same time, Thread 2 locks Resource B, then tries to lock Resource A. Now both threads are blocked, because each one is holding on to a resource that the other thread wants!
The Four Conditions of a Deadlock
A deadlock is a condition where two processes are both waiting for each other to finish before they can continue. This can happen when each process has a lock on a resource that the other process needs. There are four conditions that must be met for a deadlock to occur:
In computer science, mutual exclusion is a property of concurrency control, which is instituted for the purpose of preventing race conditions; it is the requirement that one thread of execution never enter its critical section at the same time that another concurrent thread of execution enters its own critical section.
Hold and Wait
One process is holding a resource and waiting for another resource that is being held by another process If the second process is also waiting for a third resource, then a deadlock can occur.
A condition where a process cannot be stopped before it completes its execution is called No Preemption. A main example of preemptive resource scheduling is the CPU scheduling.
In computer science, preemption is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time.
A classic example of the Circular Wait condition is shown in Figure 1. There are four processes, P1 through P4, and four resources, A through D. Each process has been allocated one resource and is waiting to be allocated another resource that is being held by another process. In this example, P1 is waiting for a resource held by P2; P2 is waiting for a resource held by P3; P3 is waiting for a resource held by P4; and finally, P4 is waiting for a resource held by P1. Because each process is waiting for a resource that is being held by another process, the system is said to be in a deadlock.
How to Avoid Deadlocks
A deadlock is a condition in which two or more threads are blocked forever, waiting for each other. This can happen when each thread has a lock on a resource that the other thread needs. In order to avoid deadlocks, programmers can use lock hierarchies or lock ordering.
In order for a deadlock to occur, four conditions must be met. If any one of the conditions is not met, then a deadlock will not occur.
The four conditions that must be met are:
Condition 1: Mutual Exclusion
Condition 2: Hold and Wait
Condition 3: No Preemption
Condition 4: Circular Wait
A great way to avoid deadlocks is to use what is called an avoidance scheme. An avoidance scheme is a way of ensuring that the four conditions for a deadlock are never met. There are two main avoidance schemes:
1) Resource hierarchy
In computer science, a deadlock is a state in which two or more processes are blocked waiting for resources that will never be released by the other processes. In other words, each process is holding a resource that the other process needs, and neither process is willing to release its resources. Deadlocks can occur in multiple processes that are running on the same computer or in different computers across a network.