Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Deadlock in OS (Operating System) – Examples & Methods of Handling

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

Process Deadlock is a classical issue in operating systems, especially where there is process scheduling and concurrent user access to resources. It happens when several processes are stuck, with each process anxiously waiting for the release of a resource that only the process holding it can release. This deadlock can seriously impact the performance and reliability of the system, and hence, suitable strategies for detecting, preventing and handling the situation are needed. It occurs when a set of processes becomes permanently blocked because each process waits for a resource that another process holds, creating a cycle of dependency that no process can break on its own. The deadlock can severely affect the system’s performance and stability, requiring efficient detection, avoidance, and recovery strategies.

What is Deadlock in OS?

Deadlocks occur in an operating system when two or more processes cannot proceed because each is waiting for the other to release a resource. This condition creates a cycle of dependency, leading to a halt in the execution for the involved processes.

Examples of Deadlock

Deadlock

In the above figure, there are two processes and two resources. Process 1 holds “Resource 1” and needs “Resource 2” while Process 2 holds “Resource 2” and requires  “Resource 1”. This creates a deadlock situation because none of the two processes can be executed. Since the resources are non-shareable they can only be used by one process at  a time (Mutual Exclusion). Each process holds a resource and waits for the other process to release the resource it requires. None of the two processes release their resource before their execution, creating a circular wait when the four conditions are satisfied.

Necessary Conditions for Deadlock

There are four necessary conditions for a deadlock to arise, which are as follows.

 

  • Mutual Exclusion: The only process that can use a resource at any given time is when the resource is non-shareable.

 

  • Hold and Wait: A process holding at least one resource is waiting to acquire additional resources currently being held by another process. The processes will not release the existing resources while waiting for other resources, leading to a potential deadlock. For example, If a process has locked a file and is waiting for access to a pointer, while another process holds the printer and waits for the file, both processes could be stuck.

 

  • No Preemption: No preemption means a resource cannot be forcibly removed from a process. It must be released voluntarily by the process holding it. If a process holds a resource and another requires it, the second process must wait until the first one releases it.

 

  • Circular Wait: The end product is the circular wait condition in which the processes create a ring, and each process waits for the other to release a resource that the latter holds. For example, Process A is waiting to use a resource from Process B, Process B is waiting for a resource from Process C, and Process C is waiting for a resource from Process A.

 

Also Read: Deadlock Avoidance in OS

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Methods of Handling Deadlocks in Operating Systems

The operating system deadlocks are events whereby several processes are halted as they each wait for some resource that the other process owns. Dealing with deadlocks is important justifiably because of its effectiveness in managing system freezes functional and system crashes, among others. Deadlocks can be prevented, avoided, or detected and recovered The methods of preventing them range from the technical to the practical. It is now the right time to discuss the principal approaches to solving deadlocks in operating systems.

 

  • Deadlock Prevention: In this case, the aim is to prevent one of the required conditions from being fulfilled. This method can do this.

 

  • Mutual Exclusion: We only use the lock for non-shareable resources, and if the resource is shareable, we do not use locks here. It ensures that multiple processes can access shareable resources at the same time. The problem is that we can only do it for shareable resources, but in the case of non-shareable resources like printers, we have to use Mutual exclusion.

 

  • Hold and Wait: In this case, we ensure that hold and wait never occurs in the computer system, we must guarantee that whenever a process requests a resource it does not hold any other resources.

 

  • No Preemption: The process is holding some resources and requisitioning other resources that are acquired and these resources are not available immediately then the resources that the current process is holding are preempted.

 

  • Circular Wait: To remove the circular wait in the system, we can order the resources that a process needs to acquire.

 

  • Deadlock Avoidance: . Deadlock avoidance is an active technique used in the operating system in order to prevent the system from getting into unsafe conditions in which deadlocks are bound to happen. While deadlock prevention attempts to remove one of the conditions that are prerequisite for deadlock, deadlock avoidance directs decisions on resource allocation in such a manner that the system never enters a deadlock state.

Difference between Starvation and Deadlocks

 

Deadlock Starvation
A state where process cannot proceed because each is waiting for resources held by another A state  where a process is perpetually denied the resources it needs to execute due to unfair scheduling.
It occurs due to a circular wait condition among processes holding resources It is caused by priority scheduling or resource allocation policies that favor certain processes over others.
The deadlock processes are stuck and cannot change state. A starving process may be ready but is continuously overlooked for execution.
It requires mutual exclusion, hold and wait , no preemption,and circular wait. It does not necessarily require all four conditions, often resulting from priority schemes.

Also Read: Classification of Operating System

Conclusion

Deadlock is a critical issue in operating systems where a set of processes become stuck, each waiting for resources held by other processes, resulting in none of them progressing. It occurs due to four necessary conditions, mutual exclusion, hold and wait, no preemption, and circular wait. The challenge of handling deadlocks lies in preventing or resolving them without compromising system performance.

FAQs
A deadlock occurs when a set of processes are blocked because each process is waiting for a resource held by another process in the set. As A result, none of the processes can proceed, leading to a system halt or failure.
The banker’s algorithm avoids deadlocks by determining whether a system is in a safe state before allocating resources. It checks whether the available resources are sufficient to satisfy at least one process without leading to a deadlock, ensuring that the system stays in a safe state.
Deadlock detection and recovery require additional computational resources to monitor the system and recover from deadlocks. Recovery methods, such as terminating processes or rolling back actions, may result in data loss, decreased system performance, or a negative user experience.
The “Circular Wait” condition occurs when there is a set of processes,each waiting for a resource held by the next process in the chain, eventually forming a circular dependency. This condition leads to deadlock, as none of the processes  can proceed. Deadlock can be prevented by ensuring that resource requests are made in a strict order, breaking the circular chain.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved