银行家算法是什么_什么是银行家算法?

银行家算法是什么

Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used in banking systems to determine whether a loan can be granted or not.

Banker算法是一种避免死锁的算法 。 之所以这样命名,是因为该算法在银行系统中用于确定是否可以授予贷款。

Consider there are n account holders in a bank and the sum of the money in all of their accounts is S. Everytime a loan has to be granted by the bank, it subtracts the loan amount from the total money the bank has. Then it checks if that difference is greater than S. It is done because, only then, the bank would have enough money even if all the n account holders draw all their money at once.

假设在银行中有n帐户持有人,并且他们所有帐户中的金额之和为S 每次银行必须提供贷款时,都会从银行的 金额中减去贷款金额 。 然后,它检查该差是否大于S 之所以这样做,是因为只有那时,所有n帐户持有者立即提取所有资金,银行才会有足够的资金。

Banker's algorithm works in a similar way in computers.

Banker的算法在计算机中的工作方式相似。

Whenever a new process is created, it must specify the maximum instances of each resource type that it needs, exactly.
每当创建新进程时,它都必须准确地指定所需的每种资源类型的最大实例。

Let us assume that there are n processes and m resource types. Some data structures that are used to implement the banker's algorithm are:

让我们假设有n进程和m资源类型。 用于实现银行家算法的一些数据结构是:

1. Available (1. Available)

It is an array of length m. It represents the number of available resources of each type. If Available[j] = k, then there are k instances available, of resource type R(j).

它是长度为m数组 。 它代表每种类型的可用资源数量。 如果Available[j] = k ,则存在k可用实例,资源类型为R(j)

2. Max ( 2. Max)

It is an n x m matrix which represents the maximum number of instances of each resource that a process can request. If Max[i][j] = k, then the process P(i) can request atmost k instances of resource type R(j).

它是一个nxm矩阵,代表进程可以请求的每个资源的最大实例数。 如果Max[i][j] = k ,则进程P(i)最多可以请求k个资源类型R(j)实例。

3. Allocation (3. Allocation)

It is an n x m matrix which represents the number of resources of each type currently allocated to each process. If Allocation[i][j] = k, then process P(i) is currently allocated k instances of resource type R(j).

它是一个nxm矩阵,表示当前分配给每个进程的每种类型的资源数量。 如果Allocation[i][j] = k ,则当前为进程P(i)分配k个资源类型R(j)实例。

4. Need (4. Need)

It is an n x m matrix which indicates the remaining resource needs of each process. If Need[i][j] = k, then process P(i) may need k more instances of resource type R(j) to complete its task.

它是一个nxm矩阵,指示每个进程的剩余资源需求。 如果Need[i][j] = k ,则进程P(i)可能需要k个资源类型R(j)更多实例来完成其任务。

Need[i][j] = Max[i][j] - Allocation [i][j]

资源请求算法 (Resource Request Algorithm)

This describes the behavior of the system when a process makes a resource request in the form of a request matrix. The steps are:

这描述了当进程以请求矩阵的形式发出资源请求时系统的行为。 这些步骤是:

  1. If number of requested instances of each resource is less than the need (which was declared previously by the process), go to step 2.

    如果每个资源的请求实例数少于需求(该资源先前已由流程声明),请转到步骤2。

  2. If number of requested instances of each resource type is less than the available resources of each type, go to step 3. If not, the process has to wait because sufficient resources are not available yet.

    如果每种资源类型的请求实例数少于每种类型的可用资源,请转到步骤3。否则,该过程必须等待,因为还没有足够的资源。

  3. Now, assume that the resources have been allocated. Accordingly do,

    现在,假设已分配资源。 因此,

  4. Available = Available - Requesti
    Allocation(i) = Allocation(i) + Request(i)
    Need(i) = Need(i) - Request(i)
    
    

This step is done because the system needs to assume that resources have been allocated. So there will be less resources available after allocation. The number of allocated instances will increase. The need of the resources by the process will reduce. That's what is represented by the above three operations.

进行此步骤是因为系统需要假定已分配资源。 因此分配后将有较少的可用资源。 分配的实例数将增加。 该过程对资源的需求将减少。 这就是以上三个操作所代表的内容。

After completing the above three steps, check if the system is in safe state by applying the safety algorithm. If it is in safe state, proceed to allocate the requested resources. Else, the process has to wait longer.

完成上述三个步骤后,请通过应用安全算法检查系统是否处于安全状态。 如果处于安全状态,请继续分配请求的资源。 否则,该过程必须等待更长的时间。

安全算法 (Safety Algorithm)

  1. Let Work and Finish be vectors of length m and n, respectively. Initially,

    WorkFinish为长度分别为mn的向量。 原来,

    Work = Available
    Finish[i] =false for i = 0, 1, ... , n - 1.
    
    

    This means, initially, no process has finished and the number of available resources is represented by the Available array.

    这意味着,最初,没有进程完成,可用资源的数量由Available数组表示。

  2. Find an index i such that both

    找到一个索引

    Finish[i] ==false
    Needi <= Work
    
    

    If there is no such i present, then proceed to step 4.

    如果没有此类提示,请继续执行步骤4。

    It means, we need to find an unfinished process whose need can be satisfied by the available resources. If no such process exists, just go to step 4.

    这意味着,我们需要找到一个未完成的过程,其可用资源可以满足其需求。 如果不存在这样的过程,请转到步骤4。

  3. Perform the following:

    执行以下操作:

    Work = Work + Allocation;
    Finish[i] = true;
    
    

    Go to step 2.

    转到步骤2。

    When an unfinished process is found, then the resources are allocated and the process is marked finished. And then, the loop is repeated to check the same for all other processes.

    当发现未完成的过程时,将分配资源并将该过程标记为完成。 然后,重复循环以对所有其他进程进行相同的检查。

  4. If Finish[i] == true for all i, then the system is in a safe state.

    如果对所有i都使用Finish[i] == true ,则系统处于安全状态。

    That means if all processes are finished, then the system is in safe state.

    这意味着,如果所有过程都已完成,则系统处于安全状态。

翻译自: https://www.studytonight.com/operating-system/bankers-algorithm

银行家算法是什么

你可能感兴趣的:(算法,java,python,linux,大数据)