?? 死鎖檢測(cè).txt
字號(hào):
實(shí)驗(yàn)四、 死鎖檢測(cè)算法的實(shí)現(xiàn)
實(shí)驗(yàn)?zāi)康模赫莆浙y行家算法
實(shí)驗(yàn)?zāi)夸? /home/b32/b32019/huangdongkai/experiment4
實(shí)驗(yàn)內(nèi)容:假定某系統(tǒng)有10類資源,編程實(shí)現(xiàn)死鎖的檢測(cè)算法。
1) 分別讀入5個(gè)進(jìn)程的資源總需求,以及各個(gè)進(jìn)程現(xiàn)占有的資源情況;
2) 生成系統(tǒng)可用資源向量;
3) 判斷此狀況下系統(tǒng)是否安全,如安全打印出安全序列。
為了方便,我的程序使用了A,B,C三個(gè)資源來模擬課本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;
}
運(yùn)行結(jié)果為如下:
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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -