Table of contents
1.
Introduction
2.
Deadlock
3.
How to prevent it?
3.1.
Eliminate Mutual Exclusion 
3.2.
Eliminate Hold and wait 
3.3.
Eliminate No Preemption 
3.4.
Eliminate Circular Wait 
4.
How can we avoid it?
4.1.
Banker’s Algorithm
5.
Recovery from deadlock
6.
Frequently Asked Questions
6.1.
What is an OS?
6.2.
What is an operating system's main purpose?
6.3.
What does a real-time system entail? 
6.4.
What purpose does paging serve in an operating system?
6.5.
Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?
7.
Conclusion
Last Updated: Mar 27, 2024

Deadlock Prevention and Avoidance

Author Saksham Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

Operating System forms the backbone of any coding interview. Thus it's essential to have a good grasp of this topic. But don't you worry about any of it. Ninjas are here for you, and today we will discuss ‘Protection in Operating System.’ 

Also see: Multiprogramming vs Multitasking and Open Source Operating System

Deadlock

When a process or thread enters a waiting state in an operating system, it is called a deadlock because a requested system resource is being held by another waiting process, which is, in turn, waiting for another waiting process.

How to prevent it?

Eliminate Mutual Exclusion 

Because some resources, such as the tape drive and printer, are fundamentally non-shareable, it is impossible to satisfy the mutual exclusion.

Eliminate Hold and wait 

  • Allocate all essential resources to the process before it begins to run; this eliminates the hold and waits for conditions, but it results in low device usage. For instance, if a process requires printing at a later time and we have allocated a printer before the start of its execution, the printer will be blocked until the process is done.
  • After releasing the current set of resources, the process will issue a new resource request. This solution may result in hunger.

Eliminate No Preemption 

Here, we'll explore two approaches. If a process requests a resource already in use by another waiting resource, the resource may be taken away from the other waiting resource. If a process requests a resource that is not immediately available, all other resources it has been preempted in the second approach.

The difficulty is that resources can only be preempted if the current state can be saved, and processes can be restarted later from the saved state.

Eliminate Circular Wait 

A numerical number will be assigned to each resource. A process might request that the resources be increased or decreased.

For example, if P1 is given R5 resources, the following time P1 requests R4, and R3 resources are less than R5, the request will be denied; only requests for resources greater than R5 will be allowed.

How can we avoid it?

Banker’s Algorithm

Bankers' Method is a resource allocation and deadlock avoidance algorithm that examines all resource requests made by processes, checks for the safe state, and accepts the request if the system stays in the safe state after granting the request. If there is no safe state, the request is denied.

Banker's Algorithm Inputs:

  • Each process has a maximum resource need.
  • Currently, each process has its own set of resources.
  • The maximum number of free resources in the system.


The request will be granted only if the following conditions are met:

  • If the process's request is less than the maximum need for that process.
  • If the process's request is less than the freely accessible resource in the system.


If effective deadlock prevention and avoidance are not implemented, a stalemate may occur, with the only option being to detect and recover from the deadlock.

Recovery from deadlock

You'll have to break the deadlock after it's been detected. It can be done in various methods, including aborting one or more processes to break the deadlock-causing circular wait situation and preempting resources from one or more blocked processes.

Frequently Asked Questions

What is an OS?

An operating system is a software that allows computer hardware and software to connect and work together. It is the most crucial computer system component; without it, the computer would be nothing more than a box.

What is an operating system's main purpose?

An operating system serves two primary functions:

  1. Its goal is to keep a computer system running smoothly by monitoring and managing its computational activities.
  2. It provides a setting for program development and implementation.

What does a real-time system entail? 

When the operation of a processor is subjected to strict time constraints, a real-time system is utilized. It has well-defined and well-defined temporal limits.

What purpose does paging serve in an operating system?

In an operating system, paging is employed to solve the problem of external fragmentation. This method ensures that the information you require is provided as soon as feasible.

Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?

Yes, Coding Ninjas Studio lets you practice coding while also answering common interview questions. The more we practice, the more probable we will land a job at our ideal firm.

Conclusion

In this article, we have extensively discussed the ‘Deadlock Prevention and Avoidance

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass