死锁的检测与解除.docx

上传人:b****3 文档编号:3895823 上传时间:2023-05-06 格式:DOCX 页数:18 大小:263.17KB
下载 相关 举报
死锁的检测与解除.docx_第1页
第1页 / 共18页
死锁的检测与解除.docx_第2页
第2页 / 共18页
死锁的检测与解除.docx_第3页
第3页 / 共18页
死锁的检测与解除.docx_第4页
第4页 / 共18页
死锁的检测与解除.docx_第5页
第5页 / 共18页
死锁的检测与解除.docx_第6页
第6页 / 共18页
死锁的检测与解除.docx_第7页
第7页 / 共18页
死锁的检测与解除.docx_第8页
第8页 / 共18页
死锁的检测与解除.docx_第9页
第9页 / 共18页
死锁的检测与解除.docx_第10页
第10页 / 共18页
死锁的检测与解除.docx_第11页
第11页 / 共18页
死锁的检测与解除.docx_第12页
第12页 / 共18页
死锁的检测与解除.docx_第13页
第13页 / 共18页
死锁的检测与解除.docx_第14页
第14页 / 共18页
死锁的检测与解除.docx_第15页
第15页 / 共18页
死锁的检测与解除.docx_第16页
第16页 / 共18页
死锁的检测与解除.docx_第17页
第17页 / 共18页
死锁的检测与解除.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

死锁的检测与解除.docx

《死锁的检测与解除.docx》由会员分享,可在线阅读,更多相关《死锁的检测与解除.docx(18页珍藏版)》请在冰点文库上搜索。

死锁的检测与解除.docx

死锁的检测与解除

 

操作系统实验报告2

死锁的检测与解除

 

班级:

计算机科学与技术082班级

姓名:

学号:

老师:

霍林

实验目的:

设计一个m 个并发进程共享n个系统资源的系统。

进程可动态申请资源和释放资源,系统按各进程的申请动态的分配资源。

要求用死锁检测算法检测当某一进程提出资源分配请求时,系统会不会陷入死锁状态;如若陷入死锁状态要求用某种算法解除死锁

实验环境:

WindosXP系统,java编程

实验分析:

本实验采用死锁检测算法检测死锁,死锁检测的数据结构如下

(1)可利用资源向量available,它表示了n类资源中每一类资源的可用数目。

(2)把不占用资源的进程用finish[i]=true标示

(3)从进程集合中找到一个request[i]<=work的进程,做如下处理:

将其资源分配图简化,释放出资源,增加工作向量work:

=work+allocation[i],并将其finish[i]置1

(4)若不能把所有的进程都标记为finish[i]==1,则表明该系统状态将发生死锁。

当发现进程死锁时,便应立即把它们从死锁状态中解脱出来。

本实验采用的解除死锁的方法是:

撤销进程。

本实验采用撤销的进程数目最小的方法,把死锁进程中占有资源最多的进程撤销,如若仍发生死锁,继续撤销下一进程

主要函数说明:

主要的常量变量

finalinti=4最大进程数

finalintj=4最大资源种类数

intAvailable[]可利用资源向量

intMax[][]最大需求矩阵

intAllocation[][]分配矩阵

intNeed[][]需求矩阵

intRequest[][]进程请求向量

intAllcount[]各资源总数

intC[];安全序列

主要模块

voidmain()主函数

publicintSisuo()检测死锁

publicvoidFree()死锁解锁

voidShow()显示函数

 

流程图:

调试结果:

 

实验总结:

通过该实验我发现死锁的检测与解除和银行家算法的数据结构基本相同,死锁解除算法就是撤销某一引起死锁进程所占有资源的释放。

通过此次实验加深了我对对死锁定理的理解,同时对对死锁解除的算法有了进一步的实践。

 

附录:

源程序代码

importjava.io.*;

publicclasst

{

publicstaticvoidmain(Stringargs[])

{

T1ts=newT1();

ts.Putout();

System.out.println();

ts.Show();

if(ts.Sisuo()==1)

{

System.out.println("不会死锁");

}

if(ts.Sisuo()==0)

{

System.out.println("会死锁");

ts.Free();

}

}

}

classT1

{

finalinti=4;//最大进程数

finalintj=4;//最大资源种类数

intdd,tt;

intAvailable[];//可利用资源向量

intMax[][];//最大需求矩阵

intAllocation[][];//分配矩阵

intNeed[][];//需求矩阵

intRequest[][];//进程请求向量

intAllcount[];//各资源总数

intC[];

publicvoidPutout()

{

System.out.println("请依次输入最大可用资源数量:

");

System.out.println();

Allcount=newint[j];

for(intt=0;t

{

try{

System.out.print("资源"+t+":

");

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Strings=br.readLine();

Allcount[t]=Integer.parseInt(s);

System.out.println();

}catch(IOExceptione){}

}

System.out.println("Max:

");

System.out.println();

Max=newint[i][j];

for(intt=0;t

{

System.out.println("P"+t+":

");

System.out.println();

for(intt1=0;t1

{

try{

do{

System.out.print("资源"+t1+":

");

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Strings=br.readLine();

Max[t][t1]=Integer.parseInt(s);

if(Max[t][t1]>Allcount[t1])

{

System.out.println("输入的最大资源数超过了声明的该资源总数!

请重新输入!

");

}

System.out.println();

}while(Max[t][t1]>Allcount[t1]);

}catch(IOExceptione){}

}

}

System.out.println();

System.out.println("Allocation:

");

System.out.println();

Allocation=newint[i][j];

for(intt=0;t

{

System.out.println("P"+t+":

");

System.out.println();

for(intt1=0;t1

{

try{

do{

System.out.print("资源"+t1+":

");

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Strings=br.readLine();

Allocation[t][t1]=Integer.parseInt(s);

if(Allocation[t][t1]>Max[t][t1])

{

System.out.println("输入的已占有资源数超过了声明的最大资源数!

请重新输入!

");

}

System.out.println();

}while(Allocation[t][t1]>Max[t][t1]);

}catch(IOExceptione){}

}

}

System.out.println();

Need=newint[i][j];

for(intt=0;t

for(intt1=0;t1

Need[t][t1]=Max[t][t1]-Allocation[t][t1];

System.out.println("Request:

");

System.out.println();

Request=newint[i][j];

for(intt=0;t

{

System.out.println("P"+t+":

");

System.out.println();

for(intt1=0;t1

{

try{

do{

System.out.print("资源"+t1+":

");

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Strings=br.readLine();

Request[t][t1]=Integer.parseInt(s);

if(Request[t][t1]>Max[t][t1])

{

System.out.println("超过了需求资源数!

请重新输入!

");

}

System.out.println();

}while(Request[t][t1]>Max[t][t1]);

}catch(IOExceptione){}

}

}

Available=newint[j];

for(intt1=0;t1

{

intp1=Allcount[t1];

for(intt=0;t

{

p1=p1-Allocation[t][t1];

Available[t1]=p1;

if(Available[t1]<0)

Available[t1]=0;

}

}

}

publicintSisuo()//死锁检测算法

{

intWork[]=newint[j];

for(intt=0;t

{Work[t]=Available[t];}

intFinish[]=newint[i];

for(intt=0;t

{Finish[t]=0;}

intaa=0;

for(intk=0;k

{

for(intt1=0;t1

{

intcount1=0;

if(Finish[t1]==0)

{

for(intt2=0;t2

{

if(Request[t1][t2]>Work[t2])

{

break;

}

else

{

count1=count1+1;

}

}

if(count1==j)

{

Finish[t1]=1;

for(intt3=0;t3

Work[t3]=Work[t3]+Allocation[t1][t3];

aa++;

}

}

}

}

if(aa==i)

{

return1;

}

else

{

return0;

}

}

publicvoidFree()//死锁解锁算法

{

System.out.println("解除死锁

(1)退出(0)");

do

{

try{

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

tt=Integer.parseInt(br.readLine());

if(tt<0||tt>1)

{

System.out.println("输入错误,重新输入:

");

}

}catch(IOExceptione){}

}while(tt<0||tt>1);

if(tt==1)

{

intWork[]=newint[j];

for(intt=0;t

{Work[t]=Available[t];}

intFinish[]=newint[i];

for(intt=0;t

{Finish[t]=0;}

intaa=0;

for(intk=0;k

{

for(intt1=0;t1

{

intcount1=0;

if(Finish[t1]==0)

{

for(intt2=0;t2

{

if(Request[t1][t2]>Work[t2])

{

break;

}

else

{

count1=count1+1;

}

}

if(count1==j)

{

Finish[t1]=1;

for(intt3=0;t3

Work[t3]=Work[t3]+Allocation[t1][t3];

}

}

}

}

intt=0;

do

{

if(t

{

for(intt1=0;t1

{

Available[t]=Available[t]+Allocation[t][t1];

Allocation[t][t1]=0;

Request[t][t1]=0;

}

Finish[t]=1;

}

t++;

}while(Sisuo()!

=1&&t>i);

System.out.println("死锁解除");

Show();

}

if(tt==0)

{

System.out.println("Bye");

System.exit(0);

}

}

publicvoidShow()

{

System.out.println();

System.out.println("最大可用资源数量:

");

for(intt=0;t

System.out.print(""+Allcount[t]+"");

System.out.println();

System.out.println();

System.out.println("空闲");

for(intt=0;t

System.out.print(""+Available[t]+"");

System.out.println();

System.out.println();

System.out.println("资源"+"Max"+"Allocation"+"Need"+"Request");

System.out.println("进程");

System.out.println("");

for(intt=0;t

{

System.out.print("P"+t+":

");

for(intt1=0;t1

{

System.out.print(Max[t][t1]+"");

}

System.out.print("");

for(intt1=0;t1

{

System.out.print(Allocation[t][t1]+"");

}

System.out.print("");

for(intt1=0;t1

{

System.out.print(Need[t][t1]+"");

}

System.out.print("");

for(intt1=0;t1

{

System.out.print(Request[t][t1]+"");

}

System.out.println();

}

System.out.println("");

System.out.println();

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2