Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

operating system

What is the difference between deadlock and starvation in OS?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Deadlock and starvation are two phenomena that take place inside the operating system.

Deadlocks

Deadlocks occur when a process needs a resource for the completion of its execution, but that resource is held by another process that waits for some other resource that it needs. This creates a cycle where resources are occupied, and each process lives in a waiting state.

Deadlocks occur when the following conditions hold:

Condition Description
Mutual Exclusion No simultaneous access to a shared resource.
Hold and Wait A process is holding a resource and will keep holding until it gets the resource that it is waiting for.
No Preemption In case a process is holding a resource, the resource cannot be taken away until the process voluntarily releases it.
Circular Wait Each process is waiting for a resource that is held by some other process.

The illustration below shows Process A and Process B in a deadlock:

A deadlock

Starvation

Starvation occurs when processes with high priority execute, whereas processes with low priority wait for an indefinite period of time. Resources are required to execute a process. These include CPU time, memory, and disk space. When a process with higher priority keeps executing, lower priority processes need to wait.

The problem of starvation can be solved using aging.

Aging is the process of increasing the priority of processes that wait for a longer time in the queue.

The illustration below shows lower priority processes starving:

Lower priority processes starve

Summary

We can summarize the differences between deadlocks and starvation in the table below:

Deadlock Starvation
Occurs when two or more processes need some resource held by the other process to complete the execution. Occurs when lower priority processes wait for the execution of a higher priority process to finish.
Also known as circular wait. Also known as a live lock.
Caused by mutual exclusion, hold and wait, no preemption, and circular wait. Caused by uneven priorities to processes.
Can be prevented by avoiding the conditions that lead to it. Can be prevented using aging.

RELATED TAGS

operating system

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring