The 4 conditions for deadlocks

Deadlocks occur when four necessary conditions hold simultaneously. These conditions, known as the "Four Conditions for Deadlock," are:

  1. Mutual Exclusion:

    • This condition means that at least one resource must be non-sharable, meaning only one process can use it at a time.

    • Examples include printers, processors, or hardware devices that can only be used by one process at a time.

    • For instance, consider a printer that can print only one document at a time. If Process A is printing a document, Process B must wait until Process A completes its printing job.

  2. Hold and Wait:

    • This condition implies that processes hold resources while waiting for others.

    • A process that has already acquired some resources may request additional resources without releasing the ones it already holds.

    • For example, suppose Process A holds Resource 1 and needs Resource 2 while Process B holds Resource 2 and needs Resource 1. Both processes are waiting for the resource the other holds, leading to a deadlock.

  3. No Preemption:

    • Preemption means forcibly taking resources from a process.

    • This condition states that resources cannot be forcibly taken away from a process; they must be released voluntarily.

    • Consider a scenario where Process A holds Resource 1, and Process B needs it. If preemption were allowed, the system could forcibly take Resource 1 from Process A and give it to Process B to resolve the deadlock. However, the "no preemption" condition prevents this.

  4. Circular Wait:

    • This condition means there exists a circular chain of processes, each of which is waiting for a resource held by the next process in the chain.

    • For instance, if Process A is waiting for a resource held by Process B, Process B is waiting for a resource held by Process C, and so on, with Process Z waiting for a resource held by Process A, a circular wait exists.

    • Circular wait can be illustrated using a graph where processes are nodes, and edges represent resources. In a deadlock, this graph contains a cycle.

Detailed Examples:

Let's use a computer science example to illustrate these conditions:

Suppose we have two processes, Process A and Process B, and two resources, Resource 1 and Resource 2, which they need to complete their tasks.

  1. Mutual Exclusion: Consider a case where both Process A and Process B require exclusive access to a printer. Since a printer can only be used by one process at a time, this condition is met.

  2. Hold and Wait: Now, imagine that Process A has acquired the printer (Resource 1) but also needs access to a scanner (Resource 2). Instead of releasing the printer, it keeps it while waiting for the scanner to become available. Meanwhile, Process B needs the scanner but is holding onto the printer while waiting. Both processes are holding resources while waiting for others.

  3. No Preemption: In this scenario, we assume that the operating system does not allow preemption of resources. It cannot forcibly take the printer from Process A and give it to Process B without violating the "no preemption" condition.

  4. Circular Wait: If we represent the situation as a graph, we see that there is a circular chain. Process A is waiting for Resource 2 (held by Process B), and Process B is waiting for Resource 1 (held by Process A). This circular dependency fulfills the "circular wait" condition.

As all four conditions hold simultaneously in this example, a deadlock is possible. Both processes are stuck and cannot proceed, resulting in a deadlock situation. To resolve it, one or more of these conditions must be broken, such as allowing preemption or ensuring that processes release resources when they cannot get the ones they need.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.