Professional Documents
Culture Documents
ON
“SIMULATE ALGORITHM FOR DEADLOCK
PREVENTION”
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE
Submitted by
SHAMANTH B M-[ENG16CS0146]
SHIVANI R-[ENG16CS0154]
SHREYA S-[ENG16CS0156]
SANJU M-[ENG16CS0139]
V Semester, 2018
Under the supervision of
Ms. Sharvari J N,
assistant professor, Department of CSE,
Dayananda Sagar University.
______________________________________
I hereby declare that the work presented in this mini project entitled
“SIMULATE ALGORITHM FOR DEADLOCK PREVENTION”
submitted to DAYANANDA SAGAR UNIVERSITY, Bangalore is a record
of group work implemented under the guidance of Ms. Sharvari J N, assistant
professor, Department of CSE, Dayananda Sagar University.
This project is submitted in the partial fulfilment of the requirements and
conductive criticism for the award of V semester mini-project in Operating
System. The results embodied in this project have not been submitted elsewhere
for the same.
ACKNOWLEDGEMENT
The satisfaction that accompanies the successful completion of any task would
be the complete without the mention of people who made it possible and whose
constant guidance and encouragement crown all the efforts with success.
We are very much thankful to Prof. Sharvari J.N, for the help and suggestion
in carrying out with this mini project completed successfully.
We have received a great deal of guidance and co-operation from our friends
and we wish to thank one and all who have directly or indirectly helped us in the
successful completion of this project work.
TABLE CONTENTS
• COVER PAGE
• CERTIFICATE
• DECLARATION
• ACKNOWLEDGEMENT
• CONTENS
• ABSTRACT
➢ INTRODUCTION
➢ PROBLEM STATEMENT
➢ LITERATURE REVIEW
➢ DESIGN
❖ FLOW CHART
➢ IMPLEMENTATION
❖ ALGORITHM
❖ CODING
➢ TESTING
➢ OUTPUT SCREEN
➢ CONCLUSION
➢ REFERENCE
ABSTRACT
Deadlock prevention algorithms are used in concurrent programming when
multiple processes must acquire more than one shared resource. If two or more
concurrent processes obtain multiple resources
indiscriminately, a situation can occur where each process has a
resource needed by another process. As a result, none of the processes can obtain
all the resources it needs, so all processes are blocked from further execution.
This situation is called a Deadlock.
Deadlock is one of the canonical problems in distributed systems. It arises
naturally in distributed systems such as distributed databases, distributed
operating systems and store and forward networks. In these systems, the data
reside at multiple distributed locations, and the processes have to collaborate in
order to perform a task. Deadlock is characterized by a set of processes that are
waiting for resources held by other processes in the set indefinitely. It reduces the
throughput and resource availability in distributed systems. It can be handled
through three ways, namely: deadlock prevention, deadlock avoidance and
deadlock detection. The suitability of a specific deadlock handling approach
highly depends on the application and environment. Deadlock prevention and
avoidance are impractical or inefficient and relatively expensive as compared to
deadlock detection approach in distributed systems. A deadlock prevention
algorithm organizes resource usage by each process to ensure that at least one
process is always able to get all the resources it needs.
Deadlock is characterized by a set of processes that are waiting for resources held
by other processes in the set indefinitely.
INTRODUCTION
LITERATURE REVIEW
As we have defined earlier that it is divided into two groups; the priority based
and nonpriority based algorithm[Lejeune,2012]. Now, thepriority based algorith
ms are further classified into two kinds, static prioritygroup [Ahmad, 1991] and
dynamic priority group [Ramos, 2012]. In static priority group the requests (Ri)
maintains their exact priority until its entrance into the critical section (CS)
which induces the strict compliance with the priorities but also violates the
aliveness property [Paydar, 2006]if another request of higher priority
requests for entrance into the CS, hence resulting in a stagnation of the lower
priority requests for much longer time creating starvation. On the other hand, in
the second group algorithm the request priority gets incremented for every
time whenever arequest ofhigher priorityisgranted by the system [Muller, 1998].
This mechanism retains the livenessproperty of the system but degrades the prio
rity order by creating a situation called as priority inversions
OBJECTIVES OF DEADLOCK
SYSTEM MODEL
REQUEST: The process requests the resource. If the request cannot be granted
immediately (for example, if the resource is being used by another process), then
the requesting process must wait until it can acquire the resource.
USE: The process can operate on the resource (for example, if the resource is a
printer, the process can print on the printer).
RELEASE: The process releases the resource.
The request and release of resources are system calls. Examples are the request()
and release() device, open() and close() file, and allocate() and free() memory
system calls. Request and release of resources that are not managed by the
operating system can be accomplished through the wait() and signal() operations
on semaphores or through acquisition and release of a mutex lock. For each use
of a kernel managed resource by a process or thread, the operating system checks
to make sure that the process has requested and has been allocated the resource.
A system table records whether each resource is free or allocated; for each
resource that is allocated, the table also records the process to which it is
allocated. If a process requests a resource that is currently allocated to another
process, it can be added to a queue of processes
waiting for this resource. A set of processes is in a deadlocked state when every
process in the set is waiting for an event that can be caused only by another
process in the set. The events with which we are mainly concerned here are
resource acquisition and release. The resources may be either physical resources
(for example, printers, tape drives, memory space, and CPU cycles) or logical
resources (for example, files, semaphores, and monitors). However, other types
of events may result in deadlocks.
To illustrate a deadlocked state, consider a system with three CD RW drives.
Suppose each of three processes holds one of these CD RW drives. If each process
now requests another drive, the three processes will be in a deadlocked state. Each
is waiting for the event "CD RW is released," which can be caused only by one
of the other waiting processes. This example illustrates a deadlock involving the
same resource type.
Deadlocks may also involve different resource types. For example, consider a
system with one printer and one DVD drive. Suppose that process P is holding
the DVD and process Pi is holding the printer. If P requests the printer and P1
requests the DVD drive, a deadlock occurs. A programmer who is developing
multithreaded applications must pay particular attention to this problem.
Multithreaded programs are good candidates for deadlock because multiple
threads can compete for shared resources.
Deadlock can occur in a multithreaded Pthread program using mutex locks. The
pthread mutex_init() function initializes an unlocked mutex.Mutex locks are
acquired and released using pthread_mutex_lock() and pthread_mutex_unlock (),
respectively. If a thread attempts to acquire a locked mutex, the call to
pthread_mutex_lock() blocks the thread until the owner of the mutex lock invokes
pthread_mutex_unlock().
Two mutex locks are created in the following code example:
/* Create and initialize the mutex locks */
Pthread_mutex_t first_mutex;
Pthread_mutex_t second_mutex;
Pthread_mutex_init(&first_mutex,NULL);
Pthread_mutex_ini t (&second_mutex, NULL) ;
Next, two threads-thread_one and thread_two are created,and both these threads
have access to both mutex locks.thread_one and thread_ two run in the functions
do_work_one () and do_work_two (), respectively
pthread_exit ( 0) ;
}
pthread_exi t ( 0) ;
}
In this example, thread_one attempts to acquire the mutex locks in the order
first_mutex,second_mutex,while thread_two attempts to acquire the mutex locks
in the order second_mutex,first_mutex.
Deadlock is possible if thread_one acquires first_mutex while thread_two
acquires second_mutex. Note that, even though deadlock is possible, it will not
occur if thread_one is able to acquire and release the mutex locks for first_mutex
and second_mutex before thread_two attempts to acquire the locks. This example
illustrates a problem with handling deadlocks: it is difficult to identify and test
for deadlocks that may occur only under certain circumstances .
RESOURCE ALLOCATION GRAPH:
Process states:
❖ Process P1 is holding an instance of resource type R2 and is waiting for
an instance of resource type R1 .
❖ Process P2 is holding an instance of R1 and an instance of R2 and is
waiting for an instance of R3.
❖ Process P3 is holding an instance of R3.
P1->R1->P2->R3->P3->R2->P1
P2->R3->P3->R2->P2
Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the resource
R3, which is held by process P3. Process P3 is waiting for either process P1 or
process P2 to release resource R2. In addition, process P1 is waiting for process
P2 to release resource R1.
Now consider the resource-allocation graph in Figure below. In this example, we
also have a cycle:
P1->R1->P3->R2->P1
However, there is no deadlock. Observe that process P4 may release its instance
of resource type R2. That resource can then be allocated to P3, breaking the cycle.
In summary, if a resource-allocation graph does not have a cycle, then the system
is not in a deadlocked state. If there is a cycle, then the system may or may not
be in a deadlocked state. This observation is important when we deal with the
deadlock problem.
Resource-allocation graph with a cycle but no deadlock.
As we know for a deadlock to occur, each of the four necessary conditions must
hold. By ensuring that at least one of these conditions cannot hold, we can prevent
the occurrence of a deadlock. We elaborate on this approach by examining each
of the four necessary conditions separately.
SAFE STATE:
A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock. More formally, a system is
in a safe state only if there exists a safe sequence. A sequence of processes is a
safe sequence for the current allocation state if, for each Pi, the resource requests
that Pi can still make can be satisfied by the currently available resources plus the
resources held by all Pj, with j < i. In this situation, if the resources that Pi needs
are not immediately available, then Pi can wait until all Pj have finished. When
they have finished, Pi can obtain all of its needed resources, complete its
designated task, return its allocated resources, and terminate. When Pi terminates,
Pi+l can obtain its needed resources, and so on. If no such sequence exists, then
the system state is said to be unsafe.
A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe
state. Not all unsafe states are deadlocks.However,an unsafe state may lead to a
deadlock. As long as the state is safe, the operating system can avoid unsafe (and
deadlocked) states. In an unsafe state, the operating system cannot prevent
processes from requesting resources in such a way that a deadlock occurs. The
behaviour of the processes controls unsafe states. Figure below shows Safe,
unsafe, and deadlocked state spaces.
Safe, unsafe, and deadlocked state spaces.
To illustrate, we consider a system with twelve magnetic tape drives and three
processes: Po, P1, and P2. Process Po requires ten tape drives, process P1 may
need as many as four tape drives, and process P2 may need up to nine tape drives.
Suppose that, at time to, process Po is holding five tape drives, process P1 is
holding two tape drives, and process P2 is holding two tape drives. (Thus, there
are three free tape drives.)
P0 10 5
P1 4 2
P2 9 2
At time t0, the system is in a safe state. The sequence <P1,P0,P2> satisfies the
safety condition. Process P1 can immediately be allocated all its tape drives and
then return them (the system will then have five available tape drives); then
process Po can get all its tape drives and return them (the system will then have
ten available tape drives); and finally process P2 can get all its tape drives and
return them (the system will then have all twelve tape drives available).
A system can go from a safe state to an unsafe state. Suppose that, at time t1,
process P2 requests and is allocated one more tape drive. The system is no longer
in a safe state. At this point, only process P1 can be allocated all its tape drives.
When it returns them, the system will have only four available tape drives. Since
process Po is allocated five tape drives but has a maximum of ten, it may request
five more tape drives. If it does so, it will have to wait, because they are
unavailable. Similarly,process P2 may request six additional tape drives and have
to wait, resulting in a deadlock. Our mistake was in granting the request from
process P2 for one more tape drive. If we had made P2 wait until either of the
other processes had finished and released its resources, then we could have
avoided the deadlock.
Given the concept of a safe state, we can define avoidance algorithms that ensure
that the system will never deadlock. The idea is simply to ensure that the system
will always remain in a safe state. Initially, the system is in a safe state. Whenever
a process requests a resource that is currently available, the system must decide
whether the resource can be allocated immediately or whether the process must
wait. The request is granted only if the allocation leaves the system in a safe state.
In this scheme, if a process requests a resource that is currently available, it may
still have to wait. Thus, resource utilization may be lower than it would otherwise
be.
BANKER’S ALGORITHM
Software requirements:
Hardware requirements:
FLOW CHART:
IMPLEMENTATION
ALGORITHM
CODE
#include<stdio.h>
#include<conio.h>
int max[10][10], alloc[10][10], need[10][10];
int avail[10],i,j,p,r,finish[10]={0},flag=0;
int main()
{
clrscr( );
printf("\n\nSIMULATION OF DEADLOCK PREVENTION");
printf("Enter no. of processes, resources");
scanf("%d%d",&p,&r);printf("Enter allocation matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter max matrix");
for(i=0;i<p;i++) /*reading the maximum matrix and availale matrix*/
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("enter available matrix");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun(); /*calling function*/
if(flag==0)
{
if(finish[i]!=1)
{
printf("\n\n Failing :Mutual exclusion");
for(j=0;j<r;j++)
{ /*checking for mutual exclusion*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}fun();
printf("\n By allocating required resources to process %d dead lock is prevented
",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("\n\n daed lock is prevented by allocating needed resources");
printf(" \n \n failing:Hold and Wait condition ");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun( );
printf("\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT
DEADLOCK");
}
}
getch( ); return 0;
}
fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else
break;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}return 0;
}
OUTPUT
RESULT:
CONCLUSIONS
In this paper, we study the deadlock problem in system level designs that include
complex synchronization constructs and function architecture separation and
mapping. We present our deadlock detection approach with a data structure called
the dynamic synchronization dependency graph and an associated deadlock
detection algorithm. We use two examples, a complex function model for video
processing and a model of function-architecture mapping, to demonstrate the
usefulness and effectiveness of our deadlock detection approach for system level
designs.We believe that the dynamic synchronization dependency graph can be
used to not only detect deadlock situations but also help search for livelock or
starvation problems. We are currently working on combining our simulation-
based deadlock detection mechanism and existing formal verification techniques
to search for more subtle
problems such as livelock and starvation in system level designs.
REFERENCES
1.Operating system concepts by Albert Silberschatz 8th edition.
2. http:/shodhganga.inflibnet.ac.in/bitstream/10603/13650/3/03_abstract.pdf
3. /www.geeksforgeeks.org/deadlock-prevention/
4 . https://en.wikipedia.org/wiki/Banker’s_algorithm