银行家算法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(); }
轻云互联成立于2018年的国人商家,广州轻云互联网络科技有限公司旗下品牌,主要从事VPS、虚拟主机等云计算产品业务,适合建站、新手上车的值得选择,香港三网直连(电信CN2GIA联通移动CN2直连);美国圣何塞(回程三网CN2GIA)线路,所有产品均采用KVM虚拟技术架构,高效售后保障,稳定多年,高性能可用,网络优质,为您的业务保驾护航。活动规则:用户购买任意全区域云服务器月付以上享受免费更换IP服...
野草云服务商在前面的文章中也有多次提到,算是一个国内的小众服务商。促销活动也不是很多,比较专注个人云服务用户业务,之前和站长聊到不少网友选择他们家是用来做网站的。这不看到商家有提供香港云服务器的优惠促销,可选CN2、BGP线路、支持Linux与windows系统,支持故障自动迁移,使用NVMe优化的Ceph集群存储,比较适合建站用户选择使用,最低年付138元 。野草云(原野草主机),公司成立于20...
Advinservers,国外商家,公司位于新泽西州,似乎刚刚新成立不久,主要提供美国和欧洲地区VPS和独立服务器业务等。现在有几款产品优惠,高达7.5TB的存储VPS和高达3.5TBDDoS保护的美国纽约高防服务器,性价比非常不错,有兴趣的可以关注一下,并且支持Paypal付款。官方网站点击直达官方网站促销产品第一款VPS为预购,预计8月1日交付。CPU为英特尔至强 CPU(X 或 E5)。官方...
银行家算法c为你推荐
建行手机网站怎么下载建行手机银行oledbdatareader根据输入信息读取数据库中的数据,总是在执行OleDbDataReader reader = cmd.ExecuteReader()时通不过,excel大写金额EXCEL金额大写listviewitem安卓如何添加Listview的item?netbios协议tcp/ip、ipx/spx、netbios这三个通信协议的区别selectintoACCESS中提示“不允许在select into语句中使用多值字段”gas是什么意思gc是什么意思啊?gas是什么意思petrol和gas的区别ocr软件下载哪个图片转文字软件比较好用?怎么查微信注册时间微信号用了多久,怎么知道
虚拟主机评测网 域名反查 中国十大域名注册商 西安服务器租用 中文域名申请 服务器评测 流媒体服务器 美国php空间 论坛空间 建立邮箱 国外ip加速器 域名与空间 智能dns解析 国外的代理服务器 华为云建站 中国联通宽带测速 阿里云邮箱个人版 中国电信宽带测速 聚惠网 镇江高防服务器 更多