Trusted answers to developer questions

What is deadlock prevention and avoidance?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

Deadlock characteristics

  1. Mutual Exclusion
  2. Hold and wait
  3. No preemption
  4. Circular wait

Deadlocks can be prevented by eliminating the aforementioned conditions.

Eliminating mutual exclusion

It is not possible to do this as some resources, such as the drivers and printers, are inherently non-shareable.

Eliminating hold and wait

Allocate all necessary resources to the process before it begins to run. This eliminates the hold and wait for conditions, but it results in low system utilization. For instance, if a process needs printing at a later time and we have allocated the printer before the start of its execution, the printer will be blocked until that process is completed.

This process will make new requests for a resource after releasing a set of resources, but this may lead to starvation.

Eliminating no preemption

Preempt resources from the process if they are required by another high-priority process.

Eliminate circular wait

A numerical number will be allocated to each resource. A process may request that the number of resources is increased or decreased. For example, if P1 is given R5 resources, the next time P1 requests R4, R3, or any other resource less than R5, the request will be denied. Only requests for resources greater than R5 will be granted.

Deadlock avoidance

A deadlock can be avoided using the Bankers’ Algorithm.

Bankers’ Algorithm:

The Bankers’ Algorithm is a resource allocation and deadlock avoidance algorithm that examines all resource requests made by systems. It checks for the safe state and makes the request if the system remains in the safe state after request approval. If there is no safe state, the request is denied.

Inputs required for the Bankers’ Algorithm:
  1. Maximum need or resources required by each process
  2. The resources currently allocated by each process
  3. Free resources available in the system
Request for the resource will only be granted:
  1. If the request made by the process is </= the freely available resource in the system
  2. If the request made by the process is </= maximum amount of resources required for the process

RELATED TAGS

os
Did you find this helpful?