📄 1.cpp
字号:
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
typedef struct Max1
{
int m_a;
int m_b;
int m_c;
} Max;
typedef struct Allocation1
{
int a_a;
int a_b;
int a_c;
} Allocation;
typedef struct Need1
{
int n_a;
int n_b;
int n_c;
} Need;
struct Available1
{
int av_a;
int av_b;
int av_c;
} q;
struct pr
{
char name;
Max max;
Allocation allocation;
Need need;
int finishflag;
} p[5];
char na[5];
//*****************************
void init() //读入
{
printf("各进程的NEED:\n");
FILE *fp;
fp = fopen("1.txt", "r+");
for (int i = 0; i < 5; i++)
{
fscanf(fp, "%c,%d,%d,%d,%d,%d,%d\n", &p[i].name, &p[i].max.m_a,
&p[i].max.m_b, &p[i].max.m_c, &p[i].allocation.a_a,
&p[i].allocation.a_b, &p[i].allocation.a_c);
p[i].need.n_a = p[i].max.m_a - p[i].allocation.a_a;
p[i].need.n_b = p[i].max.m_b - p[i].allocation.a_b;
p[i].need.n_c = p[i].max.m_c - p[i].allocation.a_c;
printf("%c:%d,%d,%d\n", p[i].name, p[i].need.n_a, p[i].need.n_b,
p[i].need.n_c);
}
fclose(fp);
}
//*****************************
int fenpei() //分配
{
printf("Available:\n");
printf("%d,%d,%d\n", q.av_a, q.av_b, q.av_c);
int finishcnt = 0, k = 0, count = 0;
for (int j = 0; j < 5; j++)
{
p[j].finishflag = 0;
}
while (finishcnt < 5)
{
for (int i = 0; i < 5; i++)
{
if (p[i].finishflag == 0 && q.av_a >= p[i].need.n_a && q.av_b >=
p[i].need.n_b && q.av_c >= p[i].need.n_c)
{
q.av_a += p[i].allocation.a_a;
q.av_b += p[i].allocation.a_b;
q.av_c += p[i].allocation.a_c;
p[i].finishflag = 1;
finishcnt++;
na[k++] = p[i].name;
break;
}
}
count++; //禁止循环过多
if (count > 5)
{
return 0;
}
}
return 1;
}
//*****************************
int shq()
{
int m, i, j, k;
printf("请输入进程号和请求资源!如:0 2 0 2\n");
scanf("%d%d%d%d", &m, &i, &j, &k);
if (i <= p[m].need.n_a && j <= p[m].need.n_b && k <= p[m].need.n_c)
{
if (i <= q.av_a && j <= q.av_b && k <= q.av_c)
{
p[m].allocation.a_a += i;
p[m].allocation.a_b += j;
p[m].allocation.a_c += k;
p[m].need.n_a = p[m].max.m_a - p[m].allocation.a_a;
p[m].need.n_b = p[m].max.m_b - p[m].allocation.a_b;
p[m].need.n_c = p[m].max.m_c - p[m].allocation.a_c;
printf("各进程的NEED:\n");
for (int w = 0; w < 5; w++)
{
printf("%c:%d,%d,%d\n", p[w].name, p[w].need.n_a, p[w].need.n_b,
p[w].need.n_c);
}
return 1;
}
else
{
printf("Request>Available\n让%c等待......\n", p[m].name);
}
}
else
{
printf("Request>Need\n让%c等待......\n", p[m].name);
}
return 0;
}
//*****************************
void main()
{
int flag;
char c;
cout << "\n\n\n\n\n\n\n\n\n\t ";
for (int i = 0; i < 30; i++)
{
cout << "*";
}
cout << endl << "\t" << " * 银 行 家 算 法 演 示 *" << endl;
cout << endl << "\t" << " * \001 大汉 \001 *"
<< endl;
printf("\t ");
for (i = 0; i < 30; i++)
{
printf("*");
}
printf("\n\n\n\n\n");
getch();
printf("\t 请确认已经在\"1.txt\"中正确输入各进程的有关信息\n");
getch();
init();
q.av_a = 10;
q.av_b = 5;
q.av_c = 7;
while (flag)
{
for (i = 0; i < 5; i++)
{
q.av_a -= p[i].allocation.a_a;
q.av_b -= p[i].allocation.a_b;
q.av_c -= p[i].allocation.a_c;
}
if (fenpei())
{
printf("\n这样配置资源是安全的!\001_\001\n");
printf("其安全序列是: ");
for (int k = 0; k < 5; k++)
{
printf("-->%c", na[k]);
}
printf("\n");
printf("有进程发出Request请求向量吗???(Enter y or Y)\n");
c = getch();
if (c == 'y' || c == 'Y')
{
if (shq())
{
continue;
}
else
{
break;
}
}
else
{
flag = 0;
}
}
else
{
flag = 0;
printf("不安全\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -