操作系统之银行家算法
实验目的
银行家算法是由Dijkstra设计的最具有代表性的避免死锁的算法。本实验要求用高级语言编写一个银行家的模拟算法。通过本实验可以对预防死锁和银行家算法有更深刻的认识。
实验要求
1、 设置数据结构
包括资源向量(Resource),最大需求矩阵(Need),分配矩阵(Allocation),
需求矩阵(Request), 可利用剩余资源数(Available)
2、 设计安全性算法
设置工作向量Work 表示系统可提供进程继续运行可利用资源数目,Finish
表示系统是否有足够的资源分配给进程
3、 利用三组数据分别测试银行家算法,并给出结果,包括安全状态和不安全状态。安全状态请给出进程进行资源分配的安全序列
总体流程
安全检测算法流程
算法代码
#include<stdio.h>
#define true 1
#define false 0
#define N 4
#define M 3
#define MAXINT 9999
int resource[M] = { 10,5,7 };
int available[M] = { 0 };
int need[N][M] =
{ 7,5,3,
3,2,2,
9,0,2,
2,2,2 };
int allocation[N][M] =
{ 0,1,0,
2,0,0,
3,0,2,
2,1,1 };
int request[N][M] = { 0 };
bool Finish[N];
int safeSeries[N] = { MAXINT, MAXINT , MAXINT , MAXINT };//用于存放安全序列
void Init();
void showInfo();
bool isSafe();
void SafeInfo(int* work, int i);
void Init()
{
int i, j;
//printf("输入进程数量、资源数量\n");
//scanf("%d %d",&processNum,&resourceNum);
printf("输入当前资源数目\n");
for (i = 0; i < M; i++) {
scanf("%d", &resource[i]);
}
printf("输入最大需求矩阵\n");
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
scanf("%d", &need[i][j]);
}
}
printf("输入分配矩阵\n");
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
scanf("%d", &allocation[i][j]);
}
}
// printf("输入当前需求矩阵\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
request[i][j] = need[i][j] - allocation[i][j];
}
}
}
void showInfo()
{
int i, j;
printf("当前资源总量:");
for (j = 0; j < M; j++) {
printf("%d ", resource[j]);
}
printf("\n");
//计算Avaliable向量
for (i = 0; i < M; i++)
{
int count = 0;
for (j = 0; j < N; j++)
{
count += allocation[j][i];
}
available[i] = resource[i] - count;
}
//计算request向量
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
request[i][j] = need[i][j] - allocation[i][j];
}
}
printf(" PID\t Need\t\tAllocation\tRequest\n");
for (i = 0; i < N; i++)
{
printf(" P%d\t", i);
for (j = 0; j < M; j++)
{
printf("%d ", need[i][j]);
}
printf("\t\t");
for (j = 0; j < M; j++)
{
printf("%d ", allocation[i][j]);
}
printf("\t\t");
for (j = 0; j < M; j++)
{
printf("%d ", request[i][j]);
}
printf("\n");
}
printf("当前资源剩余:");
for (j = 0; j < M; j++) {
printf("%d ", available[j]);
}
}
bool isSafe()
{
int i, j, k;
int trueFinished = 0;
int work[M];
for (i = 0; i < M; i++)
{
work[i] = available[i];//
}
for (i = 0; i < N; i++)
{
Finish[i] = false;
}
i = 0;
int temp = 0;
int temp0 = 0;
int flag = 0;
while (trueFinished != N)
{
int j = 0;
if (Finish[i] != true)
{
for (j = 0; j < M; j++)
{
if (request[i][j] > work[j])
{
break;
}
}
}
if (j == M)
{
Finish[i] = true;
SafeInfo(work, i);
for (k = 0; k < M; k++)
{
work[k] += allocation[i][k];
}
int k2;
safeSeries[trueFinished++] = i;
}
i++;
if (i >= N)
{
if (flag == 0)
{
temp = trueFinished;
temp0 = trueFinished;
}
i = i % N;
if (flag == 1) {
temp = trueFinished;
if (temp == temp0)
break;
else
temp0 = temp;
}
flag = 1;
}
temp = trueFinished;
}
if (trueFinished == N)
{
printf("\n系统安全!\n\n安全序列为:");
for (i = 0; i < N; i++)
{
printf("%d ", safeSeries[i]);
}
printf("\n");
return true;
}
printf("******系统不安全!******\n");
return false;
}
void SafeInfo(int* work, int i)
{
int j;
printf(" P%d\t", i);
for (j = 0; j < M; j++)
{
printf("%d ", work[j]);
}
printf("\t\t");
for (j = 0; j < M; j++)
{
printf("%d ", request[i][j]);
}
printf("\t ");
for (j = 0; j < M; j++)
{
printf("%d ", allocation[i][j]);
}
printf("\t\t");
for (j = 0; j < M; j++)
{
printf("%d ", allocation[i][j] + work[j]);
}
printf("\n");
}
int main()
{
int i, j, curProcess;
int wheInit = 0;
printf("是否使用内置数据?0是,1否:");
scanf("%d", &wheInit);
if (wheInit)
{
Init();
}
printf("---------------------------------------------------------\n");
showInfo();
printf("\n---------------------------------------------------------\n");
printf("\n系统安全情况分析\n");
printf(" PID\t Work\t\tRequest\tAllocation\tWork+Allocation\n");
isSafe();
return 0;
}