死锁的检测与解除.docx
《死锁的检测与解除.docx》由会员分享,可在线阅读,更多相关《死锁的检测与解除.docx(18页珍藏版)》请在冰豆网上搜索。
死锁的检测与解除
操作系统实验报告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;t1Need[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;t3Work[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;t3Work[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;tSystem.out.print(""+Allcount[t]+"");
System.out.println();
System.out.println();
System.out.println("空闲");
for(intt=0;tSystem.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();
}
}