银行家算法是一种资源分配和死锁避免算法,它通过模拟所有资源的预定最大可能数量的分配来测试安全性,然后进行“s状态”检查以测试可能的活动,然后决定是否应该允许继续分配。
为什么银行家算法是这样命名的?
Banker’s algorithm 之所以叫“银行家算法”,是因为它在银行系统中被用来检查贷款是否可以发放给一个人。假设一家银行有n个账户持有人,他们的存款总额是S。如果一个人申请贷款,那么银行首先从银行的存款总额中减去贷款金额,如果余额大于S,那么只有贷款被批准。这样做的原因是,如果所有的账户持有人都来取钱,那么银行就可以很容易地做到这一点。
换句话说,银行绝不会以这样一种方式分配资金。
有效值(Available)
它是一个大小为' m '的一维数组,表示每种类型的可用资源的数量。
Available[j] = k表示资源类型Rj有' k '个实例
最大值(Max)
它是一个大小为' n*m '的二维数组,它定义了系统中每个进程的最大需求。
Max[i, j] = k表示进程Pi最多可以请求资源类型Rj的' k '个实例。
可分配值(Allocation)
它是一个大小为' n*m '的二维数组,定义了当前分配给每个进程的每种类型的资源数量。
分配[i, j] = k表示进程Pi当前被分配资源类型Rj的' k '个实例
需要值 (Need)
它是一个大小为' n*m '的二维数组,表示每个进程所需的剩余资源。
Need [i, j] = k表示流程Pi当前需要资源类型Rj的' k '个实例
需求[i, j] =最大[i, j] -分配[i, j]
Allocationi指定当前分配给Pi处理的资源,Needi指定Pi处理为完成任务可能仍然请求的额外资源。
银行家算法由安全算法和资源请求算法组成
安全算法(Safety Algorithm)
判断系统是否处于安全状态的算法描述如下:
1)设Work和Finish分别为长度为“m”和“n”的向量。
初始化: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) 找到一个i
a) Finish[i] = false
b) Needi <= Work
如果不存在I,请执行步骤(4)
3) Work = Work + Allocation[i]
Finish[i] = true
执行步骤2
4) if Finish [i] = true for all i
此时系统处于安全状态
资源请求算法(Resource-Request Algorithm)
设Requesti为进程Pi的请求数组。Requesti [j] = k表示进程Pi需要k个资源类型Rj的实例。当Pi进程请求资源时,会执行以下操作:
1) If Requesti <= Needi
后到步骤(2);否则,抛出一个错误条件,因为进程已经超过了它的最大请求。
2) If Requesti <= Available
转到步骤(3);否则,Pi必须等待,因为资源不可用
3) 系统是否通过将状态修改为,假装已将请求的资源分配给处理Pi
如下:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
例子:
考虑一个有五个进程P0到P4的系统,以及a、B、C类型的三个资源。资源类型a有10个实例,B有5个实例,C有7个实例。假设在t0时刻,系统的以下快照已经被截取:
问题1。需求矩阵的内容是什么?
需求[i, j] =最大[i, j] -分配[i, j]
所以,需求矩阵的内容是:
问题2。系统是否处于安全状态?如果是,那么什么是安全序列?
将Safety算法应用于给定系统
问题3。如果进程P1请求一个资源类型A的额外实例和两个资源类型C的实例,会发生什么情况?
我们必须确定这个新的系统状态是否安全。为此,我们再次对上述数据结构执行Safety算法。
因此,新的系统状态是安全的,因此我们可以立即为流程P1授予请求。
银行家算法代码
C语言
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
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;
}
}
}
}
int flag = 1;
for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}
if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
// This code is contributed by Deep Baldha (CandyZack)
}
c++代码
// Banker's Algorithm
#include <iostream>
using namespace std;
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
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;
}
}
}
}
int flag = 1;
// To check if sequence is safe or not
for(int i = 0;i<n;i++)
{
if(f[i]==0)
{
flag = 0;
cout << "The given sequence is not safe";
break;
}
}
if(flag==1)
{
cout << "Following is the SAFE Sequence" << endl;
for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;
}
return (0);
}
Java代码
//Java Program for Bankers Algorithm
public class GfGBankers
{
int n = 5; // Number of processes
int m = 3; // Number of resources
int need[][] = new int[n][m];
int [][]max;
int [][]alloc;
int []avail;
int safeSequence[] = new int[n];
void initializeValues()
{
// P0, P1, P2, P3, P4 are the Process names here
// Allocation Matrix
alloc = new int[][] { { 0, 1, 0 }, //P0
{ 2, 0, 0 }, //P1
{ 3, 0, 2 }, //P2
{ 2, 1, 1 }, //P3
{ 0, 0, 2 } }; //P4
// MAX Matrix
max = new int[][] { { 7, 5, 3 }, //P0
{ 3, 2, 2 }, //P1
{ 9, 0, 2 }, //P2
{ 2, 2, 2 }, //P3
{ 4, 3, 3 } }; //P4
// Available Resources
avail = new int[] { 3, 3, 2 };
}
void isSafe()
{
int count=0;
//visited array to find the already allocated process
boolean visited[] = new boolean[n];
for (int i = 0;i < n; i++)
{
visited[i] = false;
}
//work array to store the copy of available resources
int work[] = new int[m];
for (int i = 0;i < m; i++)
{
work[i] = avail[i];
}
while (count<n)
{
boolean flag = false;
for (int i = 0;i < n; i++)
{
if (visited[i] == false)
{
int j;
for (j = 0;j < m; j++)
{
if (need[i][j] > work[j])
break;
}
if (j == m)
{
safeSequence[count++]=i;
visited[i]=true;
flag=true;
for (j = 0;j < m; j++)
{
work[j] = work[j]+alloc[i][j];
}
}
}
}
if (flag == false)
{
break;
}
}
if (count < n)
{
System.out.println("The System is UnSafe!");
}
else
{
//System.out.println("The given System is Safe");
System.out.println("Following is the SAFE Sequence");
for (int i = 0;i < n; i++)
{
System.out.print("P" + safeSequence[i]);
if (i != n-1)
System.out.print(" -> ");
}
}
}
void calculateNeed()
{
for (int i = 0;i < n; i++)
{
for (int j = 0;j < m; j++)
{
need[i][j] = max[i][j]-alloc[i][j];
}
}
}
public static void main(String[] args)
{
int i, j, k;
GfGBankers gfg = new GfGBankers();
gfg.initializeValues();
//Calculate the Need Matrix
gfg.calculateNeed();
// Check whether system is in safe state or not
gfg.isSafe();
}
}
python3 代码
# Driver code:
if __name__=="__main__":
# P0, P1, P2, P3, P4 are the Process names here
n = 5 # Number of processes
m = 3 # Number of resources
# Allocation Matrix
alloc = [[0, 1, 0 ],[ 2, 0, 0 ],
[3, 0, 2 ],[2, 1, 1] ,[ 0, 0, 2]]
# MAX Matrix
max = [[7, 5, 3 ],[3, 2, 2 ],
[ 9, 0, 2 ],[2, 2, 2],[4, 3, 3]]
avail = [3, 3, 2] # Available Resources
f = [0]*n
ans = [0]*n
ind = 0
for k in range(n):
f[k] = 0
need = [[ 0 for i in range(m)]for i in range(n)]
for i in range(n):
for j in range(m):
need[i][j] = max[i][j] - alloc[i][j]
y = 0
for k in range(5):
for i in range(n):
if (f[i] == 0):
flag = 0
for j in range(m):
if (need[i][j] > avail[j]):
flag = 1
break
if (flag == 0):
ans[ind] = i
ind += 1
for y in range(m):
avail[y] += alloc[i][y]
f[i] = 1
print("Following is the SAFE Sequence")
for i in range(n - 1):
print(" P", ans[i], " ->", sep="", end="")
print(" P", ans[n - 1], sep="")
# This code is contributed by SHUBHAMSINGH10
C#代码
// C# Program for Bankers Algorithm
using System;
using System.Collections.Generic;
class GFG
{
static int n = 5; // Number of processes
static int m = 3; // Number of resources
int [,]need = new int[n, m];
int [,]max;
int [,]alloc;
int []avail;
int []safeSequence = new int[n];
void initializeValues()
{
// P0, P1, P2, P3, P4 are the Process
// names here Allocation Matrix
alloc = new int[,] {{ 0, 1, 0 }, //P0
{ 2, 0, 0 }, //P1
{ 3, 0, 2 }, //P2
{ 2, 1, 1 }, //P3
{ 0, 0, 2 }};//P4
// MAX Matrix
max = new int[,] {{ 7, 5, 3 }, //P0
{ 3, 2, 2 }, //P1
{ 9, 0, 2 }, //P2
{ 2, 2, 2 }, //P3
{ 4, 3, 3 }};//P4
// Available Resources
avail = new int[] { 3, 3, 2 };
}
void isSafe()
{
int count = 0;
// visited array to find the
// already allocated process
Boolean []visited = new Boolean[n];
for (int i = 0; i < n; i++)
{
visited[i] = false;
}
// work array to store the copy of
// available resources
int []work = new int[m];
for (int i = 0; i < m; i++)
{
work[i] = avail[i];
}
while (count<n)
{
Boolean flag = false;
for (int i = 0; i < n; i++)
{
if (visited[i] == false)
{
int j;
for (j = 0; j < m; j++)
{
if (need[i, j] > work[j])
break;
}
if (j == m)
{
safeSequence[count++] = i;
visited[i] = true;
flag = true;
for (j = 0; j < m; j++)
{
work[j] = work[j] + alloc[i, j];
}
}
}
}
if (flag == false)
{
break;
}
}
if (count < n)
{
Console.WriteLine("The System is UnSafe!");
}
else
{
//System.out.println("The given System is Safe");
Console.WriteLine("Following is the SAFE Sequence");
for (int i = 0; i < n; i++)
{
Console.Write("P" + safeSequence[i]);
if (i != n - 1)
Console.Write(" -> ");
}
}
}
void calculateNeed()
{
for (int i = 0;i < n; i++)
{
for (int j = 0;j < m; j++)
{
need[i, j] = max[i, j] - alloc[i, j];
}
}
}
// Driver Code
public static void Main(String[] args)
{
GFG gfg = new GFG();
gfg.initializeValues();
// Calculate the Need Matrix
gfg.calculateNeed();
// Check whether system is in
// safe state or not
gfg.isSafe();
}
}
// This code is contributed by Rajput-Ji
JavaScript代码
<script>
let n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
let alloc = [ [ 0, 1, 0 ], // P0 // Allocation Matrix
[ 2, 0, 0 ], // P1
[ 3, 0, 2 ], // P2
[ 2, 1, 1 ], // P3
[ 0, 0, 2 ] ]; // P4
let max = [ [ 7, 5, 3 ], // P0 // MAX Matrix
[ 3, 2, 2 ], // P1
[ 9, 0, 2 ], // P2
[ 2, 2, 2 ], // P3
[ 4, 3, 3 ] ]; // P4
let avail = [ 3, 3, 2 ]; // Available Resources
let f = [], ans = [], ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
}
let need = [];
for (i = 0; i < n; i++) {
let need1 = [];
for (j = 0; j < m; j++)
need1.push(max[i][j] - alloc[i][j]);
need.push(need1);
}
let y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
let 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;
}
}
}
}
document.write("Following is the SAFE Sequence" + "<br>");
for (i = 0; i < n - 1; i++)
document.write(" P" + ans[i] + " ->");
document.write( " P" + ans[n - 1] + "<br>");
</script>
文章来源:https://www.toymoban.com/news/detail-456591.html
输出结果:P1 -> p3 -> p4 -> p0 -> p2
当进程进入系统时,它们必须预测所需资源的最大数量,而确定这一数量并非不切实际。
在该算法中,进程的数量保持固定,这在交互系统中是不可能的。
这种算法要求分配的资源数量是固定的。如果一个设备坏了,突然变得不可用,该算法将无法工作。
当有许多进程和资源时,该算法产生的开销成本可能很高,因为它必须被调用文章来源地址https://www.toymoban.com/news/detail-456591.html
到了这里,关于操作系统银行家算法(JAVA/Python/C#/JavaScript/C/C++)代码实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!