问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

银行家算法 c++版

发布网友 发布时间:2023-10-29 13:24

我来回答

1个回答

热心网友 时间:2024-11-15 13:34

我这有c版本的,你可以用extern "C"{ } 来兼容C++。

#include <stdio.h>
#include <conio.h>

typedef struct Available
{
int Avai_Num;
struct Available *next;
}Available; /* 系统当前可用资源,使用链表实现 */

Available *Creat_Available(int R_Num) /* R_Num资源的数目 */
{
int i;
struct Available *p, *q, *head;
q = malloc(sizeof(struct Available));
head = q;
p = q;
printf("Please input Available resource of current systme\n");
for(i = 0; i < R_Num; i++)
{
q = malloc(sizeof(struct Available));
p->next = q;
p = q;
scanf("%d",&p->Avai_Num);
}
p = head;
for(i = 0; i < R_Num; i++)
{
p = p->next;
printf("R[%d]= %d\n",i, p->Avai_Num);
}
return head;
} /* 生成系统当前可用资源链表 */

void free_Available(struct Available *head, int R_Num)
{
struct Available *p, *q;
int i;
q = head;
p = q;
q = q->next;
for(i = 0; i <= R_Num; i++)
{
free(p);
p = q;
q = q->next;
}
}

typedef struct Allocation
{
int Allo_Num;
struct Allocation *next;
}Allocation; /* 进程已分配资源 */

Allocation *Creat_Allocation(int P_Num,int n)
{
int i;
struct Allocation *p, *q, *head;
q = malloc(sizeof(struct Allocation));
head = q;
p = q;
printf("Please input Allocation of P%d\n", P_Num);
for(i = 0; i < n; i++)
{
q = malloc(sizeof(struct Allocation));
p->next = q;
p = q;
scanf("%d",&p->Allo_Num);
}
p = head;
for(i = 0; i < n; i++)
{
p = p->next;
printf("R[%d]= %d\n",i, p->Allo_Num);
}
return head;
} /* 生成进程已分配资源链表 */

void free_Allocation(struct Allocation *head, int R_Num)
{
struct Allocation *p, *q;
int i;
q = head;
p = q;
q = q->next;
for(i = 0; i <= R_Num; i++)
{
free(p);
p = q;
q = q->next;
}
}

typedef struct Max
{
int Max_Num;
struct Max *next;
}Max; /* 进程最大资源 */

Max *Creat_Max(int P_Num,int n)
{
int i;
struct Max *p, *q, *head;
q = malloc(sizeof(struct Max));
head = q;
p = q;
printf("Please input Max of P%d\n", P_Num);
for(i = 0; i < n; i++)
{
q = malloc(sizeof(struct Max));
p->next = q;
p = q;
scanf("%d",&p->Max_Num);
}
p = head;
for(i = 0; i < n; i++)
{
p = p->next;
printf("R[%d]= %d\n",i, p->Max_Num);
}
return head;
} /* 生成进程最大资源链表 */

void free_Max(struct Max *head, int R_Num)
{
struct Max *p, *q;
int i;
q = head;
p = q;
q = q->next;
for(i = 0; i <= R_Num; i++)
{
free(p);
p = q;
q = q->next;
}
}

typedef struct Need
{
int Need_Num;
struct Need *next;
}Need; /* 进程运行结束还需资源 */

Need *Creat_Need(struct Max *M_Head, struct Allocation *A_Head,int P_Num,int

n)
{
int i;
struct Need *p, *q, *head;
struct Max *Max_Temp;
struct Allocation *Allo_Temp;
Max_Temp = M_Head;
Allo_Temp = A_Head;
q = malloc(sizeof(struct Need));
head = q;
p = q;
for(i = 0; i < n; i++)
{
Max_Temp = Max_Temp->next;
Allo_Temp = Allo_Temp->next;
q = malloc(sizeof(struct Need));
p->next = q;
p = q;
p->Need_Num = Max_Temp->Max_Num - Allo_Temp->Allo_Num;
}
p = head;
for(i = 0; i < n; i++)
{
p = p->next;
printf("P_Need R[%d]= %d\n",i, p->Need_Num);
}
return head;
} /* 生成进程运行结束还需资源链表 */
void free_Need(struct Need *head, int R_Num)
{
struct Need *p, *q;
int i;
q = head;
p = q;
q = q->next;
for(i = 0; i <= R_Num; i++)
{
free(p);
p = q;
q = q->next;
}
}

typedef struct Resource
{
struct Allocation *Allo_R;
struct Max *Max_R;
struct Need *Need_R;
struct Resource *next;
int Finish;
}Resource;

Resource *Creat_Resource(int P_Num, int R_Num)
{
int i;
struct Resource *p, *q, *head;
q = malloc(sizeof(struct Resource));
head = q;
p = q;
printf("Creat initial state of Resource \n");
for(i = 0; i < P_Num; i++)
{
q = malloc(sizeof(struct Resource));
p->next = q;
p = q;
p->Allo_R = Creat_Allocation(i,R_Num);
p->Max_R = Creat_Max(i, R_Num);
p->Need_R = Creat_Need( p->Max_R,p->Allo_R, P_Num, R_Num);
p->Finish = 0;
}
p = head;
return head;
} /* 进程资源状况 */

void free_Resource(struct Resource *head, int R_Num)
{
struct Resource *p, *q;
int i;
q = head;
p = q;
q = q->next;
for(i = 0; i <= R_Num; i++)
{
free(p);
p = q;
q = q->next;
}
}

void free_memory(struct Resource *Resource_head, struct Available

*Available_head, int P_Num, int R_Num)
{
struct Resource *p, *q;
int i;
q = Resource_head;
p = q;
q = q->next;
for(i = 0; i <= P_Num; i++)
{
p = q;
q = q->next;
free_Need(p->Need_R, R_Num);
free_Max(p->Max_R, R_Num);
free_Allocation(p->Allo_R, R_Num);
}
q = Resource_head;
p = q;
free_Resource(p, R_Num);
free_Available(Available_head, R_Num);
}

int compair(struct Resource *Compair_Process, struct Available

*Available_Head, int R_Num)
/*比较当前的资源是否能满足进程q的运行,如可以返回1*/
{
struct Need *Current_Resource_Needed;
struct Available *Temp_Available_Resource;
int i;
int temp;
Current_Resource_Needed = Compair_Process->Need_R;
Temp_Available_Resource = Available_Head;
temp = 1;
for(i = 0; i < R_Num; i++)
{
Current_Resource_Needed = Current_Resource_Needed->next;
Temp_Available_Resource = Temp_Available_Resource->next;
temp = temp && (Current_Resource_Needed->Need_Num <=

Temp_Available_Resource->Avai_Num ? 1 : 0);
}
return temp;
}

void change_available(struct Resource *Compair_Process, struct Available

*Available_Head, int R_Num)
{ /*修改进程执行后的系统可用资源表*/
struct Allocation *Current_Resource_Allocated;
struct Available *Temp_Available_Resource;
int i;
Current_Resource_Allocated = Compair_Process->Allo_R;
Temp_Available_Resource = Available_Head;
for(i = 0; i < R_Num; i++)
{
Current_Resource_Allocated = Current_Resource_Allocated->next;
Temp_Available_Resource = Temp_Available_Resource->next;
Temp_Available_Resource->Avai_Num = Current_Resource_Allocated-

>Allo_Num + Temp_Available_Resource->Avai_Num;
}
}
void print_available(struct Available *Available_Head, int R_Num)
{ /*输出系统可用资源表*/
struct Available *Temp_Available_Resource;
int i;
Temp_Available_Resource = Available_Head;
for(i = 0; i < R_Num; i++)
{
Temp_Available_Resource = Temp_Available_Resource->next;
printf("Available_R%d = %d\n", i, Temp_Available_Resource->Avai_Num);
}
}

void Safty_judgement(struct Resource *head, struct Available *Available_Head,

int R_Num)
{ /*系统安全性判断*/
struct Resource *p, *q;

int Safty;
int one_can_finish;
one_can_finish = 1;

printf("\n\n");
while(one_can_finish)
{ /*当这一次执行没有一个进程可以完成,时

退出*/
p = head;
p = p->next;
one_can_finish = 0;
while(p)
{ /*进行一遍搜索*/
if(p->Finish == 1)
p = p->next;
else if(compair(p,Available_Head,R_Num) == 1)
{
change_available(p,Available_Head,R_Num);
p->Finish = 1;
one_can_finish = 1; /*如果有一个进程能完成,那么标识

one_can_finish为1*/
p = p->next;
}
else p = p->next;
}
}
p = head;
p = p->next;
Safty = 1; /*假定当前状态是安全的*/
while(p)
{
if( p->Finish == 0)
Safty = 0; /*如果有一个进程不能完成,那么把状态置

为不安全*/
p = p->next;
}
if(Safty)
printf("The State is safety!");
else
printf("Can not allocate the resource!");
}

main()
{
struct Resource *Process_Head;
struct Available *Available_Head;
int P_Num, R_Num, i;

printf("\n");
printf("Please input Process_Number ");
scanf("%d",&P_Num);
printf("\n");
printf("Please input Resource_Number ");
scanf("%d", &R_Num);
Available_Head = Creat_Available(R_Num);
Process_Head = Creat_Resource(P_Num,R_Num);

Safty_judgement(Process_Head, Available_Head, R_Num);

free_memory(Process_Head, Available_Head, P_Num, R_Num);
getch();
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
结核病是什么样的疾病? 曹丕17岁得了肺痨,明知自己命不长久,还要强争王位,是不是很自私呢?_百... 古代小说常出现的病名 急求一篇"生活小窍门"(500字)的作文 至今最有什么小妙招 健康的戒烟方法 笔记本电池锁死是什么原因引起的? 黑龙江债权转让合同纠纷该怎样取证 安徽债权转让合同纠纷应该怎么样取证 房产官司律师费多少 企业融资的出质,抵押费用应由哪方出? 如何评价安妮海瑟薇在《穿普拉达的女王》中演技? 微信好友怎么迁移到新 CAD里面能不能不改变物体高度,而拉伸物体宽度的?怎么操作?6 被注销怎么找回来? qq飞车里d级赛车图片列表3 动漫【A kite】结局是什么意思112 卡卡西同人文4 朱元璋三十一年的统治留下了一个怎样的江山? 诸暨市行政区划图4 安徽南陵奎湖张氏有多久历史 如何用一个手机号注册多个? 80S影视为什么打不开了? 改一年内怎么改第二次 酷我音乐盒卸载掉了为什么图标删除不了1 酷我音乐盒怎么删除不了7 酷我音乐盒怎么卸载不了啊?3 如何将微信好友转移到另一个上? 爱发脾气的孩子一定是被父母惯坏了吗?孩子爱发脾气家长该如何应对? 婚前存款婚后取存的记录不清晰,还能证明婚前财产吗?9 鲜奶热开后为什么有条丝状能喝吗2 qq飞车里d级赛车图片列表3 诸暨和庄属于什么街道? 绍兴市诸暨市(和庄)东环三路福田花园东侧是什么镇&#47;街道? 诸暨市高湖东路是属于哪个街道的7 怎么在微信里再申请一个 介绍一些好听的歌子?谢谢了,大神帮忙啊 诈骗中心发信息给我要我提醒信息会有事吗 怎么下不了80s了呢? 广州白云区哪里有好点的学校学会计?2 什么是“网民”?7 网民是指? 网民的标准定义是什么?拜托各位大神 网络红人是如何定义的? 安妮海瑟薇在公主日记1里穿的蓝色睡衣是什么牌子 贝爷会被寄生虫感染吗?144 南宁化妆造型摄影学校杂七杂八的,我该咋选择呢?郁闷中!求推荐...4 给化妆师和摄影师好评怎么写?88 HOOMAG赫美娇胶原蛋白每天喝几袋? 海沧跆拳道哪家好