[OS] Deadlock detection algorithm

data structure.

  • n: integer, the number of processes;
  • m: integer, the number of resource types;
  • Available[m]: A one-dimensional array that stores the number of available instances of each resource in [1...m];
  • Allocation[n][m]: n×m matrix, Allocation[i][j] represents the number of resources j already held by the process i;
  • Request[n][m]: n×m matrix, Request[i][j] represents the number of resource j requested by the process i at this time.

Deadlock detection algorithm.

TestDeadLock()
{
    
    
	//1.
	Work[1..m]=Available[1..m];
	//向量Work初始化为当前可用资源实例数向量.

	Finish[1..n]=false;
	//向量Finish每个分量都为false,表示初态都是未完成.

	//2.
	change=true;	//循环控制变量.
	while(change)
	{
    
    
		//Request[i]表示矩阵的第i行,也就是进程Pi所申请的资
		//源数向量,若进程Pi未完成,并且需求能够得到满足,就执
		//行Pi,后续释放其中的资源,等价于Work获得Pi已经持有
		//的资源数向量Allocation[i].
		change=false;
		for(int i=1;i<=n;++i)
		{
    
    
			if(Finish[i]==false && Request[i]<Work) 
			{
    
    
				Work=Work+Allocation[i];
				Finish[i]=true;
				change=true;
			}
		}
	}
	//不动点算法:只有当Finish和Allocation不再发生改变时,
	//也就意味着所有的进程都已经完成或者剩余进程都无法运行
	//这两种情况之一发生时,才会跳出while循环.

	//3.
	for(int i=1;i<=n;++i)
	{
    
    
		if(Finish[i]==false)
		{
    
    
			return true;
			//只要存在一个进程是false,就代表出现了死锁
			//返回真值表示检测到了死锁
		}
	}
	return false;
}

The difference with the banker's algorithm.

Banker's algorithm.

change=true;
while(change)
{
    
    
	change=false;
	for(int i=1;i<=n;++i)
	{
    
    
		if(Finish[i]==false && Need[i]<Work) 
		{
    
    
			Work=Work+Allocation[i];
			Finish[i]=true;
			change=true;
		}
	}
}

Deadlock detection algorithm.

change=true;
while(change)
{
    
    
	change=false;
	for(int i=1;i<=n;++i)
	{
    
    
		if(Finish[i]==false && Request[i]<Work) 
		{
    
    
			Work=Work+Allocation[i];
			Finish[i]=true;
			change=true;
		}
	}
}

Explanation.

  • Pay attention to the 7th line in the two sets of codes. Compared with the deadlock detection algorithm, the conditions are weaker, and only Request [i] <W ork Request[i]<WorkRequest[i]<W o r k takes back the processP i P_iPiResources. The banker’s algorithm determines that N eed [i] <W ork Need[i]<WorkNeed[i]<W o r k only recovered resources.
  • The purpose of the banker algorithm is to avoid deadlocks, that is, during the entire operation and scheduling process of this group of processes, there can be no deadlocks, so it is necessary to judge whether the process can run completely, that is, to obtain all the resources it needs. .
  • The deadlock detection algorithm only detects whether there is a deadlock at the moment, if Request [i] <W ork Request[i]<WorkRequest[i]<If W o r k is true, it means that the processP i P_iPiAt this moment, it is not involved in the deadlock, so we optimistically assume that the process P i P_iPiIt will not apply for more resources in the future (in many application scenarios, there are such optimistic assumptions), so the resources it holds are directly recovered during the test. Of course, if this optimistic assumption is wrong and a deadlock occurs in the subsequent execution, it will be detected during the execution of the subsequent deadlock detection algorithm.

Guess you like

Origin blog.csdn.net/weixin_44246009/article/details/108548948