Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

banker Algorithm (deadlock avoidence), Summaries of Operating Systems

The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Typology: Summaries

2022/2023

Uploaded on 11/27/2023

aryan-verma-10
aryan-verma-10 🇮🇳

1 document

1 / 9

Related documents


Partial preview of the text

Download banker Algorithm (deadlock avoidence) and more Summaries Operating Systems in PDF only on Docsity! Banker’s Algorithm The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue. Why Banker’s algorithm named so? Banker’s algorithm is named so because it is used in the banking system to check whether a loan can be sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount from the total money that the bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done because if all the account holders come to withdraw their money then the bank can easily do it. In other words, the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its customers. The bank would try to be in a safe state always. Advantages Following are the essential characteristics of the Banker's algorithm: 1. It contains various resources that meet the requirements of each process. 2. Each process should provide information to the operating system for upcoming resource requests, the number of resources, and how long the resources will be held. 3. It helps the operating system manage and control process requests for each type of resource in the computer system. 4. The algorithm has a Max resource attribute that represents indicates each process can hold the maximum number of resources in a system. Disadvantages 1. It requires a fixed number of processes, and no additional processes can be started in the system while executing the process. 2. The algorithm does no longer allows the processes to exchange its maximum needs while processing its tasks. 3. Each process has to know and state their maximum resource requirement in advance for the system. 4. The number of resource requests can be granted in a finite time, but the time limit for allocating the resources is one year. Working on Bankers' algorithm When working with a banker's algorithm, it requests to know about three things: 1. How much each process can request for each resource in the system. It is denoted by the [MAX] request. 2. How much each process is currently holding each resource in a system. It is denoted by the [ALLOCATED] resource. 3. It represents the number of each resource currently available in the system. It is denoted by the [AVAILABLE] resource. The Banker's Algorithm is the combination of the safety algorithm and the resource request algorithm to control the processes and avoid deadlock in a system: The following Data structures are used to implement the Banker’s Algorithm: Let ‘n’ be the number of processes in the system and ‘m’ be the number of resource types. Allocation: • Specifies the number of resources currently allocated to a process. • 2-D array of size n*m. • Allocation[i,j]=k, i.e, 'k' instances of a resource Rj is allocated to process Pi. Max need: • Specifies the number of resources needed by a process. • 2-D array of size n*m. • Max need [i,j]=Max[i,j]-Allocation[i,j]. Available: • Specifies the number of available resources for a process. • 1-D array of size m. • Available[j]=k, ie, resource type Rj has k instances. Remaining need(Max): • Specifies the maximum demand of each process in a system. • 2-D array of size n*m. Else, the algorithm will keep the process till its next turn. Also, detection algorithm create safe sequence i.e. sequence of resource allocated processes without any deadlock. Here, safe sequence is P1 -> P3 -> P4 -> P0 -> P2 Code for Banker’s Algorithm #include <stdio.h> int main() { // P0, P1, P2, P3, P4 are the Process names here int n, m, i, j, k,alloc[10][10],max[10][10],avail[10]; // n = 5; // Number of processes printf("Enter NO. Of Processes"); scanf("%d",&n); // m = 3; // Number of resources printf("Enter Number of resources"); scanf("%d",&m); printf("Enter allocation matrics"); for (i=0;i<n;i++){ for(j=0;j<m;j++){ scanf("%d",&alloc[i][j]); } } printf("Enter Max matrics"); for (i=0;i<n;i++){ for(j=0;j<m;j++){ scanf("%d",&max[i][j]); } } printf("Enter Available array"); for (i=0;i<m;i++){ scanf("%d",&avail[i]); } // int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix // { 2, 0, 0 }, // P1 // { 3, 0, 2 }, // P2 // { 2, 1, 1 }, // P3 // { 0, 0, 2 } }; // P4 // int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix // { 3, 2, 2 }, // P1 // { 9, 0, 2 }, // P2 // { 2, 2, 2 }, // P3 // { 4, 3, 3 } }; // P4 // int avail[3] = { 3, 3, 2 }; // Available Resources int f[n], ans[n], ind = 0; for (k = 0; k < n; k++) { f[k] = 0; } int need[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) need[i][j] = max[i][j] - alloc[i][j]; } int y = 0; for (k = 0; k < 5; k++) { for (i = 0; i < n; i++) { if (f[i] == 0) { int flag = 0; for (j = 0; j < m; j++) { if (need[i][j] > avail[j]){ flag = 1; break; } } if (flag == 0) { ans[ind++] = i; for (y = 0; y < m; y++) avail[y] += alloc[i][y]; f[i] = 1;
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved