Deadlock

A deadlock is a situation in computer science and concurrency theory where two or more processes or threads are unable to proceed with their execution because they are waiting for resources held by each other, causing a standstill. In a deadlock, none of the processes can make progress, leading to a system that is effectively frozen.

Let's illustrate a deadlock situation using two semaphores and two processes:

Scenario: Two Processes and Two Semaphores

Imagine we have two processes, Process A and Process B, and two semaphores, Semaphore X and Semaphore Y. These processes need access to two shared resources, Resource 1 and Resource 2, to complete their tasks. To avoid conflicts and ensure mutual exclusion, they use semaphores. Here's how a deadlock can occur:

  1. Process A starts executing and acquires Semaphore X to access Resource 1.

  2. Process B starts executing and acquires Semaphore Y to access Resource 2.

  3. Now, both processes need access to the other's resource to complete their tasks. Process A needs Resource 2 (guarded by Semaphore Y), and Process B needs Resource 1 (guarded by Semaphore X).

  4. Since neither process can proceed without the other releasing the necessary resource, they both enter a waiting state, where they are blocking for the resources held by the other process.

  5. As a result, both processes are stuck, unable to make progress, and this is a deadlock situation.

Real-World Examples to Motivate Students:

  1. Traffic Intersection Deadlock:

    • Imagine two roads intersecting with a four-way stop sign.

    • Cars from one road (Process A) arrive at the intersection and stop, expecting to cross.

    • Cars from the other road (Process B) also arrive and stop, expecting to cross.

    • Since both sets of cars cannot move without the other yielding, a deadlock occurs, and traffic comes to a standstill.

  2. Resource Allocation in a Bank:

    • Consider a bank with two customers (Process A and Process B) who need to access their safety deposit boxes.

    • The bank uses two keys (Semaphore X and Semaphore Y) to access the respective deposit boxes (Resource 1 and Resource 2).

    • If both customers arrive at the bank simultaneously and try to access their boxes using their keys, they may end up in a deadlock if they need each other's keys to unlock their deposit boxes. Neither customer can complete their transaction.

In both cases, it becomes evident how a lack of coordination and proper resource management can lead to a deadlock scenario.

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.