操作系统课程设计:银行家算法与随机分配算法(linux篇)

  #include <stdio.h> typedef struct { int A; int B; int C; }RES; typedef int bool; #define false 0 #define true 1 //系统中所有进程数量 #define PNUMBER 3 //最大需求矩阵 RES Max[PNUMBER]; //已分配资源数矩阵 RES Allocation[PNUMBER]; //需求矩阵 RES Need[PNUMBER]; //可用资源向量 RES Available={0,0,0}; //安全序列 int safe[PNUMBER]; void setConfig() { int i=0,j=0; printf(“================开始手动配置资源==================\n”); //可分配资源 scanf(“%d%d%d”,&Available.A,&Available.B,&Available.C); //最大需求矩阵MAX for (i=0;i<PNUMBER;i++) { scanf(“%d%d%d”,&Max[i].A,&Max[i].B,&Max[i].C); } //已分配矩阵Alloc for (i=0;i<PNUMBER;i++) { scanf(“%d%d%d”,&Allocation[i].A,&Allocation[i].B,&Allocation[i].C); } //需求矩阵 for (i=0;i<PNUMBER;i++) { scanf(“%d%d%d”,&Need[i].A,&Need[i].B,&Need[i].C); } printf(“================结束配置资源==================\n”); } void loadConfig() { FILE *fp1; if ((fp1=fopen(“config.txt”,”r”))==NULL) { printf(“没有发现配置文件,请手动输入!!!\n”); setConfig(); } else{ int i=0; printf(“发现配置文件,开始导入..”); //可分配资源 fscanf(fp1,”%d%d%d”,&Available.A,&Available.B,&Available.C); //最大需求矩阵MAX for (i=0;i<PNUMBER;i++) { fscanf(fp1,”%d%d%d”,&Max[i].A,&Max[i].B,&Max[i].C); } //已分配矩阵Alloc for (i=0;i<PNUMBER;i++) { fscanf(fp1,”%d%d%d”,&Allocation[i].A,&Allocation[i].B,&Allocation[i].C); } //需求矩阵 for (i=0;i<PNUMBER;i++) { fscanf(fp1,”%d%d%d”,&Need[i].A,&Need[i].B,&Need[i].C); } printf(“信息导入完成…..\n”); } } //试探分配 void ProbeAlloc(int process,RES *res) { Available.A -= res->A; Available.B -= res->B; Available.C -= res->C; Allocation[process].A += res->A; Allocation[process].B += res->B; Allocation[process].C += res->C; Need[process].A -= res->A; Need[process].B -= res->B; Need[process].C -= res->C; } //若试探分配后进入不安全状态,将分配回滚 void RollBack(int process,RES *res) { Available.A += res->A; Available.B += res->B; Available.C += res->C; Allocation[process].A -= res->A; Allocation[process].B -= res->B; Allocation[process].C -= res->C; Need[process].A += res->A; Need[process].B += res->B; Need[process].C += res->C; } //安全性检查 bool SafeCheck() { RES Work = Available; bool Finish[PNUMBER] = {false,false,false}; int i; int j = 0; for (i = 0; i < PNUMBER; i++) { //是否已检查过 if(Finish[i] == false) { //是否有足够的资源分配给该进程 if(Need[i].A <= Work.A && Need[i].B <= Work.B && Need[i].C <= Work.C) { //有则使其执行完成,并将已分配给该进程的资源全部回收 Work.A += Allocation[i].A; Work.B += Allocation[i].B; Work.C += Allocation[i].C; Finish[i] = true; safe[j++] = i; i = -1; //重新进行遍历 } } } //如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态 for (i = 0; i < PNUMBER; i++) { if (Finish[i] == false) { return false; } } return true; } //资源分配请求 bool request(int process,RES *res) { //request向量需小于Need矩阵中对应的向量 if(res->A <= Need[process].A && res->B <= Need[process].B && res->C <= Need[process].C) { //request向量需小于Available向量 if(res->A <= Available.A && res->B <= Available.B && res->C <= Available.C) { //试探分配 ProbeAlloc(process,res); //如果安全检查成立,则请求成功,否则将分配回滚并返回失败 if(SafeCheck()) { return true; } else { printf(“安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n”); printf(“正在回滚…\n”); RollBack(process,res); } } else { printf(“安全性检查失败。原因:请求大于可利用资源。\n”); } } else { printf(“安全性检查失败。原因:请求大于需求。\n”); } return false; } //输出资源分配表 void PrintTable() { printf(“===================================资源分配表==================================\n”); printf(“Process Max Allocation Need Available\n”); printf(” A B C A B C A B C A B C\n”); printf(” P0 %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d\n”,Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C); printf(” P1 %2d %2d %2d %2d %2d %2d %2d %2d %2d\n”,Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C); printf(” P2 %2d %2d %2d %2d %2d %2d %2d %2d %2d\n”,Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C); printf(“===============================================================================\n”); } //银行家算法分配 void banker() { int ch; //判断输入的是否是安全状态 PrintTable(); printf(“先检查初始状态是否安全。\n”); if (SafeCheck()) { printf(“系统处于安全状态。\n”); printf(“安全序列是{P%d,P%d,P%d}。\n”,safe[0],safe[1],safe[2]); } else { printf(“系统处于不安全状态。程序将退出…\n”); printf(“执行完毕。\n”); getchar(); return ; } //开始分配 do { int process; RES res; printf(“请依次输入请求分配的进程和对三类资源的请求数量:”); scanf(“%d%d%d%d”,&process,&res.A,&res.B,&res.C); if(process<3 && process>=0){ if (request(process,&res)) { printf(“分配成功。\n”); PrintTable(); printf(“安全序列是{P%d,P%d,P%d}。\n”,safe[0],safe[1],safe[2]); } else { printf(“分配失败。\n”); } printf(“是否继续分配?(Y/N):”); getchar(); ch = getchar(); }else { printf(“输入的进程号0~2\n”); ch = ‘y’; } } while (ch == ‘Y’ || ch == ‘y’); printf(“执行完毕。\n”); } //随机分配算法执行 bool RandRequest(int process,RES *res) { //request向量需小于Available向量 if(res->A <= Available.A && res->B <= Available.B && res->C <= Available.C) { //试探分配 ProbeAlloc(process,res); //判断进程是否执行完,执行完释放资源 if(Max[process].A <= Allocation[process].A && Max[process].B <= Allocation[process].B && Max[process].C <= Allocation[process].C) { printf(“\nP%d 执行完毕,释放所分配的资源…\n”,process); //有则使其执行完成,并将已分配给该进程的资源全部回收 Available.A += Allocation[process].A; Available.B += Allocation[process].B; Available.C += Allocation[process].C; Allocation[process].A = 0; Allocation[process].B = 0; Allocation[process].C = 0; Need[process].A = Max[process].A; Need[process].B = Max[process].B; Need[process].C = Max[process].C; } return true; } else { printf(“分配失败。原因:请求大于可利用资源。\n”); } return false; } //随机分配 void randPatch() { int ch; //判断输入的是否是安全状态 PrintTable(); printf(“先检查初始状态是否安全。\n”); if (SafeCheck()) { printf(“系统处于安全状态。\n”); printf(“安全序列是{P%d,P%d,P%d}。\n”,safe[0],safe[1],safe[2]); } else { printf(“系统处于不安全状态。程序将退出…\n”); printf(“执行完毕。\n”); getchar(); return ; } //开始分配 do { int process; RES res; printf(“请依次输入请求分配的进程和对三类资源的请求数量:”); scanf(“%d%d%d%d”,&process,&res.A,&res.B,&res.C); if (RandRequest(process,&res)) { printf(“分配成功。\n”); PrintTable(); if(!SafeCheck()) { printf(“系统发生死锁。”); getchar(); getchar(); break; } } else { printf(“分配失败。\n”); } printf(“是否继续分配?(Y/N):”); getchar(); ch = getchar(); } while (ch == ‘Y’ || ch == ‘y’); printf(“执行完毕。\n”); } int main() { int x; while(1) { system(“clear”); printf(“===============================================================================\n”); printf(“\t\t\t共享资源分配与银行家算法\n”); printf(“===============================================================================\n”); printf(“\t\t\t 按1.导入配置信息\n”); printf(“\t\t\t 按2.银行家算法\n”); printf(“\t\t\t 按3.随机分配算法\n”); printf(“\t\t\t 按0.退出系统\n”); printf(“===============================================================================\n”); printf(“您输入的是:”); scanf(“%d”,&x); fflush(stdin); system(“clear”); printf(“===============================================================================\n”); printf(“\t\t\t共享资源分配与银行家算法”); if (x == 2) { printf(“\t—银行家算法\n”); }else if(x==3) { printf(“\t—随机分配算法\n”); } printf(“===============================================================================\n”); switch(x) { case 1: { //加载配置文件 loadConfig(); //打印资源分配表 PrintTable(); getchar(); getchar(); };break; case 2: banker();break; case 3: randPatch(); break; case 0: return 0;break; default:printf(“请输入0~1之间的数字\n”); } } return 0; }

    原文作者:andriod2012
    原文地址: https://www.cnblogs.com/andriod-html5/archive/2011/12/29/2539305.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞