计算机四级银行家死锁预防策略 银行家检测算法怎么算

北京邮电大学相关信息:
北京邮电大学论坛新帖:
考研帮最新资讯
北京邮电大学热点文章
北京邮电大学热门话题
课程预告,帮学堂出品
考研帮地方站银行家算法判断安全序列是小于零还是小于等于零?_计算机四级吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0可签7级以上的吧50个
本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:1,109贴子:
银行家算法判断安全序列是小于零还是小于等于零?
四级解读真题,详解考试题型与应试技巧.听力,阅读,写作,全面提升四级综合实力助你击破听说读写.签约班+名师指导,一站式学习服务,英语四级,备战高分!
这题要怎么做啊?
能不能把过程写下来拍给我一下,谢谢啦
贴吧热议榜
使用签名档&&
保存至快速回贴操作系统课设(模拟实现银行家算法实现死锁避免)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
操作系统课设(模拟实现银行家算法实现死锁避免)
上传于||文档简介
&&比​较​详​细​吧
阅读已结束,如果下载本文需要使用2下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩8页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢模拟死锁检测算法 1. 输入: &资源分配表&文件,每一行包含资源编号、进程编号两项(均用整数表示,并用空格分隔开),记录资源分配给了哪个进程。 &进程等待表&文件,每一行包含进程编号、资源编号两项(均用整数表示,并用空格分隔开),记录进程正在等待哪个资源。 下面是一个示例: 资源分配表: 1 1 2 2 3 3 进程等待表: 1 2 2 3 3 1 2. 处理要求: 程序运行时,首先提示&请输入资源分配表文件的文件名:&;再提示&请输入进程等待表文件的文件名:&。 输入两个文件名后,程序将读入两个文件中的有关数据,并按照死锁检测算法进行检测。 3. 输出要求: 第一行输出检测结果:有死锁 或 无死锁。 第二行输出进程循环等待队列,即进程编号(如果有死锁)。 4. 文件名约定 提交的源程序名字:resourceXXX.c或者resourceXXX.cpp(依据所用语言确定) 输入文件名字:可由用户指定 结果输出到resultXXX.txt中 其中:XXX为账号。 5. 死锁检测算法:当任一进程Pj申请一个已被其他进程占用的资源ri时,进行死锁检测。检测算法通过反复查找进程等待表和资源分配表, 来确定进程Pj对资源ri的请求是否导致形成环路,若是,便确定出现死锁。 6. 测试说明:测试教师将事先准备好一组文件(格式为*.txt),从中为每个程序随机指定一至三个作为输入文件 (被测试者需从键盘输入指定文件的文件名),并查看程序输出结果。
死锁检测算法
#include&stdio.h& #include&iostream.h& #include&string.h& const int MAXQUEUE=100; //定义表的最大行数 typedef struct node{ int int } cell occupy[MAXQUEUE]; int occupy_ cell wait[MAXQUEUE]; int wait_ //初始化函数 void initial() { int for(i=0;i&MAXQUEUE;i++){ occupy[i].process=-1; occupy[i].resource=-1; wait[i].process=-1; wait[i].resource=-1; } occupy_quantity=0; wait_quantity=0; } //读数据文件 int readData() { FILE * char fname[20]; int cout&&"请输入资源分配表文件的文件名:"&& strcpy(fname,"10trouble1.txt"); //cin&& if((fp=fopen(fname,"r"))==NULL){ cout&&"错误,文件打不开,请检查文件名:)"&& return 0; } else{ while(!feof(fp)){ fscanf(fp,"%d %d",&occupy[occupy_quantity].resource,&occupy[occupy_quantity].process); occupy_quantity++; } } cout&&"请输入进程等待表文件的文件名:"&& strcpy(fname,"10trouble2.txt"); //cin&& if((fp=fopen(fname,"r"))==NULL){ cout&&"错误,文件打不开,请检查文件名:)"&& return 0; } else{ while(!feof(fp)){ fscanf(fp,"%d %d",&wait[wait_quantity].process,&wait[wait_quantity].resource); wait_quantity++; } } //输出所读入的数据 cout&&endl&&endl&&"输出所读入的数据"&& cout&&"━━━━━━━━━━━━━━━━━━━━━━━"&& cout&&"资源分配表"&& cout&&"资源编号 进程编号"&& for(i=0;i&occupy_i++){ cout&&" "&&occupy[i].resource&&" "&&occupy[i].process&& } cout&&"───────────────────────"&& cout&&"进程等待表"&& cout&&"进程编号 资源编号"&& for(i=0;i&wait_i++){ cout&&" "&&wait[i].resource&&" "&&wait[i].process&& } return 1; } //检测 void check() { int table[MAXQUEUE][MAXQUEUE]; int table1[MAXQUEUE][MAXQUEUE]; int i,j,k; int flag,t,p; int max_ //初始化表格 for(i=0;i&MAXQUEUE;i++){ for(j=0;j&MAXQUEUE;j++){ table[i][j]=0; table1[i][j]=0; } } //先找到进程最大编号 max_process=-1; for(i=0;i&occupy_i++){ if(occupy[i].process&max_process){ max_process=occupy[i]. } } for(i=0;i&wait_i++){ if(wait[i].process&max_process){ max_process=wait[i]. } } for(i=0;i&wait_i++){ for(j=0;j&occupy_j++){ if(wait[i].resource==occupy[j].resource){ table[wait[i].process][occupy[j].process]=1; table1[wait[i].process][occupy[j].process]=1; } } } cout&&"初始等待占用表:"&& for(i=0;i&max_process+1;i++){ for(j=0;j&max_process+1;j++){ cout&&table[i][j]&&" "; } cout&& } cout&& for(i=0;i&max_process+1;i++){ for(j=0;j&max_process+1;j++){ for(k=0;k&max_process+1;k++){ table[i][j]=table[i][j]||(table[i][k]&&table[k][j]); } } } cout&&"检测后的等待占用表:"&& for(i=0;i&max_process+1;i++){ for(j=0;j&max_process+1;j++){ cout&&table[i][j]&&" "; } cout&& } flag=-1; for(i=0;i&max_process+1;i++){ if(table[i][i]==1){ flag=i; break; } } cout&&endl&&endl&&"检测结果"&& cout&&"───────────────────"&& if(flag!=-1){ cout&&"存在死锁"&& cout&&"进程循环等待队列:"; p= //存在进程循环等待队列的那一进程 //进程循环等待队列中的所有进程是table表中的这一行是1的进程,只是顺序要再确定 t=1; while(t){ cout&&p&&" "; for(j=0;j&max_process+1;j++){ if(table1[p][j]==1){ if(table[j][flag]==1){ p=j; break; } } } if(p==flag)t=0; } cout&&flag&& } else{ cout&&"不存在死锁"&& } } //显示版权信息函数 void version() { cout&&endl&& cout&&" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"&& cout&&" ┃      死 锁 检 测 算 法         ┃"&& cout&&" ┠───────────────────────┨"&& cout&&" ┃   (c)All Right Reserved Neo       ┃"&& cout&&" ┃                ┃"&& cout&&" ┃     version 2004 build 1122      ┃"&& cout&&" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"&& cout&&endl&& } void main() { int version(); initial(); flag=readData(); if(flag)check(); }
&操作系统十大算法摘自:
阅读(...) 评论()避免死锁之银行家算法
1.安全序列
讲银行家算法之前,我们首先引入安全序列的定义:所谓是安全的,是指系统中的所有进程能够按照某一种次序分配资源,并且依次地运行完毕,这种进程序列{P1,P2,...,Pn}就是安全序列。如果存在这样一个安全序列,则系统是安全的;如果系统不存在这样一个安全序列,则系统是不安全的。
安全序列{P1,P2,...,Pn}是这样组成的:若对于每一个进程Pi,它需要的附加资源可以被系统中当前可用资源加上所有进程Pj当前占有资源之和所满足,则{P1,P2,...,Pn}为一个安全序列,这时系统处于安全状态,不会进入死锁状态。  
虽然存在安全序列时一定不会有死锁发生,但是系统进入不安全状态(四个死锁的必要条件同时发生)也未必会产生死锁。当然,产生死锁后,系统一定处于不安全状态。
2.银行家算法
(为了熟悉英语请原谅我借用wiki上的文字来描述) For the Banker's algorithm to work, it needs to know three things:
How much of each resource each process could possibly request[CLAIMS]How much of each resource each process is currently holding[ALLOCATED]How much of each resource the system currently has available[AVAILABLE] Resources may be allocated to a process only if it satisfies the following conditions:
request & max, else set error condition as process has crossed maximum claim made by it.request & available, else process waits until resources are available.
Basic data structures to be maintained to implement the Banker's Algorithm:
Available: A vector of length m indicates the number of available resources of each type. If Available[j] = k, there are k instances of resource type Rj available.Max: An n&m matrix defines the maximum demand of each process. If Max[i,j] = k, then Pi may request at most k instances of resource type Rj.Allocation: An n&m matrix defines the number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then process Pi is currently allocated k instance of resource type Rj.Need: An n&m matrix indicates the remaining resource need of each process. If Need[i,j] = k, then Pi may need k more instances of resource type Rj to complete task. Note: Need[i,j] = Max[i,j] - Allocation[i,j].
银行家算法: 设进程i提出请求Request[j],则银行家算法按如下规则进行判断。
(1) 如果Request[j]&Need[i,j],则转向(2),否则认为出错。
(2) 如果Request[j]&Available[j],则转向(3);否则表示尚无足够资源,Pi需等待。
(3) 假设进程i的申请已获批准,于是修改系统状态:
Available[j]=Available[j]-Request[i]
Allocation[i,j]=Allocation[i,j]+Request[j]
Need[i,j]=Need[i,j]-Request[j]
(4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。
安全性检查 (1) 设置两个工作向量Work=Available;Finish[i]=False
(2) 从进程集合中找到一个满足下述条件的进程,
Finish [i]=F
Need[i,j]&Work[j];
如找到,执行(3);否则,执行(4)
(3) 设进程获得资源,可顺利执行,直至完成,从而释放资源。
Work[j]=Work[i]+Allocation[i,j];
Finish[i]=T
go to step 2;
(4) 如所有的进程Finish[i]=true,则表示安全;否则系统不安全。
由于时间不早了就借用下wiki上的实现代码,改天用java实现一遍。
/*PROGRAM TO IMPLEMENT BANKER'S ALGORITHM
--------------------------------------------*/
#include &stdio.h&
int curr[5][5], maxclaim[5][5], avl[5];
int alloc[5] = {0,0,0,0,0};
int maxres[5], running[5], safe=0;
int count = 0, i, j, exec, r, p,k=1;
int main()
printf(&\nEnter the number of processes: &);
scanf(&%d&,&p);
for(i=0;i&p;i++)
running[i]=1;
printf(&\nEnter the number of resources: &);
scanf(&%d&,&r);
for(i=0;i&r;i++)
printf(&\nEnter the resource for instance %d: &,k++);
scanf(&%d&,&maxres[i]);
printf(&\nEnter maximum resource table:\n&);
for(i=0;i&p;i++)
for(j=0;j&r;j++)
scanf(&%d&,&maxclaim[i][j]);
printf(&\nEnter allocated resource table:\n&);
for(i=0;i&p;i++)
for(j=0;j&r;j++)
scanf(&%d&,&curr[i][j]);
printf(&\nThe resource of instances: &);
for(i=0;i&r;i++)
printf(&\t%d&,maxres[i]);
printf(&\nThe allocated resource table:\n&);
for(i=0;i&p;i++)
for(j=0;j&r;j++)
printf(&\t%d&,curr[i][j]);
printf(&\n&);
printf(&\nThe maximum resource table:\n&);
for(i=0;i&p;i++)
for(j=0;j&r;j++)
printf(&\t%d&,maxclaim[i][j]);
printf(&\n&);
for(i=0;i&p;i++)
for(j=0;j&r;j++)
alloc[j]+=curr[i][j];
printf(&\nAllocated resources:&);
for(i=0;i&r;i++)
printf(&\t%d&,alloc[i]);
for(i=0;i&r;i++)
avl[i]=maxres[i]-alloc[i];
printf(&\nAvailable resources:&);
for(i=0;i&r;i++)
printf(&\t%d&,avl[i]);
printf(&\n&);
//Main procedure goes below to check for unsafe state.
while(count!=0)
for(i=0;i&p;i++)
if(running[i])
for(j=0;j&r;j++)
if(maxclaim[i][j] - curr[i][j] & avl[j]){
printf(&\nProcess%d is executing\n&,i+1);
running[i]=0;
for(j=0;j&r;j++) {
avl[j]+=curr[i][j];
printf(&\nThe processes are in unsafe state.\n&);
printf(&\nThe process is in safe state&);
printf(&\nSafe sequence is:&);
for(i=0;i&r;i++)
printf(&\t%d&,avl[i]);
printf(&\n&);
-----------------
Enter the number of resources:4
Enter the number of processes:5
Claim Vector:8 5 9 7
Enter Allocated Resource Table:
Enter Maximum Claim table:
The Claim Vector is: 8 5 9 7
The Allocated Resource Table:
Maximum Claim Table:
Allocated resources: 7 3 7 5
Available resources: 1 2 2 2
Process3 is executing
The process is in safe state
Available vector: 5 2 2 5
Process1 is executing
The process is in safe state
Available vector: 7 2 3 6
Process2 is executing
The process is in safe state
Available vector: 7 3 5 7
Process4 is executing
The process is in safe state
Available vector: 7 5 6 7
Process5 is executing
The process is in safe state
Available vector: 8 5 9 7
---------------------------------------------------------*/
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 预防死锁的银行家算法 的文章

 

随机推荐