Bankers Algorithm examples and explan, Assignments of Operating Systems

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

2019/2020

Uploaded on 05/31/2020

youssef-harby
youssef-harby 🇪🇬

3

(1)

1 document

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ABSTRACT
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Bankers Algorithm examples and explan and more Assignments Operating Systems in PDF only on Docsity!

ABSTRACT

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.

1.0 INTRODUCTION

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.

1.1 NECESSARY CONDITIONS FOR DEADLOCK

A deadlock situation can arise if the following four conditions hold simultaneously in a system.

  1. Mutual exclusion: At least one resource must be held in a non-sharable mode; that is, only one process at a time can use the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.
  2. Hold and wait: A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes.
  3. No preemption: Resources cannot be preempted; that is, a resource can be released only voluntarily by the process holding it, after that process has completed it task.
  4. Circular wait: A set {P0, P1, …, Pn} of waiting processes must exist such that P is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2,…, Pn-1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

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:

  1. Safe state
  2. Resource Allocation Graph Algorithm
  3. Banker’s Algorithm

2.0 BANKER’S ALGORITHM

2.1 INTRODUCTION

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

  1. Request < = max, else set error condition as process has crossed maximum claim made by it.
  2. Request < = available, else process waits till resources are available. Some of the resources that are tracked in real systems are memory, semaphores and interface access.

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.

  1. Can the request be granted?  If not, the request is impossible and must either be denied or put on a waiting list
  2. Assume that the request is granted
  3. Is the new state safe?  If so grant the request.  If not, either deny the request or put it on a waiting list. Whether the system denies or postpones an impossible or unsafe request is a decision specific to the operating system_._

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

  1. If we assume that Process D completes, it will turn over its currently allocated resources, incrementing the Available vector. (1, 0, 2, 0) Current value of Available
    • (1, 1, 0, 1) Allocation (Process D) (2, 1, 2, 1) Updated value of Available

Iteration 2:

  1. Examine the Need matrix, ignoring the row for Process D. The only row that is less than the Available vector is the one for Process A. Need(Process A) = (1, 1, 0, 0) < (2, 1, 2, 1) = Available
  2. If we assume that Process A completes, it will turn over its currently allocated resources, incrementing the Available vector. (2, 1, 2, 1) Current value of Available
    • (2, 0, 1, 1) Allocation (Process A) (4, 1, 3, 2) Updated value of Available

Iteration 3:

  1. Examine the Need matrix without the row for Process D and Process A. The only row that is less than the Available vector is the one for Process B. Need(Process B) = (0, 1, 1, 2) < (4, 1, 3, 2) = Available
  1. If we assume that Process B completes, it will turn over its currently allocated resources, incrementing the Available vector. (4, 1, 3, 2) Current value of Available +(0, 1, 0, 0) Allocation (Process B) (4, 2, 3, 2) Updated value of Available

Iteration 4:

  1. Examine the Need matrix without the rows for Process A, Process B, and Process D. The only row left is the one for Process C, and it is less than the Available vector. Need(Process C) = (3, 1, 0, 0) < (4, 2, 3, 2) = Available
  2. If we assume that Process C completes, it will turn over its currently allocated resources, incrementing the Available vector. (4, 2, 3, 2) Current value of Available
    • (1, 0, 1, 1) Allocation (Process C) (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:

PROGRAM TO IMPLEMENT BANKER'S ALGORITHM

#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.

REFERENCES

Banker's algorithm Wikipedia, the free encyclopedia Dijkstra EW (1965) Cooperating Sequential Processes. Technical report, Technological University, Eindhoven, The Netherlands, pp 43– 112

TABLE OF CONTENT

  • Abstract ……………………………………………………………………………….
  • Introduction …………………………………………………………………………...
  • Method of handling deadlock ………………………………………………………....
  • Bankers algorithm ………………………………………………………………….…
  • Application of bankers algorithm ………………………………………………...…..
  • Program to implement bankers algorithm …………………………………………...
  • Conclusion …………………………………………………………………………...
  • References ……………………………………………………………………………

AN ASSIGNMENT

ON

BANKER’S ALGORITHM

WRITTEN BY

EWANG, KINGSLEY ANTHONY

AK11/NAS/CSC/

SUBMITTED TO

DR I. J UMOREN

(COURSE LECTURER CSC 352)

JANUARY, 2016.