死锁检测///summary///死锁检测解除////summaryprivatevoidCheck(){boolpossible=true;ArrayListArrLock=newArrayList();//用于存放死锁进程索引号for(inti=0;iClaim.Length;i++){ArrLock.Add(i);}bool[]finish={true,true,true,true,true};StructionCurrentaviail=newStruction();//当前每类资源可用的数量Currentaviail.P1=Available.P1;Currentaviail.P2=Available.P2;Currentaviail.P3=Available.P3;for(inti=0;iAllocation.Length;i++){if(Allocation[i].P1==0&&Allocation[i].P2==0&&Allocation[i].P3==0){finish[i]=true;ArrLock.Remove(i);}else{finish[i]=false;}}while(possible){intn=0;for(inti=0;iAllocation.Length;i++){if(finish[i]==false&&Claim[i].P1-Allocation[i].P1=Currentaviail.P1&&Claim[i].P2-Allocation[i].P2=Currentaviail.P2&&Claim[i].P3-Allocation[i].P3=Currentaviail.P3){Currentaviail.P1+=Allocation[i].P1;Currentaviail.P2+=Allocation[i].P2;Currentaviail.P3+=Allocation[i].P3;finish[i]=true;ArrLock.Remove(i);n++;}}if(n==0){possible=false;}}if(ArrLock.Count!=0){stringstr=;for(intj=0;jArrLock.Count;j++){str+=+ArrLock[j].ToString();}MessageBox.Show(产生死锁的进程为+str+确定解除?);//messagetext.Text=死锁!!!产生死锁的进程为+str+系统尝试解除中。;Getout(ArrLock);}}///summary///释放死锁进程资源////summary///paramname=ArraLock/paramprivatevoidGetout(ArrayListArraLock){stringstr=;for(inti=0;iArraLock.Count;i++){str+=+ArraLock[i].ToString();intindex=int.Parse(ArraLock[i].ToString());Available.P1+=Allocation[index].P1;Available.P2+=Allocation[index].P2;Available.P3+=Allocation[index].P3;Allocation[index].P1=0;Allocation[index].P2=0;Allocation[index].P3=0;Claim[index].State=3;//进程挂起,但不撤销}//messagetext.Text=系统已经将进程+str+施放资源,并且挂起!!;}