Operating system knowledge - bankers algorithm

Description with a language they understand, if wrong, please madness on her face does not matter, want to be able to point it out.

0. The main idea

Before allocating resources to determine whether the system is safe.
Bankers algorithm is a resource allocation and deadlock avoidance algorithms, to test security by simulating allocation of all resources.

Borrowed from the bank lending system allocation strategy.
In banking, the number of customers applying for loans is limited, each client for the first time to apply for loans to declare the completion of the maximum amount of money required for the project, while meeting all the requirements of the loan, the customer should be promptly returned. When the number of loans banker client application does not exceed the maximum own, we should try to meet the needs of customers.
We have the following relationship:

banker operating system
Bank funds system resource
Bank customers To process the application resources

Describe that, in the operating system, the number of process application resources are limited, each process at the time of the first application of resources to the maximum amount of resources required to complete statement of the process, at the end of the process, the process should be timely release of resources . When the amount of resources in the process of applying operating system does not exceed the maximum own, we should try to meet the needs of the process.

1. Data Structure

Suppose n is the number of processes in the system, m is the number of resource types.

(1) Available resources available vector
  • It is a size of m number of available resources of the one-dimensional array, an indication of each type.
  • Wherein the number of each element represents a resource available.
  • As the Available [J] = K , the system represents an existing R & lt J resource of type K number.

Here Insert Picture Description

(2) the maximum resource demand matrix Max
  • It is one of size n * m two-dimensional array, each process defined in the system for the maximum demand for resources.
  • The Max [I, J] = K , said process P I requires R & lt J maximum number of resource type K .

Here Insert Picture Description

(3) Assigned matrix Allocation
  • It is one of size n * m two-dimensional array, used to define the number of each type of resource currently allocated to each process.
  • The Allocation [I, J] = K , said process P I currently share R & lt J number of resources such as K .

Here Insert Picture Description

(4) demand matrix Need
  • It is one of size n * m two-dimensional array, indicating the number of remaining resource requirements of each process.
  • The Need [I, J] = K , said process P I still need R J resource type K months, in order to complete the task.

Here Insert Picture Description
They have the following relationship:

  • Need[i,j] = Max[i,j] - Allocation[i,j]
  • Available_Ti =
  • Need, at least the number of each type of resource is less than a thread equal to the number of various types of Available resources, the system was a safe state.

2. A chestnut

Here Insert Picture Description

2.1. 计算T0 时刻Available矩阵和Need矩阵

T0 时刻系统状态表告诉我们了Max矩阵和Allocation矩阵的情况,由公式 Need[i,j] = Max[i,j] - Allocation[i,j] 可得有Need矩阵的系统状态表如下:

Here Insert Picture Description
系统A、B、C各资源总量为(注意,这不是Available矩阵,它减去已分配资源才是):

A B C
17 5 20

结合Allocation矩阵的情况,得到T0 时刻Available_0矩阵如下:

Here Insert Picture Description
记作Available_0 = [2, 3, 3]
与Need矩阵比较发现,P4和P5各类型资源数目都小于等于Available_0矩阵各类型资源数目,符合要求,所以系统当前为安全状态。

所以接下来,给P4或P5分配资源都可以。以P4为例,先分配给P4。

2.2. 计算T1 时刻(P4释放资源后)Available矩阵

当P4结束,释放资源后,记为T1 时刻,Available矩阵和系统状态表如下:
Here Insert Picture Description
记作Available_1 = [4, 3, 7]
Here Insert Picture Description
与Need矩阵比较发现,P2、P3和P5各类型资源数目都小于等于Available_1矩阵各类型资源数目,符合要求,所以接下来,给P2、P3或P5分配资源都可以。以P5为例,先分配给P5。

2.3. 计算T2 时刻(P5释放资源后)Available矩阵

这里说明一下,随着系统的运行,Max矩阵、Allocation矩阵和Need矩阵,与未运行的线程有关的部分不变化,每次变化的只有Available矩阵,所以只需求Available矩阵即可。

当P5结束,释放资源后,记为T2 时刻,Available矩阵如下:
Here Insert Picture Description
记作Available_2 = [7, 4, 11]
与Need矩阵比较发现,P1、P2、P3都符合要求,所以接下来,给P1、P2、P3分配资源都可以。以P3为例。

2.4. 计算其他时刻Available矩阵

依照上述方法,继续计算可得:
Available_3 (P3释放资源后)= [11, 4, 16],P1和P2都符合要求,以P2为例。
这里已不必继续计算,可得安全序列P4—>P5—>P3—>P2—>P1。

2.5. 题目答案

(1) T0 时刻为安全状态,其中一个安全序列为P4—>P5—>P3—>P2—>P1(不唯一)。
(2) T0 时刻若进程P2请求资源(0,3,4),不能实施资源分配,因为此时的Available = [2, 3, 3],C类型资源不足。
(3) 因为(2)不能实施资源分配,所以此时的Available = [2, 3, 3],若进程P4请求资源(2,0,1),能够实施资源分配。
(4) 因为(3)实施了资源分配,所以此时的Available = [0, 3, 2],若进程P1请求资源(0,2,0),能够实施资源分配。

3. 一段程序(C语言实现)

/*
Name: 银行家算法
Author: Deep Baldha (CandyZack)
Mender: Vistar Terry (万俟淋曦)
Time: 2019/10/8 16:43
*/ 
#include <stdio.h> 
int main() 
{ 
	// P1, P2, P3, P4, P5  为线程名
	int n, m, i, j, k; 
	n = 5; // 线程数目 
	m = 3; // 资源数目 
	
	// MAX 矩阵
	int max[5][3] = { { 5, 5, 9 }, // P1  
					  { 5, 3, 6 }, // P2 
					  { 4, 0, 11 }, // P3 
					  { 4, 2, 5 }, // P4 
					  { 4, 2, 4 } }; // P5 
	
	// Allocation 矩阵
	int alloc[5][3] = { { 2, 1, 2 }, // P1  
						{ 4, 0, 2 }, // P2 
						{ 4, 0, 5 }, // P3 
						{ 2, 0, 4 }, // P4 
						{ 3, 1, 4 } }; // P5 
	
	// Available 矩阵
	int avail[3] = { 2, 3, 3 };  

	int f[n], ans[n], ind = 0; 
	for (k = 0; k < n; k++) 
	{ 
		f[k] = 0; 
	} 
	int need[n][m]; 
	for (i = 0; i < n; i++) 
	{ 
		for (j = 0; j < m; j++) 
			need[i][j] = max[i][j] - alloc[i][j]; 
	} 
	int y = 0; 
	for (k = 0; k < 5; k++) 
	{ 
		for (i = 0; i < n; i++) 
		{ 
			if (f[i] == 0) { 

				int flag = 0; 
				for (j = 0; j < m; j++) 
				{ 
					if (need[i][j] > avail[j])
					{ 
						flag = 1; 
						break; 
					} 
				} 

				if (flag == 0) 
				{ 
					ans[ind++] = i; 
					for (y = 0; y < m; y++) 
						avail[y] += alloc[i][y]; 
					f[i] = 1; 
				} 
			} 
		} 
	} 

	printf("安全序列如下:\n"); 
	for (i = 0; i < n - 1; i++) 
		printf(" P%d ->", ans[i]+1); 
	printf(" P%d", ans[n - 1]+1); 

	return (0); 
} 

Published 72 original articles · 87 won praise · Views 200,000 +

Guess you like

Origin blog.csdn.net/maizousidemao/article/details/102297414