Table of Contents
Deadlock is a situation that occurs when a process is requesting a resource that is occupied by another process in a computer or computer network. A deadlock has four common conditions i.e Mutual Exclusion, Hold and Wait, Non-Preemption, and Circular Wait.
In this article, I explain to you what is a deadlock? its characteristics or conditions and important methods that are used to avoid and prevent deadlocks. Here we go!
You May Also Like:
In a multiprogramming environment, several processes compete for resources. A situation may arise where a process is waiting for a resource that is held by other waiting processes. This situation is called a deadlock.
A system has a finite set of resources such as memory, I/O devices, Pointing Devices, etc. It also has a finite set of processes that need to use these resources. A process that wishes to use any of these resources, makes a request to that resource.
If the resource is free, the process gets it. If it is being used by another process, it waits for it to become free. The assumption is that the resource will eventually become free and the waiting process will then use the resource. But in some situations, the other process may also be waiting for some resource.
“A set of processes is in a deadlock state when every process in the set is waiting for an event that can only be caused by another process in the set.” Suppose we have two tape drives and two processes. Each process has got one tape drive for each process.
But both need two such tape drives to proceed with the execution. Each is waiting for the other process to release the other tape drive. This will never happen as the other is also waiting for the same thing. This situation is the deadlock.
The following are some examples of deadlock.
Example 1
The following figure is of a traffic deadlock. It makes the nature of deadlock very clear. None of the cars can pass unless at least one of them backs up or is removed.
Example 2
When two trains approach each other at a crossing, both shall come to a full stop and neither will start up again until the other has gone.
Example 3
Suppose we have a printer and a tape drive. Process A requests for the printer and gets it. Process B requests a tape drive and it is granted to it. Now A asks for the tape drive but the request is denied until B releases it. At this point, process B asks for the printer before releasing the tape drive.
Now both processes are waiting for each other to release the resource and are blocked. Both processes will remain in this situation forever. This situation is called a deadlock.
A resource is an object that is used by a process. It can be a piece of hardware such as:
A resource can be a piece of information such as:
A computer typically has many different resources. In some cases, there may be many instances of a resource of a given type. A process needing one of these resources can use any one of them. In other cases, there may be only one instance of a resource.
Different types of resources are as follows:
A preemptible resource is one that can be allocated to a given process for a period of time. Then it can be allocated to another process. Then it can be reallocated to the first process without any negative effects. Examples of preemptible resources include memory, buffers, CPU, and array processor.
Non-Preemptible resources cannot be taken from one process and given to another without side effects. One example is a printer. A printer cannot be taken away from one process and given to another process in the middle of a print job. Deadlocks usually involve non-preemptible resources.
The usual sequence of events that occur as a resource is used is as follows:
Deadlock occurs if the following four conditions take place simultaneously in a system:
At least one resource must be held in a non-sharable mode. It means that only one process at a time can use the resource. If another process requests the resource, the requesting process must be delayed until the resource has been released.
A process must be holding at least one resource and wait to acquire additional resources that are currently being held by other processes.
Resources cannot be preempted. A resource can be released only voluntarily by the process holding it after that process has completed its task.
A set {P0, P1, P2…, Pn} of waiting processes must exist such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …., Pn-1 is waiting for a resource that is held by Pn, and Pn waiting for a resource that is held by P0.
The resource allocation graph is used to describe a deadlock graphically. The graph has two different types of nodes i.e. process nodes and resource nodes. Processes are represented by circles and resources are represented by rectangles.
For each instance of a resource, there is a dot in the resource node rectangle. For example, if there are two identical printers, the printer resource will have two dots. The edges among nodes represent resource allocation and request.
If the edge goes from resource to process node, it indicates that the process has acquired the resource. If the edge goes from the process node to the resource node, it indicates that the process has requested the resource. We can use these graphs to determine if a deadlock has occurred or may occur.
For example, if all resources only have one instance (all resource node rectangles have one dot) and the graph is circular, a deadlock has occurred. If some resources have several instances, a deadlock may occur. If the graph is not circular, a deadlock cannot occur because the circular wait condition will not be satisfied.
Below is an example of a deadlock system. Process X has resource B and is waiting for resource A. Process Y has resource A and is waiting for resource B. Note there exists a cycle in the directed graph.
The diagram below is an example of a resource graph that has a cycle but does not represent a deadlocked system. It is possible for process Z to continue execution. When process Z is completed, it will release resource A.
It will allow process X to acquire it and continue processing. A cycle is a necessary, but not sufficient condition for deadlock. All deadlocked systems will exhibit all of the above four conditions. Not every system that exhibits the above four conditions will be deadlocked.
A deadlock can be handled in several ways:
We can prevent deadlock by ensuring that at least one of the four necessary conditions for deadlock cannot occur. If at least one condition is not satisfied, a deadlock will not occur.
Mutual exclusion conditions must hold for non-sharable resources. For example, only one process can have access to a printer at a time, otherwise, the output is disturbed. Some resources can be made shareable like a read-only file.
Several processes can be granted read-only access to a file without inferring from each other. However, deadlock cannot be prevented by only denying mutual exclusion conditions because some resources are intrinsically non-shareable.
Deadlock can be prevented by denying the hold and waiting for a precondition. This can be implemented in two different ways.
Problems With This Approach
Each of these strategies has some performance or resource utilization issues.
Preemption of resources means that we take away sources from processes when they are waiting for other resources. This could work in the following ways:
Problems With This Approach
We can prevent deadlock by making circular wait impossible. We can define an order by which the process is to get resources to prevent circular wait. For example, each resource type is assigned a number. The process can only get resources in increasing order of those resource numbers.
Suppose the tape drive has a number 1, the disk drive has a number 5, and the printer has a number 12. A process wants to read the disk drive and print out the results. It will first need to allocate disk drive then the printer. It will be prevented from going it in reverse order.
Problems With This Approach
Problems with this approach are as follows:
Having bad credit can make it challenging to obtain a personal loan, but it's not… Read More
Traveling doesn't have to break the bank. With some careful planning and smart strategies, you… Read More
Are you looking for a job in the fruit packing industry with the added benefit… Read More
Are you considering a move from the United States to Canada? Whether it's for a… Read More
A credit card is a financial tool that allows you to borrow money from a… Read More
Watching sports online for free can be challenging due to the licensing agreements and restrictions… Read More