银行家算法实验报告.doc_第1页
银行家算法实验报告.doc_第2页
银行家算法实验报告.doc_第3页
银行家算法实验报告.doc_第4页
银行家算法实验报告.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

银行家算法实验报告实验目的:了解和掌握银行家算法,进一步熟悉进程和如何避免死锁。实验内容:1、主要程序结构(a)resource allocation algorithm nest.allocl*+request*nest.claiml* error /如果要求的资源量+占有的资源总的资源需求量requestinest.availablei结束 N Ynest.allocl*=nest.allocl*+request*;nest.vailable*=nest.available*-request*; NSafe(nest)=truesuspend N t=true Y /t=true可以carry out allocation结束(b) test for safety algorithm 注:- 中的内容为未具体写明但说明其功能的程序段bool safe(struct state new )bool possible = true,found=false;process restn;/正在执行的进程号int p=n;/ 正在执行的进程数量int currentavailm;/可用资源的一个副本-为rest和currentavail赋值-while(possible=true&p0)int g=0; -寻找可以执行完成的进程-if(found=true)int c=0; -假设可以执行完的进程以完成,及currentavail*=currentavail*+new.allocg*;-restg = -1;-p;found =false;elsepossible=false;return possible;2、实验步骤1)设计结构体#ifndef STATE_H_#define m 3#define n 5struct stateint resourcem;int availablem;int claimnm;int allocnm; ;#define STATE_H_#endif /* STATE_H_ */2)设计实现程序的结构的具体程序Safe算法的具体代码bool safe(struct state new )bool possible = true,found=false;process restn; /正在执行的进程号int f=0,p=n;/ 正在执行的进程数量dorestf=f;+f;while(fn);int currentavailm; /可用资源的一个副本int i=0;docurrentavaili=new.availablei;+i;while(i0)int g=0;/*-寻找可以执行完成的进程-*/while(gn)int h = 0; /*while(hm)if(new.claimgh-new.allocgh)=currentavailh&restg!=(-1)if(h=m-1)found=true;+h;if(found=true)break;+g;/*-*/if(found=true)int c=0;while(cm)currentavailc=currentavailc+new.allocgc;c+;restg = -1;-p;found =false;elsepossible=false;return possible;实验代码:/* = Name : pre.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style = */#include #include #includestate.h#define bool int#define process int#define false 0#define true 1int main(void) bool alloc(int,int,struct state );struct state sta1=10,5,7,3,3,2,7,5,3,3,2,2,9,0,2,2,2,2,4,3,3,0,1,0,2,0,0,3,0,2,2,1,1,0,0,2;int requestm;int k=0;while(k=0)puts(输入要改变资源的进程号);scanf(%d,&k);puts(输入要改变的资源);int i =0;while(im)scanf(%d,&requesti);i+;if(alloc(k,request,sta1)=true)i=0;puts(输出个进程占用的资源);while(in)int j=0;printf(%d ,i);while(jm)if(i=k)sta1.allockj=requestj+sta1.allockj;sta1.availablej=sta1.availablej-requestj;printf(%d ,sta1.allocij);j+;printf(n);+i;puts(!银行家算法!);return 0;bool alloc(int l,int requestm,struct state nest)bool safe(struct state news);bool t = false;int i=0;while(inest.claimli)puts(!resource request error!);return false;else if(requestinest.availablei)puts(suspend process);return false;elseif(i=m-1)int f=0;while(fm)nest.alloclf=nest.alloclf+requestf;nest.availablef=nest.availablef-requestf;f+;if(safe(nest)=true)t=true;elseputs( no safe suspend process);t=false;i+;return t;bool safe(struct state new )bool possible = true,found=false;process restn;int f=0,p=n;dorestf=f;+f;while(fn);int currentavailm;int i=0;docurrentavaili=new.availablei;+i;while(i0)int g=0;while(gn)int h = 0;while(hm&(new.claimgh-new.allocgh)=currentavailh&restg!=(-1)if(h=m-1)found=true;+h;if(found=t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论