HomeBlogWeb DevelopmentWhat is Deadlock in Operating System (OS)?

What is Deadlock in Operating System (OS)?

Published
05th Sep, 2023
Views
view count loader
Read it in
9 Mins
In this article
    What is Deadlock in Operating System (OS)?

    It is well-known that every process that is taking place on a system needs the assistance of some resource to get executed. The resource could be in the form of the system’s CPU or some plugged-in device such as the standard input or output device. And as soon as the process ends, the in-use resource is duly released. But when multiple processes start running simultaneously on a system, chances are that each of these processes has to compete for a resource. This competition gets culminated in what tech experts have termed the deadlock in OS.

    In this blog, we will analyze in detail what deadlock is in OS, get a clear picture with a deadlock example in OS, methods of dealing with it, and much more. So, hang tight and read till the end. Later, if you wish to learn more about deadlocks, you can do so with our lucrative Full-Stack Developer Bootcamp online.

    What is Deadlock in OS?

    When more than one process taking place in a system is prevented from getting executed, it is called a deadlock. It happens when a resource is being held up or occupied at the moment because some other processes are running in the background. A deadlock in OS makes it circle the drain since no process is executed.

    Example of Deadlock 

    Let’s understand what deadlock is in the operating system better with this example. Let’s assume that Process 1 and Process 2 are two processes. In the same vein, Resource 1 and Resource 2 are two resources. Supposedly, 

    • Process 1 is assigned to “Resource 1” but ends up waiting for “Resource 2”.
    • Process 2 needs “Resource 2” and waits for “Resource 1”.

    Example of Deadlock

    Source: Includehelp.com

    Subsequently, neither Process 1 nor Process 2 gets executed because the resources needed by them are held up. So, this was a deadlock example resulting in a potential deadlock in the operating system.

    Necessary Conditions for Deadlock in OS

    It isn’t necessary that a deadlock will happen whenever more than one process, along with more than one resource, is involved. This is because a deadlock is earmarked by four conditions that are to be fulfilled anyhow. These include

    1. Mutual Exclusion

    Mutually exclusive means that for every process, there is a designated resource that cannot be shared. Henceforth, no two processes can ask for the same resource.

    2. Hold and wait

    As the name suggests, this deadlock condition in OS requires a process to wait for an occupied resource. This condition cannot be truer. Deadlock example

    3. No preemption

    Only one process can be scheduled at a point in time. So, it is said that no two processes can be executed simultaneously. This implies that only when a process is completed, then only the allocated or occupied resource will be duly released.

    4. Circular wait

    Every set of processes waits in a cyclic manner. This keeps them waiting forever, and they never get executed. As a result, deadlocks are called “circular wait” since they get a process stuck in a circular fashion. Every set of processes waits in a cyclic manner. This keeps them waiting forever, and they never get executed. As a result, deadlocks are also called “circular wait” since they get a process stuck in a circular fashion.

    So, these were the four conditions of deadlock.

    Methods for Handling Deadlock in OS

    Are you brainstorming about the ways of handling a deadlock in an operating system? Well, it’s time to address your significant concern about handling deadlocks. These include

    1. Deadlock Detection and Recovery

    This method involves identifying the situation of deadlock and following some ways to recover your system from a deadlock. A user can simply abort all the processes that can lead to further potential deadlocks. It is recommended to abort one process at a time rather than ending all the processes simultaneously. Keep aborting the processes till the system returns back to normalcy from the situation of deadlock. Another way out could be of freeing the resources from allocation until the deadlock perishes. This is called “resource preemption”.

    2. Deadlock Prevention

    As the name goes, if a user prevents any one of the four conditions for a deadlock from taking place, a deadlock will be prevented.  

    3. Deadlock Avoidance

    Whenever a process asks for a resource to be allocated, an algorithm is exercised to examine whether this resource allocation is safe for the system or not. If it isn’t safe, then the request is denied. This very algorithm is known as the “deadlock avoidance” method. 

    4. Deadlock Ignorance

    UNIX, Windows, and some other operating systems often are oblivious to deadlocks and ignore the situation of a deadlock, whenever it arises. This approach is termed the “Ostrich algorithm”. So, whenever a deadlock occurs, you can simply reboot the PC, and the deadlock will get resolved in no time.

    Difference Between Starvation and Deadlock in the Operating System

    Some users often confuse deadlocks for starvation in OS. However, the two are as different as day and night. Very commonly asked questions “What is starvation in OS” and “what is a deadlock in ” are also answered below. The table below lucidly traces the difference between starvation and deadlock:

    BasisStarvationDeadlock
    DefinitionIt is a process wherein resource-allocation is never done to low-priority resources and subsequently, they are never executed.

    It is a situation where more than one process taking place in a system is prevented from getting executed because no resource is allocated to it.
    Resource allocation


    Here, resources are allocated to high-priority processes only.Resource allocation is not done.
    Ways to handle


    A way to handle starvation is via aging.


    By avoiding any of the four necessary conditions for deadlock.  


    ExecutionBecause of resource allocation, only high-priority resources are executed.

    No process ever gets executed.
    Other namesStarvation in OS is also termed as “lived lock”.Also termed as “circular wait”.

    Advantages of the Deadlock Method

    Since we are well-versed with what is a deadlock in OS now, let’s get through the following advantages of it:

    • Perfect for a single task: The deadlock method is suitable for performing single tasks. 
    • No preemptions: There is no need to block a process from accessing that resource and preempting it to some other process. 
    • Lucrative method: If your resource can get stored as well as restored quickly, then this is the method for you. 
    • No computations required: Sincere thanks to all the problem-solving done in the system design, you can now bid adieu to run-time algorithmic computations. Wondering the role system design has to play in this? You can learn all about it in this Web Designing and Development course online

    Disadvantages of the Deadlock Method

    While the pros of the deadlock method exist, so do the cons: 

    • Late initiation: One of the biggest flaws of the deadlock method is that it lags in process initiation. 
    • Forestall losses: There is no iota of doubt that the deadlock method has inherent forestalled losses. 
    • Preemptions are encountered frequently: This method cannot be deemed good since it results in taking away the resource from the process making the output fetched up till now totally inconsistent. 
    • No piecemeal resources: The deadlock method doesn’t approve any request asking for gradational resources. 
    • Unawareness about future needs: Usually, processes ain’t aware of their future resource requirements. 

    Looking to enhance your coding skills? Get a Python certification online and unlock endless possibilities! Dive into the world of programming with Python and become a coding maestro. Start your journey today!

    Conclusion

    Summing it up, 

    • Definition: When more than one process taking place in a system is prevented from getting executed, it is called a deadlock.  
    • Why is deadlock bothersome?: A deadlock makes the system circle the drain since no process is executed.  
    • Cause of deadlock operating systems: It happens when a resource is being held up or is occupied at the moment because of some other on-going processes. A deadlock satisfies four mandatory conditions of the circular set, hold and wait, no preemption, and most importantly, the processes being mutually exclusive. 
    • Methods for handling deadlocks involve deadlock detection and deadlock recovery in OS, deadlock ignorance, deadlock prevention, and lastly, deadlock avoidance.  
    • Deadlock and starvation in operating system are two different kettles of fish. Starvation is basically the postponement of a low-priority process by no allocation of resources. Subsequently, such processes are never executed. 
    • Advantages: The deadlock method comes with a variety of perks. This method is perfect for performing a single task, neither needing any preemptions nor any algorithmic computations and helps in lucratively storing and restoring a resource.  
    • Disadvantages: However, because of this very method, processes aren't aware of their future resource needs, they forestall losses, and a lot of preemptions occur, and apart from late process initiation, no additional resources are allocated. 

    That’s all about the deadlock happening in an operating system. For more such insightful information, stay tuned to our page. Get enrolled in KnowledgeHut’s Full-stack Developer Bootcamp online and learn loads on many such intuitive concepts. 

    Frequently Asked Questions (FAQs)

    1What causes deadlock in OS?

    A number of reasons are attributed to causing a deadlock. However, the top cause is the fulfillment of four conditions including the circular set, hold and wait, no preemption, and most importantly, the processes being mutually exclusive.

    2How many types of deadlocks are in an operating system?

    These include deadlock detection and recovery, deadlock ignorance, deadlock prevention, and lastly, deadlock avoidance. These are also the methods for handling deadlock in operating system.

    3What is SQL deadlock?

    A Structured Query Language deadlock or SQL deadlock is a situation wherein a resource requested by more than one process is blocked.

    Profile

    Abhresh Sugandhi

    Author

    Abhresh is specialized as a corporate trainer, He has a decade of experience in technical training blended with virtual webinars and instructor-led session created courses, tutorials, and articles for organizations. He is also the founder of Nikasio.com, which offers multiple services in technical training, project consulting, content development, etc.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon