In a multiprogramming system, multiple processes compete for limited resources. When a process requests resources that are unavailable at the time, it goes into a waiting state. Sometimes, a waiting process cannot change its state again because other processes hold the resources it needs, resulting in a deadlock. This tutorial will provide a brief overview of this issue about semaphores.

System Model

A system model or structure comprises a fixed number of resources that must be shared among different processes. These resources are divided into different types, each having a specific number of identical instances. Some examples of resource types are memory space, CPU cycles, directories and files, and I/O devices such as keyboards, printers, and CD/DVD drives. For instance, when a system has two CPUs, the resource type CPU has two instances.

Under the standard mode of operation, any process may use a resource in only the below-mentioned sequence:

  1. Request: When the request can't be approved immediately (where the case may be when another process is utilizing the resource), the requesting job must wait until it can obtain the resource.
  2. Use: The process can run on the resource (like when the resource is a printer, its job/process is to print on the printer).
  3. Release: The process releases the resource (like terminating or exiting any specific process).

Necessary Conditions and Preventions for Deadlock

A deadlock state can occur when the following four circumstances hold simultaneously within a system:

  • Mutual exclusion: At least one resource must be held non-sharable; i.e., only a single process at a time can utilize the resource. If another process demands that resource, the request must be postponed until the resource is released.
  • Hold and wait: A job must hold at least one resource and wait to obtain supplementary resources currently being held by several other processes.
  • No preemption: Resources can't be anticipated; i.e., a resource can get released only willingly by the process holding it, and then after that, the process has completed its task.
  • Circular wait: The circular-wait situation implies the hold-and-wait state or condition; hence, all four conditions are not entirely independent. They are interconnected among each other.

Methods for Handling Deadlocks

Usually, you can deal with the deadlock issues and situations in one of the three ways mentioned below:

  1. You can employ a protocol for preventing or avoiding deadlocks and ensure that the system will never go into a deadlock state.
  2. You can let the system enter any deadlock condition, detect it, and recover.
  3. You can overlook the issue and assume that deadlocks never occur within the system.

But it is recommended to deal with deadlock, from the 1st option.

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram