












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In an operating system, there exist at least three strategies dealing with deadlocks for concurrent processes namely; deadlock prevention, avoidance and detection, in the increasing order of handling extent. Deadlock avoidance tries to contain the system in a safe state so that deadlock will never occur.
Typology: Assignments
1 / 20
This page cannot be seen from the preview
Don't miss anything!













In an operating system, there exist at least three strategies dealing with deadlocks for concurrent processes namely; deadlock prevention, avoidance and detection, in the increasing order of handling extent. Deadlock avoidance tries to contain the system in a safe state so that deadlock will never occur. In this paper, we present an algorithm for the deadlock avoidance problem – “The Banker’s algorithm”. The Banker’s algorithm is a classical algorithm on deadlock avoidance. It is called the Banker’s Algorithm because it could be used by a Bank to make sure that money is allocated in such a way that all customer needs are met. It has time complexity of O( n 3 d ), where n is the number of processes and d is the number of resources.
In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources; if the resources are not available at that time, the process enters a wait state. Waiting processes may never again change state, because the resources they have requested are held by other waiting processes. This situation is called a deadlock.
Most current operating systems do not provide deadlock-prevention facilities, but such features will probably be added soon. Deadlock problems can only become more common, given current trends, including larger numbers of processes, multithreaded programs, many more resources within a system, and the emphasis on long-lived file and database servers rather than batch systems.
A deadlock situation can arise if the following four conditions hold simultaneously in a system.
We emphasize that all four conditions must hold for a deadlock to occur. The circular- wait condition implies the hold-and -wait condition, so the four conditions are not completely independent.
1.2 METHODS FOR HANDLING DEADLOCKS
following protocol. If a process is holding some resources and requests another resource that cannot be immediately allocated to it, then all resources currently being held are preempted. In other words, these resources are implicitly released. The preempted resources are added to the list of resources for which the process is waiting. The process will be restarted only when it regain its old resources, as well as the new ones that it is requesting.
Circular Wait
The fourth and final condition for deadlocks is the circular-wait condition. One way to ensure that this condition never holds is to impose a total ordering of all resource types, and to require that each process requests resources in an increasing order of enumeration.
1.2.2 Deadlock Avoidance
Deadlock avoidance requires that the operating system be given in advance additional information concerning which resources a process will request and use during its lifetime. A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular-wait condition can never exist. The resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.
The following are the methods of deadlock avoidance:
Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined
maximum possible amounts of all resources, and then makes a "safe state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue. In an operating system, deadlock is a state in which two or more processes are "stuck" in a circular wait state. All deadlocked processes are waiting for resources held by other processes. Because most systems are non-preemptive (that is, will not take resources held by a process away from it), and employ a hold and wait method for dealing with system resources (that is, once a process gets a certain resource it will not give it up voluntarily), deadlock is a dangerous state that can cause poor system performance.
Banker's algorithm is modeled on the way a small town banker might deal with customers' lines of credit. By using the Banker's algorithm, the bank ensures that when customers request money the bank never leaves a safe state. If the customer's request does not cause the bank to leave a safe state, the cash will be allocated, otherwise the customer must wait until some other customer deposits enough. The Banker's Algorithm seeks to prevent deadlock by becoming involved in the granting or denying of system resources. Each time that a process needs a particular non-sharable resource, the request must be approved by the banker, the banker takes a careful look at the bank books and attempts to determine whether or not a deadlock state could possibly arise in the future if the loan request is approved. This determination is made by “pretending'' to grant the request and then looking at the resulting post granted request system state. After granting a resource request there will be an amount of that resource left free in the system. Furthermore, there may be other processes in system. We demanded that each of these other processes state the maximum amount of all system resources they needed to terminate upfront so, therefore, we know how much of each resource every process is holding and has claim to. If the banker has enough free resource to guarantee that even one process can terminate, it can then take the resource held by that process and add it to the free pool. At this point the banker can look at the (hopefully) now larger free pool and attempt to guarantee that another process will terminate by checking whether its claim can be met. If the banker can guarantee that all jobs in system will terminate, it approves
The Banker's algorithm: Allows: mutual exclusion hold and wait no preemption Prevents: circular wait User process may only request one resource at a time. System grants request only if the request will result in a safe state.
Safe and Unsafe States A state is considered safe if it is possible for all processes to finish executing (terminate). Since the system cannot know when a process will terminate, or how many resources it will have requested by then, the system assumes that all processes will eventually attempt to acquire their stated maximum resources and terminate soon afterward. Also, if a process terminates without acquiring its maximum resource it only makes it easier on the system. A safe state is considered to be the decision maker if it is going to process ready queue. Safe State ensures the Security. Given that assumption, the algorithm determines if a state is safe by trying to find a hypothetical set of requests by the processes that would allow each to acquire its maximum resources and then terminate (returning its resources to the system). Any state where no such set exists is an unsafe state.
Requests When the system receives a request for resources, it runs the Banker's algorithm to determine if it is safe to grant the request. The algorithm is fairly straight forward once the distinction between safe and unsafe states is understood.
The Banker's algorithm Example:
Assume we have the following resources: 5 tape drives 2 graphic displays 4 printers 3 disks We can create a vector representing our total resources: Total = (5, 2, 4, 3). Consider we have already allocated these resources among four processes as demonstrated by the following matrix named Allocation. Process Name Tape Drives Graphics Printers Disk Drives Process A 2 0 1 1 Process B 0 1 0 0 Process C 1 0 1 1 Process D 1 1 0 1 The vector representing the allocated resources is the sum of these columns:
Need(Process D) = (0, 0, 1, 0) < (1, 0, 2, 0) = Available
(2, 1, 2, 1) Updated value of AvailableIteration 2:
(4, 1, 3, 2) Updated value of AvailableIteration 3:
(4, 2, 3, 2) Updated value of AvailableIteration 4:
(5, 2, 4, 3) Updated value of Available Notice that the final value of the Available vector is the same as the original Total vector, showing the total number of all resources: Total = (5, 2, 4, 2) < (5, 2, 4, 2) = Available This means that the initial state represented by the Allocation and Need matrices is a safe state. The safe sequence that assures this safe state is <D, A, B, C>.THE BANKER'S ALGORITHM - ADVANTAGE:
#include <stdio.h>
int curr[5][5], maxclaim[5][5], avl[5];
int alloc[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe=0;
int count = 0, i, j, exec, r, p, k = 1;
int main()
{
printf("\nEnter the number of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++) {
running[i] = 1;
count++;
}
printf("\nEnter the number of resources: ");
scanf("%d", &r);
printf("\nEnter Claim Vector:");
for (i = 0; i < r; i++) {
scanf("%d", &maxres[i]);
}
printf("\nEnter Allocated Resource Table:\n");
for (i = 0; i < p; i++) {
for(j = 0; j < r; j++) {
scanf("%d", &curr[i][j]);
}
}
printf("\nEnter Maximum Claim Table:\n");
for (i = 0; i < p; i++) {
for(j = 0; j < r; j++) {
scanf("%d", &maxclaim[i][j]);
}
}
printf("\nThe Claim Vector is: ");
for (i = 0; i < r; i++) {
printf("\t%d", maxres[i]);
}
printf("\nThe Allocated Resource Table:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
printf("\t%d", curr[i][j]);
}
printf("\n");
}
printf("\nThe Maximum Claim Table:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
printf("\t%d", maxclaim[i][j]);
}
printf("\n");
}
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
alloc[j] += curr[i][j];
}
running[i] = 0;
count‐‐;
safe = 1;
for (j = 0; j < r; j++) {
avl[j] += curr[i][j];
}
break;
}
}
}
if (!safe) {
printf("\nThe processes are in unsafe state.\n");
break;
} else {
printf("\nThe process is in safe state");
printf("\nAvailable vector:");
for (i = 0; i < r; i++) {
printf("\t%d", avl[i]);
}
printf("\n");
}
}
}
CONCLUSION
In an operating system, deadlock is a state in which two or more processes are "stuck" in a circular wait state. All deadlocked processes are waiting for resources held by other processes. Because most systems are non-preemptive (that is, will not take resources held
by a process away from it), and employ a hold and wait method for dealing with system resources (that is, once a process gets a certain resource it will not give it up voluntarily), deadlock is a dangerous state that can cause poor system performance. The goal of this algorithm is to handle all the requests without entering into the unsafe state. One reason this algorithm is not widely used in the real world is because to use it, the operating system must know the maximum amount of resources that every process is going to need at all times.
Banker's algorithm Wikipedia, the free encyclopedia Dijkstra EW (1965) Cooperating Sequential Processes. Technical report, Technological University, Eindhoven, The Netherlands, pp 43– 112