运行结果
源代码
#include<stdio.h>
#include<stdlib.h>
int Max[5][3] = { {7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };//第几个进程需要多少第几个资源
int Allocation[5][3] = { {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2} };//已占有的资源
int Need[5][3] = { {7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1} };//还需要的资源
int Available[3] = {3,3,2};//可用的资源
int Resource[3] = {10,5,7};//资源总数
bool Finish[5] = { false,false,false,false,false };
int now[3] = { 3,3,2 };
int Work[3] = {3,3,2};//此时可用的资源
int Work_Allocation[5][3];
int sequence[5] = {-1,-1,-1,-1,-1};//安全队列
int num = 0;
int boolen[5];
int clock = 0;
bool is_safe = 1;
void display()
{
printf("--------------当前进程资源情况-------------\n");
printf("\t%10s%16s%10s\n", "Max", "Allocation", "Need");
printf("进程名\t");
for (int x = 0; x < 3; x++) {
printf(" ");
for (int i = 0; i < 3; i++)
printf("%2c", 'a' + i);
}
printf("\n");
for (int i = 0; i < 5; i++) {
printf(" %d ", i);
printf(" ");
for (int j = 0; j < 3; j++)
printf("%2d", Max[i][j]);
printf(" ");
for (int j = 0; j < 3; j++)
printf("%2d", Allocation[i][j]);
printf(" ");
for (int j = 0; j < 3; j++)
printf("%2d", Need[i][j]);
printf("\n");
}
printf("系统可利用资源:");
for (int i = 0; i < 3; i++)
printf("%3d", Available[i]);
printf("\n ---------------------------------------------\n");
}
void Sequence()
{
is_safe = 1;
clock = 0;
clock++;
if (clock >5)
{
for (int i = 0; i < 5; i++)
{
if (sequence[i] == -1) {
printf("不安全,无法分配资源\n");
is_safe = 0;
break;
}
}
}
for (int i = 0; i < 5; i++)
{
int flag = 0;
if (boolen[i] == 1)
continue;
for (int j = 0; j < 3; j++)
{
if (Need[i][j] > now[j])
flag = 1;
}
if (flag != 1)
{
sequence[num] = i;
Finish[num] = true;
boolen[i] = 1;
for (int k = 0; k < 3; k++)
{
now[k] = now[k] + Allocation[i][k];
Work_Allocation[num][k] = now[k];
}
num++;
Sequence();
break;
}
}
}
void display2() {
printf("----------------------------安全性队列----------------------------\n");
printf("\t%15s%16s%18s%20s\n", "Work", "Need", "Allocation","Work+Allocation");
printf("进程名\t");
for (int x = 0; x < 4; x++) {
printf(" ");
for (int i = 0; i < 3; i++)
printf("%2c", 'a' + i);
}
printf("%15s", "Finish");
printf("\n");
for (int num = 0; num < 5; num++) {
printf(" %d ", sequence[num]);
printf(" ");
for (int j = 0; j < 3; j++)
{
if (num == 0)
printf("%2d", Work[j]);
else
printf("%2d", Work_Allocation[num - 1][j]);
}
printf(" ");
for (int j = 0; j < 3; j++)
printf("%2d", Need[sequence[num]][j]);
printf(" ");
for (int j = 0; j < 3; j++)
printf("%2d", Allocation[sequence[num]][j]);
printf(" ");
for (int j = 0; j < 3; j++)
printf("%2d", Work_Allocation[num][j]);
printf(" ");
if(Finish[num]==1)
printf("TRUE");
printf("\n");
}
}
void Request()
{
int f = 0;
int m;
int input[3];
int n;
printf("请输入您想操作的进程(0,1,2,3,4):");
scanf_s("%d", &m);
for (int i = 0; i < 5; i++)
{
if (sequence[i] == m)
n = i;//该进程是第i个顺序
}
printf("请输入您想申请的各资源的个数(0,1,2):");
for (int i = 0; i < 3; i++)
scanf_s("%d", &input[i]);
for (int i = 0; i < 3; i++)
{
if (Need[m][i] < input[i]) {
printf("所申请的资源超过了该进程所需资源范围,不予分配\n");
f = 1;
break;
}
if (input[i] > Resource[i])
{
printf("所需资源大于系统声明资源\n");
}
if (input[i] > Work[i]) {
printf("尚无足够资源,进程需等待\n");
f = 2;
break;
}
}
if (f == 0)
{
num = 0;
for (int j = 0; j < 5; j++)
{
sequence[j] = -1;
boolen[j] = 0;
Finish[j] = false;
}
for (int i = 0; i < 3; i++) {
Work[i] = Work[i] - input[i];
now[i] = Work[i];
Allocation[m][i] = Allocation[m][i] + input[i];
Need[m][i] = Need[m][i] - input[i];
}
Sequence();
display2();
for (int i = 0; i < 5; i++)
{
if (sequence[i] == -1)
is_safe = 0;
}
if (is_safe)
{
for (int i = 0; i < 3; i++) {
Available[i] -= input[i];
Allocation[m][i] += input[i];
Need[m][i] -= input[i];
}
display();
}
else
printf("不安全,不予分配资源");
}
}
void Release()
{
int f = 0;
int input[3];
int m;
printf("请输入您想操作的进程(0,1,2,3,4):");
scanf_s("%d", &m);
printf("请输入您想释放的各资源的个数(0,1,2):");
for (int i = 0; i < 3; i++)
scanf_s("%d", &input[i]);
for (int i = 0; i < 3; i++)
{
if (input[i] > Allocation[m][i]) {
printf("大于该进程所释放资源");
f = 1;
break;
}
}
if (f == 0)
{
num = 0;
for (int i = 0; i < 3; i++)
{
Work[i] = Work[i] + input[i];
now[i] = Work[i];
Allocation[m][i] = Allocation[m][i] - input[i];
Need[m][i] = Need[m][i] + input[i];
Available[i] += input[i];
}
display();
}
}
int main()
{
int m;
display();
Sequence();
display2();
while (1)
{
printf("请选择操作(1为申请资源,2为释放资源,3为退出)");
scanf_s("%d", &m);
if (m == 1)
Request();
if (m == 2)
Release();
if (m == 3)
break;
}
return 0;
}
实验心得
本次实验我根据课本上的内容设置了五个进程,三种资源,采用银行家算法,通过安全性检验判断能否申请资源,通过实践我认为以下几点需要细细思考:
①如何判断安全队列是否存在?
这一点我采取了递归的办法,设置一个队列数组(初值全为-1),判断进程所需资源是否小于可用资源,如果小于就把这个进程插入到队列里,并将其判断位记为1(原本为0),在再次递归的过程中进行判断,如果其判断位为1就直接跳过这个进程,判断其他进程能否插入安全队列中,最后判断队列数组是否还有-1的值存在,如果有就证明有进程没有插入这个安全队列,代表有进程的资源情况得不到满足,此刻的安全队列是不存在的,反之则存在。
②如何判断能否申请资源?
首先拿要申请的资源和该进程所需资源比较,前者如果大于后者那么就报出信息,不予分配,在前者小于等于后者的情况下将要申请的资源和此刻可调度资源作比较,如果前者大于后者,那么就让该进程等待一会,如果要申请的资源大于总资源,那么直接报出不可分配的信息,如果都不是就将该进程所占资源加上申请资源,可调度资源减去申请资源,该进程所需资源减去申请资源,再次判断安全队列是否存在,如果存在那么证明进程是安全的,允许申请资源,否则就报错,不予分配。
③如何实现释放资源的功能?
释放资源相比申请资源简单的地方在于不用判断那么多条件,它只需要满足被操作进程的资源大于等于释放的资源数即可,如果满足这个条件我们就用该进程所占资源减去释放资源,可调度资源加上释放资源,该进程所需资源加上释放资源。
④有哪些收获?
本次实验不太理想的地方在于判断安全序列是否存在时,如果该序列不存在没有展示是哪个进程出了问题,只是整个报错表明没有安全序列,这是可以再完善的地方,还有就是可以添加一个查阅进程情况的功能,而不是操作一次才展示一次,本次实验耗费时间较长,但是很有意义,纠正了我对银行家算法的一些错误理解,不仅局限于书本,对此有了更深刻的印象。