0% found this document useful (0 votes)
5 views5 pages

Deadlock Detection Algorithm Explained

The document explains a deadlock detection algorithm, detailing the initialization of processes, resources, and their allocations. It describes how to use arrays to track available resources, allocation, and requests, along with loops to determine if processes can finish without deadlock. The document provides examples illustrating scenarios with and without deadlock situations.

Uploaded by

Mohmad Raffat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Deadlock Detection Algorithm Explained

The document explains a deadlock detection algorithm, detailing the initialization of processes, resources, and their allocations. It describes how to use arrays to track available resources, allocation, and requests, along with loops to determine if processes can finish without deadlock. The document provides examples illustrating scenarios with and without deadlock situations.

Uploaded by

Mohmad Raffat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Deadlock Detection algorithm:

I will explain the code :

First:

Here we initialize the values where:

1- N is the number of processes


2- M is the number of resources
3- Available[M] is A vector of length m that indicates the number of available
resources of each type
4- Allocation[N][M] is an n x m matrix that defines the number of resources of each
type currently allocated to each thread.
5- Request[N][M] is an n x m matrix that indicates the current request of each thread
which If Request [i][j] = value, then thread Ti is requesting k more instances of
resource type Rj.

Second:
Here we initialize both Work and Finish, which help us to indicate if the process has
finished its processes or not :

1- The First loop is used to assign the value of available to work


Work[j]=Available[j]

2- The Second loop is used to initialize finished work with a false value that
indicates that no process is finished

3- The Third Loop is used to help us indicate if the available resource can
handle the need for each process for the process that is not finished:
From this function, it indicates that if the Need[i][j] is more than the
available resource Work[j], then it will break from the loop, but if not,
then it will continue without breaking until the value of j==M, then it will
break, and then go to the fourth loop
4- Then the fourth loop where available instances for each resource will
increase by adding the value of Allocated instances for each resource of
the process so Work[j]+=Allocation[i][j] and make the finish[i] = true
for the process that are handled to be not take in the next time and print
the value of the thread handled after that.
5- The final loop here will return false if any processes haven’t been
handled
Fourth:
And here is the example one :

we set the Allocation[i][j] and request[N][M] and it appears :


No dead block here
And we assign the second example as :
As we see here, there is a deadlock as it will be stuck due to P2 requesting
more instances, and this leads to a deadlock.
And here is the output:

You might also like