Table of Contents
ToggleIntroduction
In this article we will talk about circular wait which is essential condition for deadlock to happen and also talk about various ways the circular wait condition can be prevented by. Deadlock is the condition when processes are blocked because each process is holding a resource and is also in waiting state for other process to release the resource. In simpler words when any process enters a waiting state because another waiting process is holding the demanded resource deadlock is said to occur.
To understand using real world example, consider one way bridge (resource) used by cars/traffic (process) coming from two different direction. The two cars seems to be in deadlock state until one car backs off(rollback).
The Circular Wait Condition Can Be Prevented By
- defining a linear ordering of resource types
- using thread
- using pipes
- all of the mentioned
Answer: Correct choice is (a) defining a linear ordering of resource types
Now let’s understand the above answer in detail. But first let’s understand what do we mean by circular wait.
Circular Wait
Circular wait is condition when two or more processes wait for resources in a circular order. It is one of the condition for deadlock.
For example, In the figure above, process P3 is requesting resource R1 which is blocked by P1, which in return waiting for R2 which is again blocked by P2. P2 is waiting for R3 to be released which is blocked by P3 creating a circular loop.
Preventing Circular Wait
The circular wait condition can be prevented by defining a linear ordering of resource types. This means if we number all resources, then and a process has to access the resource in increasing or decreasing order. In other words, in order to request resource Rk, a process must first release all Rj such that j>= k
In other words, let’s suppose process acquires resources in increasing order, with this scheme the process will only have access to the new additional resource which has a higher integer value than allocated resources. And if that resource has a lesser integer value, it must free the acquired resource before taking the new resource and vice-versa for decreasing order. In this way by defining linear ordering of resources the circular wait condition can be prevented by.
Let’s understand this using example how the circular wait condition can be prevented by defining a linear ordering of resource types.
Let’s suppose we have two processes P and Q acquiring resources Ri and Rj. For deadlock to occur, P need to hold Ri and request Rj , while Q holds Rj and requests Ri. And if we define linear ordering (increasing or decreasing) of resources, this implies that i < j (for P’s request order) and j < i (for Q’s request order), which is impossible thus preventing deadlock.
One big challenge in this algorithm is determining the integer value or ordering for all the different resources if there are too many resources. When the system is too complex involving too many processes and resources, determining the relative ordering or priorities of resources will be a tedious task.
Conclusion
In this article we discussed about one of the condition for deadlock to happen i.e. circular wait and also discussed how the circular wait condition can be prevented by defining a linear ordering of resource types. Checkout more about deadlock and related articles here
Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!