You are on page 1of 20

NITTE MEENAKSHI INSTITUTE OF TECHNOLOGY

(AN AUTONOMOUS INSTITUTION, AFFILIATED TO VISVESVARAYA TECHNOLOGICAL UNIVERSITY,

BELGAUM, APPROVED BY AICTE & GOVT.OF KARNATAKA )

COURSE PROJECT REPORT


ON

DEADLOCK DETECTION ALGORITHM


Submitted in partial fulfilment of the requirement for the award of Degree of
Bachelor of Engineering
in

Computer Science and Engineering


Submitted by:
SUPREETHA DEVAGIRI 1NT19CS196 SECOND YEAR 4TH SEM

Department of Computer Science and Engineering


2020-21

1|Page
Dept. of CSE, NMIT, 2020-21
Nitte Meenakshi Institute of Technology
(AN AUTONOMOUS INSTITUTION AFFILIATED TO VISVESVARAYA TECHNOLOGICAL UNIVERSITY,
BELGAUM)
PB No. 6429, Yelahanka, Bangalore 560-064, Karnataka
Telephone: 080- 22167800, 22167860
Fax: 080 – 22167805

Department of Computer Science and Engineering

CERTIFICATE

This is to certify that the Course Project titled “Deadlock Detection


Algorithm” is an authentic work carried out by Supreeetha
Devagiri(1NT19CS196) bonafide student of Nitte Meenakshi
Institute of Technology, Bangalore in partial fulfilment for the award
of the degree of Bachelor of Engineering in COMPUTER SCIENCE
AND ENGINEERING of Visvesvaraya Technological University,
Belagavi during the academic year 2020-2021.

Name Signature of the Faculty Name and Signature of the

Incharge HOD

2|Page
Dept. of CSE, NMIT, 2020-21
DECLARATION

We hereby declare that

(i) This Presentation does not contain text, graphics or


tables copied and pasted from the internet unless it is
specifically acknowledged and the source being detailed
detailed in the report and in the reference section

(ii) All corrections and suggestions indicated during the


internal presentation have been incorporated in the
report

(iii) Content of the report has been checked for the plagiarism
Requirement

NAME USN SIGNATURE


SUPREETHA 1NT19CS196
DEVAGIRI

DATE : 25-06-2021

3|Page
Dept. of CSE, NMIT, 2020-21
ACKNOWLEDGEMENT

The satisfaction and euphoria that accompany the successful


completion of any task would be incomplete without the mention of the
people who made it possible, whose constant guidance and
encouragement crowned our effort with success. we express our sincere
gratitude to our Principal Dr. H. C. Nagaraj, Nitte Meenakshi Institute
of Technology for providing facilities.

We wish to thank our HoD, Dr. Thippeswamy M.N for the excellent
environment created to further educational growth in our college. We
also thank him for the invaluable guidance provided which has helped
in the creation of a better technical report.

Thanks to our Subject Faculty. We also thank all our friends, teaching
and non-teaching staff at NMIT, Bangalore, for all the direct and
indirect help provided in the completion of the presentation.

NAME USN SIGNATURE


SUPREETHA 1NT19CS196
DEVAGIRI

DATE : 25-06-2021

4|Page
Dept. of CSE, NMIT, 2020-21
ABSTRACT
This report surveys research work performed in deadlock detection.
The last survey paper on this topic appeared in 1980; since that time a
large number of interesting algorithms have been described in the
literature. A new, more efficient scheme is the probe-based deadlock
detection strategy used by many of the new algorithms. This report will
concentrate on deadlock detection algorithms. Only detection of
deadlocks will be reviewed here.

The contents in this report describes the introduction to the design of


the algorithm and the working procedure of the code .

In systems where deadlock detection strategies are used, conflicts


resulting from requests for resources are handled by allowing the
requesting transactions to wait freely. As a result, deadlocks may arise
and therefore must be detected and resolved.

5|Page
Dept. of CSE, NMIT, 2020-21
INTRODUCTION
A Deadlock is a situation where multiple program/process are sharing
a common resource and are preventing each other from accessing the
resource and are waiting for each other to release the resource in order
to progress any further.
Deadlock detection is necessary as when a deadlock occurs it will
completely halt the ongoing process until the required resource is not
released or the program gets terminated.
Deadlock can occur if and only if all of these four conditions (Coffman
condition) are fulfilled:
1. Mutual Exclusion:
The resources involved must be unshareable; otherwise, the processes
would not be prevented from using the resource when necessary.
2. Hold and Wait:
Requesting process hold already, resources while waiting for requested
resources.
3. No Pre-emption:
Resources already allocated to a process cannot be preempted.
4. Circular Wait:
The processes in the system form a circular list or chain where each
process in the list is waiting for a resource held by the next process in
the list.
There are three common strategies to deal with deadlock:
Deadlock prevention, Deadlock avoidance, and Deadlock detection.
Most studies agreed that both deadlock prevention and deadlock
avoidance strategies are conservative and less feasible in handling the
deadlock in general, so the deadlock detection/resolution strategy is
widely accepted as an optimistic and feasible solution to the deadlock
problem.

6|Page
Dept. of CSE, NMIT, 2020-21
There are many deadlock detection algorithms introduced, but they
suffer from problems such as increasing number of processes affecting
the performance, long run time complexity, detecting false positives.
The reason to research newer algorithms for deadlock detection and
avoidance is to detect and avoid the deadlocks more quickly and in an
more optimized way with less false positives.
The probability of deadlocks depends on factors such as process mix,
resource request and release patterns, resource holding time, and the
average number of data objects held (locked) by processes.
The probability of deadlocks is difficult to analyze because deadlock
occurrence is highly sensitive to the timing and order in which resource
requests are made.
There is always a need of improvement in software algorithms because
we are processing more and more data.
Algorithm improvements are important even if its only a small
improvement.
Some of the algorithms that we present will use a wait-for graph. A
wait-for graph is a directed graph where each node represents a process.
Each of the directed arc from node Pi to Pj represents that the node Pi
is waiting for the node Pj. The order of execution depends on the
directed arc and the next node will only execute once the previous
process releases the resources.
The simplest example of deadlock is where process 1 has been
allocated non-shareable resources A, say, a tap drive, and process 2 has
be allocated non-sharable resource B, say, a printer.
Now, if it turns out that process 1 needs resource B (printer) to proceed
and process 2 needs resource A (the tape drive) to proceed and these
are the only two processes in the system, each is blocked the other and
all useful work in the system stops. This situation is termed as deadlock.
The system is in deadlock state because each process holds a resource

7|Page
Dept. of CSE, NMIT, 2020-21
being requested by the other process neither process is willing to
release the resource it holds.
Deadlock detection is a process of actually determining that a deadlock
exists and identifying the processes and resources involved in the
deadlock.
Deadlock detection helps if in a scenario of multi instance resources for
various processes are in deadlock or not, Incase of single resource
instance we can create Wait-for graph to check deadlock state.
Deadlock detection can easily happen through Resource Allocation
Graph. Resource Allocation Graph tells us the state of the system in
terms of process and resource, like how many resources are available ,
how many are allocated and what is the request of each process
everything can be represented in terms of the diagram.
Deadlock detection is a way to deal with deadlocks by testing for a
deadlock, if a deadlock the process will recover from it. A way to test
for deadlock is by doing a graph re-duction algorithm.

However, there are many types of detection algorithm, which are


Distributed, Centralized, and Hierarchical.
In Distributed deadlock schemes, no single site has all the information
relating to all transactions and resources involved in the graph.
Therefore, to detect deadlocks, information must be passed between
sites. A variant of this scheme, an edge chasing deadlock detection

8|Page
Dept. of CSE, NMIT, 2020-21
algorithm, involves sending information according to the structure of
the graph.
Centralized deadlock detection algorithms require that all information
represented by the graph be kept at the acting controller, which is
responsible for running the deadlock detection and resolution
algorithms.
Hierarchical deadlock schemes are based on providing several levels
of hierarchy, namely, local, regional and global. The hierarchy should
be established so that deadlocks can be detected by a site as close to the
sites involved in the deadlock as possible .

The algorithm employs several time varying data structures:


1. Available
Vector of length m
Indicates number of available resources of each type.
2. Allocation
Matrix of size n*m
A[i,j] indicates the number of j th resource type allocated to i th process.
3. Request
Matrix of size n*m
Indicates request of each process.
Request[i,j] tells number of instance Pi process is request of jth
resource type.

9|Page
Dept. of CSE, NMIT, 2020-21
FLOW DIAGRAM

10 | P a g e
Dept. of CSE, NMIT, 2020-21
ALGORITHM

1. Mark each process that has a row in the Allocation matrix of all
zeros.

2. Initialize a temporary vector W to equal the Available vector.

3. Find an index i such that process i is currently unmarked and the i


the row of Q is less than or equal to W . That is, Qik ≤ Wk, for 1 ≤ k ≤
m . If no such row is found, terminate the algorithm.

4. If such a row is found, mark process i and add the corresponding


row of the allocation matrix to W . That is, set Wk = Wk + Aik, for 1 ≤
k ≤ m . Return to step 3.

11 | P a g e
Dept. of CSE, NMIT, 2020-21
STEPS OF ALGORITHM

Step 1
Let Work(vector) length = m
Finish(vector) length = n
Initialize Work= Available.
For i=0, 1, …., n-1, if Allocation i = 0, then Finish[i] = true; otherwise,
Finish[i]= false.

Step 2
Find an index i such that both
Finish[i] == false
Request i <= Work
If no such i exists go to step 4.

Step 3
Work= Work+ Allocation i
Finish[i]= true
Go to Step 2.

Step 4
If Finish[i]== false for some i, 0<=i<n, then the system is in a
deadlocked state. Moreover, if Finish[i]==false the process Pi is
deadlocked.

12 | P a g e
Dept. of CSE, NMIT, 2020-21
CODE IMPLEMENTATION
#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 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++)

13 | P a g e
Dept. of CSE, NMIT, 2020-21
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)

14 | P a g e
Dept. of CSE, NMIT, 2020-21
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)

15 | P a g e
Dept. of CSE, NMIT, 2020-21
{
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");
}

16 | P a g e
Dept. of CSE, NMIT, 2020-21
ADVANTAGES AND DISADVANTAGES
ADVANTAGES:
• There is no central point of failure.
• A single node failure cannot cause a crash.
• There is no one site with heavy traffic due to the detection
algorithm.
• The algorithm is only initiated when process(es) feel there might
be a problem.
• The algorithm is not run periodically, only when needed.
• Its permits the use of relatively simple algorithms.
• Once a cycle is formed in the state graph, it persists until it is
detected and broken.
• Cycle detection can proceed concurrently with the normal
activities of a system.
DISADVANTAGES:
• There is one, single point of failure.
• There can be a communication bottleneck around the site due to
all the WFG information messages.
• Furthermore, this traffic is independent of the formation of any
deadlock.
• The Resolution may be difficult, as not all sites may be aware of
the processes involved in the deadlock.
• The proof of correctness for this type of algorithm may be
difficult.
• Additional overhead incurred due to detection of cycles in the
graph and abortion and restart of transactions upon detection of
deadlocks.
• Selection of the transaction to be aborted adds to the complexity
of the scheme.
• In some algorithms based on this strategy, a situation may arise
wherein more than one site detects the same deadlock This
complicates the deadlock resolution phase.
17 | P a g e
Dept. of CSE, NMIT, 2020-21
APPLICATIONS
Deadlocks are a commonly used in :-
• Multiprocessing systems
Multiprocessor is a system with two or more central processing units
(CPUs) that is capable of performing multiple tasks where as a
multicomputer is a system with multiple processors that are attached
via an interconnection network to perform a computation task. There
are mainly two types of multiprocessors :
1. Symmetric and
2. Asymmetric multiprocessors.

• Parallel computing
Parallel computing refers to the process of breaking down larger
problems into smaller, independent, often similar parts that can be
executed simultaneously by multiple processors communicating via
shared memory, the results of which are combined upon completion
as part of an overall algorithm.
Some examples of parallel computing include weather forecasting,
movie special effects, and desktop computer applications

• Distributed systems
A distributed operating system is system software over a collection of
independent, networked, communicating, and physically separate
computational nodes. They handle jobs which are serviced by
multiple CPUs. Each individual node holds a specific software subset
of the global aggregate operating system.
• The Software and Hardware locks are used to arbitrate shared
resources
• It is used to Implement process synchronization.

18 | P a g e
Dept. of CSE, NMIT, 2020-21
OUTPUT OF THE IMPLEMENTED CODE

19 | P a g e
Dept. of CSE, NMIT, 2020-21
CONCLUSION

• Most current operating system ignore deadlocks. It is left to the


application developer to handle.
• There is not one approach that is sufficient.
• The eventual solution is to combine the three basic approaches
• Different resource classes would employ a selected solution

BIBLOGRAPHY

• Abraham Silberschatz, Peter Baer Galvin, Greg Gagne,


Operating System Principles 7th edition, Wiley
• Geeks for Geeks
• Tutorials point

20 | P a g e
Dept. of CSE, NMIT, 2020-21

You might also like