C语言银行家算法实现 链表实现
两天写完的,更改痕迹还没删,可能不是很严谨,有不足的地方欢迎更正。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#define N 10
#define LEN sizeof(PCB)
/*typedef struct Ziyuan
{
int a[N];
int b;
}Zy;*/
/*typedef struct Juzhen
{
Zy max;//最大资源
Zy allocation;//已分配
Zy need;
}Jz;*/
int z = 3;
int m = 5;
int available[N];
int request[N];
int copy[N];
int copy2[N];
typedef struct PCB
{
char name;
int max[N];//最大资源
int allocation[N];//已分配
int need[N];
int biaoji;//完成标记
struct PCB* up;
struct PCB* next;
}PCB;
PCB* safe[N];
PCB* creat() {
m = 0;
char name;
char k = '0';
int alloction,need;
PCB* head, *p1, *p2;
head = p1 = p2 = NULL;
head = (PCB*)malloc(LEN);
if (head)head->next = NULL;
p2 = head;
while (true) {
//int lei = 1;
printf("进程名(输入0结束):");
scanf_s("%c", &name, sizeof(name));
if (name == k) {
break;
}else {
p1 = (PCB*)malloc(LEN);
if(p1)p1->name = name;
if(p1)p1->biaoji = 0;
m++;//进程个数
printf("\nallocation\n");
for (int i = 0; i < z; i++) {
printf("第%d类资源:", i + 1);
scanf_s("%d", &alloction);
p1->allocation[i] = alloction;
}
printf("need\n");
for (int i = 0; i < z; i++) {
printf("第%d类资源:", i + 1);
scanf_s("%d", &need);
p1->need[i] = need;
}
getchar();
if (p2)p2->next = p1;
if (p1)p1->up = p2;
if (p1)p1->next = NULL;
p2 = p1;
}
}
return head;
}
/*void cp1() {//备份更改后
for (size_t i = 0; i < z; i++)
{
copy[i] = available[i];
}
}
void cp2() {//备份更改前
for (size_t i = 0; i < z; i++)
{
copy2[i] = available[i];
}
}
void hf1() {//恢复更改后
for (size_t i = 0; i < z; i++)
{
available[i] = copy[i];
}
}
void hf2() {//恢复更改前
for (size_t i = 0; i < z; i++)
{
available[i] = copy2[i];
}
}*/
void hhh() {
printf("*******\navailable:");
for (size_t i = 0; i < z; i++)
{
printf("%d ",available[i]);
}
printf("\n");
}
void xs(PCB* tou) {
PCB* u, * n;
u = tou;
n = tou->next;
printf("进程\talloction\tneed\n");
while (n) {
printf("%c\t", n->name);
for (size_t i = 0; i < z; i++)//unsigned int
{
printf("%d ",n->allocation[i]);
}
printf("\t");
for (size_t i = 0; i < z; i++)
{
printf("%d ", n->need[i]);
}
printf("\n");
n = n->next;
}
}
PCB* cx(PCB* tou,char x) {
PCB* p = tou;
int i = 0;
if (p) {
p = tou->next;
while (p)
{
if (p->name == x) {
printf("已找到该进程:%c\n",p->name);
i = 1;
break;
}
p = p->next;
}
}
if (i == 1) {
return p;
}
else {
printf("未找到进程\n");
return NULL;
}
}
void huisu(PCB* tou,PCB *k){
int b = 0;
int wanchen = 0;
PCB* qq = k;
PCB* l = tou->next;
for (size_t i = 0; i < z; i++)
{
if (request[i] > qq->need[i])b = 1;//请求大于需求
if (request[i] > available[i])b = 2;//请求大于系统空闲
}
// cp2();
if (b == 0) {
printf("试分配成功,正在寻找安全序列......\n");
for (size_t i = 0; i < z; i++)
{
k->allocation[i] = k->allocation[i] + request[i];
k->need[i] = k->need[i] - request[i];
available[i] = available[i] - request[i];
}
// cp1();//备份更改后
while (l)
{
if (l->biaoji == 0) {//未完成进程才比较资源
int leijia = 0;
for (size_t i = 0; i < z; i++)
{
if (available[i] >= l->need[i])leijia++;
}
if (leijia == 3)
{
for (size_t i = 0; i < z; i++)
{
available[i] = available[i] + l->allocation[i];//完成进程并回收已分配的程序
}
l->biaoji = 1;
safe[wanchen] = l;
wanchen++;
l = tou;
}
}
l = l->next;
}
}
if (wanchen != m) {//没有都完成
for (int i = wanchen - 1; i >= 0; i--)
{
for (size_t j = 0; j < z; j++)
{
available[j] = available[j] - safe[i]->allocation[j];
printf("1");
}
}
if (b == 0) {
for (size_t i = 0; i < z; i++)
{
k->allocation[i] = k->allocation[i] - request[i];
k->need[i] = k->need[i] + request[i];
available[i] = available[i] + request[i];
}
}
// hhh();
// hf2();
printf("寻找安全序列失败,已回溯到请求前\n");
hhh();//显示当前空闲资源
}
else
{
printf("分配成功!\n安全序列为:");
for (size_t i = 0; i < m; i++)
{
printf("%c",safe[i]->name);
}
// hf1();//恢复
printf("\n");
hhh();
}
}
int main() {
PCB* h = NULL;
PCB* chax = NULL;
char jc;
printf("资源个数:");
scanf_s("%d",&z);
getchar();
h = creat();
xs(h);
printf("当前系统空闲资源:\n");
for (size_t i = 0; i < z; i++)
{
printf("第%d类资源:", i + 1);
scanf("%d",&available[i]);
}
while (true)
{
getchar();
printf("需要请求资源的进程名:(0结束)");
scanf_s("%c", &jc, sizeof(jc));
if (jc == '0')break;
chax = cx(h, jc);
//while (chax){ }
if (chax) {
printf("增量请求资源:");
for (size_t i = 0; i < z; i++)
{
printf("第%d类资源:", i + 1);
scanf("%d", &request[i]);
}
huisu(h, chax);
xs(h);
}
else
{
printf("11111111");
xs(h);
}
}
}```