银行家算法c银行家算法 c++版
银行家算法c 时间:2022-02-23 阅读:(
)
银行家算法 c++版
我这有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
"); 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
",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
", 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
",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
", 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
",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
",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
"); 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); } pair(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
", 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("
"); while(one_can_finish) { /*当这一次执行没有一个进程可以完成,时 退出*/ p = head; p = p->next; one_can_finish = 0; while(p) { /*进行一遍搜索*/ if(p->Finish == 1) p = p->next; else pair(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("
"); printf("Please input Process_Number "); scanf("%d",&P_Num); printf("
"); 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(); }
racknerd在促销美国洛杉矶multacom数据中心的一款大硬盘服务器,用来做存储、数据备份等是非常划算的,而且线路还是针对亚洲有特别优化处理的。双路e5+64G内存,配一个256G的SSD做系统盘,160T SAS做数据盘,200T流量每个月,1Gbps带宽,5个IPv4,这一切才389美元...洛杉矶大硬盘服务器CPU:2 * e5-2640v2内存:64G(可扩展至128G,+$64)硬...
提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...
欧路云怎么样?欧路云主要运行弹性云服务器,可自由定制配置,可选加拿大的480G超高防系列,也可以选择美国(200G高防)系列,也有速度直逼内地的香港CN2系列。所有配置都可以在下单的时候自行根据项目 需求来定制自由升级降级 (降级按天数配置费用 退款回预存款)。2021年7月14日美国 CERA 弹性云服务器 上新 联通CUVIP 线路!8折特惠中!点击进入:欧路云官方网站地址付款方式:PayPa...
银行家算法c为你推荐
订房系统常用的酒店管理软件有哪些isbackgroundbokeh是什么意思scriptmanagerScriptManager是什么素数算法求100以内的素数的算法excel大写金额在EXCEL表格里怎样自动生成金额大写怎么用电脑发短信怎样用电脑给别人的手机发短信?暴力破解rar求暴力破解rar压缩包软件listviewitem求解如何获取listview中的item国家法规数据库哪个常用的法律APP比较好用?webservice框架如何用webservice 的cxf框架
天津服务器租赁 n点虚拟主机管理系统 已经备案域名 cybermonday 256m内存 表格样式 lamp配置 建站代码 网站在线扫描 阿里云免费邮箱 中国域名 浙江服务器 so域名 侦探online weblogic部署 服务器是什么 ddos攻击小组 主机游戏 dmz主机 次时代主机 更多