Deadlock detection program in C

In this article, we will discuss the deadlock detection program in C. But before discussing the discussion, we must know about the deadlock.

What is a Deadlock?

A deadlock occurs when a group is stuck in a process because everyone is hanging onto resources while waiting for other ways to get them.

Example:

Imagine just one track is available, and two trains are moving towards one another. None of the trains can move once positioned in front of one another. In operating systems, a deadlock occurs when multiple processes cannot proceed because each process waits for a resource acquired by another process. When a process or thread enters a waiting state because it has requested a system resource that is held by another process, and that other process is also waiting for a resource held by the first process.

A deadlock might occur in almost all locking-based programs. The exceptions to this principle are the Berkeley DB Concurrent Data Store product, which provides deadlock-free operations at the cost of lower concurrency, and scenarios in which all control streams accessing the relational database are read-only. There are deadlock-free data access patterns, although they are extremely rare (for example, when a program overwrites fixed-length entries in an existing database).

The deadlock detector searches the "waits-for" graph for cycles to identify deadlocks. To be more precise, the deadlock detector checks each lock object that is currently locked and traverses the lock table. Each item has a list of vaults awaiting a lock and lockers no longer accessible. The list of lockers that are still empty for each object establishes a partial ordering. Every holding locker comes behind every waiting locker because every holding locker must release its lock before the awaiting locker is allowed to move forward. Conceptually, the incomplete orderings are sorted topologically once each item is analyzed. The lockers that make up any cycles this topological sort reveals cannot proceed.

Conditions for Deadlock:

There are several conditions for deadlock. Some main deadlock conditions are as follows:

Mutual exclusion: A resource can only be held by one process at a time. In other words, if Process P1 uses a resource R at a certain time, Process P2 cannot simultaneously use an identical resource R. Process P1 cannot utilize the resource R simultaneously with Process P2, yet Process P2 has access to it.

Hold & Wait: "Hold & Wait" refers to a situation where a process holds resources and requests additional ones held by other processes. For instance, a process P1 may be demanding a resource R3 that the process P2 already has while preserving two resources, R1 and R2.

Preemption is not permitted: Another process cannot require a resource to leave a process. For instance, if resource R is used by process P1, process P2 cannot take it. If so, it is not required to use various scheduling methods. To obtain the resource R, process P2 must first wait for process P1 to stop using it.

Strategies to Prevent Deadlocks

The OS can use deadlock avoidance strategies to prevent deadlock. The OS will keep track of the maximum amount of resources required for a process throughout its life before it is executed. The OS will continually check the system's condition before providing any newly requested resources to any procedure.

During the deadlock avoidance process, the OS will avoid entering a cyclic wait situation. If the system enters a stalemate in the hypothetical future, the request for any resources will be met to prevent a deadlock.

According to the simplest and most useful method, the process should define the maximum amount of material of each kind it may ever use. The deadlock prevention method examines the resource allocations and ensures that there is never a cyclic wait state.

Detection of deadlocks:

The OS predicts a future deadlock with this plan of action. As a result, it runs a deadlock detection procedure periodically and performs a recovery plan when it detects a dead draw.

The OS's main function is to identify the deadlock.

There are two methods for finding.

Deadlocks can be created through deadlock detection. After that, assess the system's condition to see if a stalemate has occurred and resolve it. Tracking the allocation of resources and process status, rolling back, restarting one or more processes, and breaking discovered deadlocks are all done using algorithms. Identifying deadlocks is simple since the operating system's resource scheduler is aware of the resources that every process has locked or is actively seeking.

  1. For Each Resource in a Single Instance:
    In the wait-for-graph method, the OS focuses on forming a circle. It resembles the Resource allocation graph (RAG), with some differences. Most frequently, it combines RAG along with the Wait-for graph.
  2. Each Resource in Multiple Instance:
    For resources containing many instances, we use the Safety algorithm, which implements the same logic as the Banker's algorithm. There isn't a maximum required resource matrix, but there are just the allocated, available, and present requirements matrices.

Algorithm:

C program for implementation of the Deadlock detection Algorithm

Filename: Deadlock.c

Output:

Enter the number of processes: 3
Enter the number of resources: 3
Enter the total amount of Resource R1: 4
Enter the total amount of Resource R2: 5
Enter the total amount of Resource R3: 7
Enter the request matrix:
1 2 3
4 5 6
7 8 9
Enter the allocation matrix:
5 6 7 1 2 3 7 8 9
Deadlock detected