📄 死锁检测.txt
字号:
实验四、 死锁检测算法的实现
实验目的:掌握银行家算法
实验目录: /home/b32/b32019/huangdongkai/experiment4
实验内容:假定某系统有10类资源,编程实现死锁的检测算法。
1) 分别读入5个进程的资源总需求,以及各个进程现占有的资源情况;
2) 生成系统可用资源向量;
3) 判断此状况下系统是否安全,如安全打印出安全序列。
为了方便,我的程序使用了A,B,C三个资源来模拟课本P97例子:
源程序如下: (Banker.c )
#i nclude
#define PROCESS 5
#define COURCE 3
int findp = 0;
int Done[5];
int Available[COURCE];
int Allocation[PROCESS][COURCE];
int Work[COURCE];
int Need[PROCESS][COURCE];
bool Finish[PROCESS];
bool done();
void readInit(void)//初始化
{
printf("input the Allocation of each Process:\n");
for(int i = 0;i {
printf("Process %d : ",i+1);
for(int j = 0;j {
scanf("%d",&Allocation[i][j]);
}
}
printf("input the Need of each Process:\n");
for(int i = 0;i {
printf("Process %d : ",i+1);
for(int j = 0;j {
scanf("%d",&Need[i][j]);
}
Finish[i] = false;
}
printf("input the Available array :\n");
for(int j = 0;j {
scanf("%d",&Available[j]);
}
}
void findProcess(int n)//查找安全序列
{
int i = n;
for(int j = 0;j<3;j++)
{
Work[j] = Available[j];
}
while(!done()||(n/5) <=5)
{
if(Finish[i] == false&&
Need[i][0]<=Work[0]&&
Need[i][1]<=Work[1]&&
Need[i][2]<=Work[2])
{
Work[0] += Need[i][0];
Work[1] += Need[i][1];
Work[2] += Need[i][2];
Finish[i] = true;
Done[findp] = i;
findp ++;
}
i = (i+1)%5;
n++;
}
}
bool done()
{
if(Finish[0] == true
&& Finish[1] == true
&& Finish[2] == true
&& Finish[3] == true
&& Finish[4] == true
)
return true;
else return false;
}
int * list()//返回安全序列
{
int i = 0;
while(!done()&&i<5)
{
findProcess(i);
i++;
}
return Done;
}
int main()
{
readInit();
int *getlist;
getlist = list();
printf("get the safe process list:\n");//打印安全序列
for(int i = 0;i<5;i++)
{
printf("P%d ",getlist[i]);
}
printf("\n");
return 0;
}
运行结果为如下:
input the Allocation of each Process:
Process 1 : 0 1 0
Process 2 : 2 0 0
Process 3 : 3 0 2
Process 4 : 2 1 1
Process 5 : 0 0 2
input the Need of each Process:
Process 1 : 7 4 3
Process 2 : 1 2 2
Process 3 : 6 0 0
Process 4 : 0 1 1
Process 5 : 4 3 1
input the Available array :
3 3 2
get the safe process list:
P1 P3 P4 P0 P2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -