死锁检测代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

死锁检测
/// <summary>
///死锁检测解除
/// </summary>
private void Check()
{
bool possible = true;
ArrayList ArrLock = new ArrayList(); for(int i=0;i<Claim.Length;i++)
{
//用于存放死锁进程索引号
//当前每类资源可用的数量
ArrLock.Add(i);
}
bool[] finish = { true, true, true, true, true }; Struction Currentaviail = new Struction();
Currentaviail.P1 = Available.P1;
Currentaviail.P2 = Available.P2;
Currentaviail.P3 = Available.P3;
for (int i = 0; i < Allocation.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)
{
int n = 0;
for (int i = 0; i < Allocation.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)
{
string str = "";
for (int j = 0; j < ArrLock.Count; j++)
{
str += " " + ArrLock[j].ToString();
}
MessageBox.Show("产生死锁的进程为"+str+"确定解除?");
//messagetext.Text = "死锁!!!产生死锁的进程为" + str + "系统尝试解除中。

";
Getout(ArrLock);
}
}
/// <summary>
///释放死锁进程资源
/// </summary>
/// <param name="ArraLock"></param>
private void Getout(ArrayList ArraLock)
{
string str = "";
for (int i = 0; i < ArraLock.Count; i++)
{
str += " " + ArraLock[i].ToString();
int index=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+"施放资源,并且挂起!!";
}。

相关文档
最新文档