计算机操作系统课设需要,写了两个下午的银行家算法(陷在bug里出不来耽误了很多时间),参考计算机操作系统(汤子瀛)
实现过程中不涉及难度较大的算法,仅根据银行家算法的思想和步骤进行实现。以下为详细步骤:
- 定义:
max1[ ][ ] : 最大需求矩阵,max1[i][j]为第i条进程的第j项资源的最大需求数目;
allocation[ ][ ] : 分配矩阵,allocation[i][j]为第i条进程已分得的第j项资源的数目;
need[ ][ ] : 需求矩阵,need[i][j]为第i条进程尚需要的第j项资源的数目;
available[ ] : 可利用资源量,available[i]为系统中第i项资源的可分配数目;
request[ ][ ] : 请求矩阵,request[i][j]表示第i条进程对第j项资源的请求数目;//可以改成一维数组
int safe (int n,int m,int work) : n条进程,m项进程,返回值为1时当前状态安全,否则不安全;
- 程序流程:
- 键盘输入max1矩阵,allocation矩阵,available数组,计算出need矩阵。
- 判断当前时刻系统的状态是否安全。true 转向3,false转向7
- 判断当前时刻request<=need。true 转向4,false 转向7
- 判断当前时刻request<=available。true 转向5,false 转向7
- 进行安全性算法检测。true 转向6,false 转向7
- 系统分配资源并继续等待指令。
- 系统不予分配资源并输出原因。
- 安全性算法 : 每次从第一个进程开始检测,如遇到所有的m项资源都可以满足时,work+=allocation,否则转入下一个进程的检测。两种情况跳出第20行的循环。
- 所有finish均为1,i无法置为-1 ,i==N时跳出循环
- 存在为0的finish,但直至i==N时,仍未有新的work<need出现(从最近的一次i==-1算起),i==N时跳出循环
第50行进行检测区分上述两种情况,如安全返回1,否则返回0;
以下为完整的代码实现:(另附测试数据)
#include<bits/stdc++.h>
int max1[][]= {};
int allocation[][]= {};
int need[][]= {};
int finish[]= {};
int available[]= {};
int request[][]= {};
int waitq[]= {};
int waitnum=;
int safeq[]= {};
int safe (int N , int M ,int work[])
{
int s=;
memset(finish,,*sizeof(int));
for(int i=; i<M; i++)
{
work[i]=available[i];
}
int flag=;
for(int i=; i<N; i++)
{
flag=;
if(!finish[i])
{
for(int j=; j<M; j++)
{
if(need[i][j]>work[j])
{
flag=;
break;
}
}
if(flag)
{
for(int j=; j<M; j++)
{
work[j]+=allocation[i][j];
printf(" %d ",work[j]);
}
for(int j=; j<; j++)
printf("%d ",available[j]);
printf("program %d\n",i);
safeq[s++]=i;
finish[i]=;
i=-;
}
}
}
int te=;
for(int i=; i<; i++)
if(!finish[i])
te=;
return te;
}
void print(int pn,int yn)
{
printf("current status\n");
char a='A';
int i2=;
for(i2=; i2<; i2++)
{
switch(i2)
{
case :
printf("Max:");
for(int i=; i<yn-; i++)
printf(" ");
printf(" ");
break;
case :
printf("Allocation:");
for(int i=; i<yn-; i++)
printf(" ");
printf(" ");
break;
case :
printf("Need:");
for(int i=; i<yn-; i++)
printf(" ");
break;
case :
printf("Available:");
for(int i=; i<yn-; i++)
printf(" ");
printf(" ");
printf("\n");
break;
}
}
for(i2=; i2<; i2++)
{
switch(i2)
{
case :
for(int j=; j<yn; j++)
printf("%c ",a+j);
break;
case :
for(int j=; j<yn; j++)
printf("%c ",a+j);
break;
case :
for(int j=; j<yn; j++)
printf("%c ",a+j);
break;
case :
for(int j=; j<yn; j++)
printf("%c ",a+j);
break; }
}
printf("\n");
for(int i=; i<pn; i++)
{
for(int j=; j<yn; j++)
{
printf("%d ",max1[i][j]);
}
for(int j=; j<yn; j++)
{
printf("%d ",allocation[i][j]);
}
for(int j=; j<yn; j++)
{
printf("%d ",need[i][j]);
}
if(i==)
for(int j=; j<yn; j++)
printf("%d ",available[j]);
printf("\n");
}
}
int main()
{
int work[]= {};
int pn,yn;
printf("Please input the number of the program\n");
scanf("%d",&pn);
printf("Please input the number of the element\n");
scanf("%d",&yn);
printf("Please input Max and Allocation of the program \n");
for(int i=; i<pn; i++)
{
for(int j=; j<yn; j++)
{
scanf("%d",&max1[i][j]);
}
for(int j=; j<yn; j++)
{
scanf("%d",&allocation[i][j]);
}
for(int j=; j<yn; j++)
{
need[i][j]=max1[i][j]-allocation[i][j];
}
}
printf("Please input the Available \n");
for(int i=; i<yn; i++)
{
scanf("%d",&available[i]);
work[i]=available[i];
} if(safe(pn,yn,work))
{
printf("it is safe now \n");
for(int i=; i<pn; i++)
printf("%d ",safeq[i]);
printf("\n");
printf("is the one of the safe sequence \n");
}
else
printf("it is not safe now\n"); if(safe(pn,yn,work))
{
while()
{
int num;
int ex;
int judge=;
printf("if you want to exit , please input 0 else input 1 \n");
scanf("%d",&ex);
if(!ex)
break;
printf("Please input the number of the request program \n");
scanf("%d",&num);
printf("Please input the Request \n");
for(int i=; i<yn; i++)
{
scanf("%d",&request[num][i]);
if(request[num][i]>need[num][i])
{
judge=;
printf("error!\n");
break;
}
}
if(judge)
{
int wait=;
for(int i=; i<yn; i++)
{
if(request[num][i]>available[i])
{
wait=;
printf("wait because request>available!\n");
break;
}
}
if(!wait)
{ for(int j1=; j1<yn; j1++)
{
available[j1]-=request[num][j1];
allocation[num][j1]+=request[num][j1];
need[num][j1]-=request[num][j1];
}
if(safe(pn,yn,work))
{
printf("it is safe now \n");
for(int i=; i<pn; i++)
printf("%d ",safeq[i]);
printf("\n");
printf("is the one of the safe sequence \n");
printf("complete !!!!!!!\n");
}
else
{
for(int j1=; j1<yn; j1++)
{
available[j1]+=request[num][j1];
allocation[num][j1]-=request[num][j1];
need[num][j1]+=request[num][j1];
}
printf("wait because it is not safe \n");
}
} }
}
}
print(pn,yn);
} /*
5
3
7 5 3 0 1 0
3 2 2 2 0 0
9 0 2 3 0 2
2 2 2 2 1 1
4 3 3 0 0 2
3 3 2
1
1
1 0 2
1
4
3 3 0
1
0
0 2 0
0 */