Deadlock Detection Program in C
What is Deadlock?
A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them.
Think of a situation where only one track and two trains are approaching each other. Once they are in front of each other, none of the trains can move. When multiple processes are waiting for resources owned by other methods, a similar situation arises in operating systems.
There is a deadlock in an operating system when a process or thread enters a waiting state because another waiting process is holding a system resource requested by another method.
Almost all locking-based applications have the potential to deadlock. The Berkeley DB Concurrent Data Store product, which assures deadlock-free operation at the expense of decreased concurrency, or situations where all the threads of control accessing the database are read-only, are the exceptions to this rule.
Deadlock-free data access patterns exist (for instance, when an application overwrites fixed-length records in an existing database), but they are incredibly uncommon.
The deadlock detector locates deadlocks by scanning the "waits-for" graph for a cycle. To be more exact, the deadlock detector scans the lock table and examines each lock object that is locked at the moment. Each object contains lockers now closed to it and a list of vaults awaiting a lock. A partial ordering is defined by the list of waiting lockers for each object. Every holding locker must release its lock before a waiting locker can go forward, so for a specific thing, every holding locker comes after every waiting locker. Conceptually, the partial orderings are topologically sorted once each object has been studied. If this topological sort reveals any cycles, the lockers composing the cycle are stuck in an impasse. The safe chosen for rejection is one of the others.
Conditions for Deadlock
- Mutual exclusion: Only one process may hold a resource at once. Put another way, if a process P1 uses a resource R at a specific moment, another process P2 cannot have the same resource R at that exact moment. The resource R is available to process P2, but process P1 cannot use it concurrently with process P2.
- Hold & Wait: A process might request other resources that are being held by another method while simultaneously having several resources of its own. For instance, a process P1 might be saving two resources, R1 and R2, and it might also be requesting some resource R3 that process P2 is now having.
- Preemption is not permitted: A resource cannot be forcibly removed from the process by another process. For instance, if process P1 utilizes resource R, process P2 cannot completely grab that resource. If that is the case, different scheduling algorithms are not necessary. Process P2 has can ask for the resource R and waits for the process P1 to release it.
- Circular Wait: A circular wait is a situation in which a first process is waiting for a resource held by a second process, a second process is waiting for a third process's help, and so on. The final process is now waiting for the resist process’s resources result; the method is releasing its resource; instead, everyone is waiting for the processor to remove techniques.
Deadlock Avoidance
The OS can avoid deadlock by using deadlock avoidance techniques. Before bee execution, the OS will track the maximum resources needed for a process during its entire life cycle. Before allocating any newly requested resource to any procedure, the OS will continuously check the system’s state.
In essence, the OS will strive to avoid entering a cyclic wait condition during the deadlock avoidance.
Suppose the system’s subsequent state results in a deadlock, the request for any resource will be granted in deadlock avoidance.
The process should specify the maximum number of resources of each type it may ever require, according to the simplest and most practical way. There can never be a cyclic wait condition thanks to the deadlock avoidance algorithm's examination of the resource allocations.
Deadlock Detection
With this approach, the OS anticipates a deadlock in the future. As a result, it periodically executes a deadlock detection algorithm, and upon detecting a dead draw initiates a recovery strategy.
The OS's primary responsibility is to find the deadlock. There are two ways to detect.
Deadlock detection can cause deadlocks. Then check the state of the system to see if a deadlock has occurred and fix it. Algorithms are used to track resource allocations and process state, roll back, restart one or more processes, and eliminate detected deadlocks. Because the operating system's resource scheduler knows what resources each process has locked or is currently requesting, it's easy to detect deadlocks that have already occurred.
FOR A SINGLE INSTANCE OF EVERY RESOURCE
The OS monitors the creation of a circle in the wait-for-graph approach. With certain variations, it is somewhat similar to the resource allocation graph (RAG). Most often, it confuses the Wait-for graph and RAG.
FOR MULTIPLE INSTANCES OF EVERY RESOURCE
We adopt the Safety algorithm, which follows the same methodology as the Banker's algorithm, for resources with many instances. However, there is no maximum necessary resource matrix. Allocated, available, and current requirement matrices are the only ones present.
DEADLOCK AVOIDANCE ALGORITHM (BANKERS ALGORITHM)
Steps of Algorithm
Step 1
- Let Work(vector) length = m
- Finish(vector) length = n
- Initialise Work= Available.
- if Allocationi = 0 ∀ i ∈[0,N-1], then Finish[i] = true;
otherwise, Finish[i]= false.
- if Allocationi = 0 ∀ i ∈[0,N-1], then Finish[i] = true;
Step 2
- Find an index i such that both
- Finish[i] == false
- Work >= Request i
If there exists no i, go to stepI4.
Step 3
- Work += Allocation i
- Finish[i] = true
Go to Step 2.
Step 4
- For some i in [0, N), if Finish[i]==false, then the system is considered deadlock Finish [i]==false the, process Pi is deadlocked.
DEADLOCK DETECTION PROGRAM
#include<stdio.h>
static int mark[20];
int i, j, np, nr;
int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
printf ("\nEnter the no of the process: ");
scanf("%d",&np);
printf ("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr; i++)
{
printf("\nTotal Amount of the Resource R % d: ",i+1);
scanf("%d, &r[i]);
}
printf("\nEnter the request matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
/*Available Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
//marking processes with zero allocation
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
// initialize W with avail
for(j=0;j<nr; j++)
w[j]=avail[j];
//mark processes with request less than or equal to W
for(i=0;i<np; i++)
{
int canbeprocessed= 0;
if(mark[i]!=1)
{
for(j=0;j<nr;j++)
{
if(request[i][j]<=w[j])
canbeprocessed=1;
else
{
canbeprocessed=0;
break;
}
}
if(canbeprocessed)
{
mark[i]=1;
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}
//checking for unmarked processes
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}
