Classic question type of operating system - banker's algorithm for deadlock avoidance


banker's algorithm

use

Banker's algorithm is used to avoid deadlock and is the most famous deadlock avoidance algorithm.

Competition for resources and improper process advancement order will causelead to deadlock

The so-called deadlock refers to a deadlock caused by multiple processes competing for resources during operation. When a process is in this deadlock, it will be unable to move forward without external force.

  • Necessary conditions for deadlock:
    ① Mutual exclusion : The process requires exclusive control of the allocated resources, that is, a resource is only occupied by one process within a period of time
    ② Request and hold : When the process is blocked due to requesting resources , keep the obtained resources
    ③ No deprivation : The resources obtained by the process cannot be deprived before they are used up, and can only be released by themselves when they are used up
    ④ Loop waiting : When a deadlock occurs, there must be a Process – a circular chain of resources

data structure

  • (1) The available resource vector Available
    is an array containing m elements, where each element (Available[i]) represents the i-th categoryNumber of resources available, its initial value is the number of resources of this type configured in the system, and its value changes dynamically with the allocation and recycling of this type of resources [
    Available[j] represents the number of type j resources in the system]
  • (2) The maximum demand matrix Max
    is an nxm matrix that defines the demand for m types of resources by each process in the system.Total demand
    [Max[i][j] represents the total demand for j-type resources by the i-th process]
  • (3) Allocation matrix Allocation
    This is also an nxm matrix, which defines each type of resource in the systemcurrently assignedThe number of resources given to each process
    [Allocation[i][j] represents the number of resources of the jth type that process i has currently obtained.
  • (4) Demand matrix Need
    is also an n×m matrix used to represent each process.still needThe number of various types of resources
    [Need[i][j] indicates that process i also needs the number of resources of the jth type]

There is the following relationship between the above nxm matrices:
         Need[i][j] = Max[i][j] - allocation[i][j]

Algorithm Description

Resource allocation algorithm
Insert image description here
security algorithm (i.e., the security check part of the resource allocation algorithm in the above figure )
Insert image description here

Example explanation

Insert image description here
① Is T0 safe?
Insert image description here

②After time T0, if process P2 issues a resource request Request 2 (1,0,1), can the system allocate resources to it?
Insert image description here

③After process P2 applies for resources, if P1 issues a resource request Request 1 (1,0,1), can the system allocate resources to it?
④After process P1 applies for resources, if P3 issues a resource request Request 3 (0,0,1), can the system allocate resources to it?
Insert image description here

Guess you like

Origin blog.csdn.net/m0_50609545/article/details/122362101