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: