[Operating system] deadlock problem --- deadlock elimination method

This article will mainly explain the elimination method of deadlock

1. The concept of deadlock

        This is the "Operating System" definition of deadlock:

There are concurrent processes P1, P2,...Pn, which share resources R1, R2,...Rm (n>0, m>0, n>=m). Among them, each Pi (1≤i≤n) owns resources Rj (1≤j≤m) until there are no remaining resources. At the same time, each Pi requires Rk (k≠j, 1≤k≤m) on the premise of not releasing Rj, thus causing resources to occupy and wait for each other. In the absence of external force, the group of concurrent processes stops moving forward and falls into a permanent waiting state.

        In human terms, I don’t give my own resources to others, and I don’t give other people’s resources to me. As a result, both of them can’t perform related operations because they can’t get enough resources and keep waiting. This is a deadlock. The structure diagram is as follows:

 2. Necessary conditions for deadlock to occur

        If a deadlock occurs, the following conditions must be met:

1. Mutually exclusive conditions. A resource cannot be owned by two or more processes at the same time.

2. No deprivation of conditions. Before a process ends, no process can get the resources of the process.

3. Partial distribution. Each time a process applies for a part of the resources it needs, it will not release the resources it already owns while waiting to apply for other resources.

4. Loop conditions. As shown below:

And as long as the above conditions are violated, no deadlock will occur. This is our idea to solve the deadlock problem.

Three, deadlock elimination method

1. Deadlock prevention

        Deadlock prevention is a static way to eliminate deadlocks. It adopts a certain strategy to limit the resource requests of concurrent processes, so that the necessary conditions for deadlock are not satisfied at any time during system execution.

method:

① Mutually exclusive conditions are inherent properties of resources and cannot be avoided

②, destroy the "partial allocation (request and hold)" condition. Fully allocated in advance, waiting to be released. Disadvantages: serious waste of resources (occupied even if not used), reducing process concurrency

③. Undermining the "non-deprivation" condition. Disadvantages: increase system overhead, and the front-end work of the process may fail.

④. Destroy the "loop waiting" condition. Classify resources and arrange them smoothly, and request resources in increasing order; if multiple resources are needed, they must be requested at the same time (first low and then high), if high resources are occupied and low resources are requested, high resources must be released first . This avoids loops. Disadvantages: limit the process's request for resources, and the system overhead is large. The specific operation is shown in the figure below. P3 occupies the s3 resource. When it wants to apply for the s2 resource, it must release the s3 resource.

2. Deadlock avoidance

        Deadlock avoidance can be called dynamic prevention, because the system adopts the method of dynamically allocating resources, predicting the possibility of deadlock and avoiding it during the allocation process. 

method:

  • Process startup rejection: If a process's application for resources may cause a deadlock, the process will not be started
  • Resource allocation rejection: If a process's application for a resource may lead to a deadlock, the resource is not allocated to the process 

Detailed explanation:

process start denied

The meaning of the table description expression is as follows:

Rj\geqslant C_{(n+1)j}+{\sum^n}_{i=1} C_{ij}^{}

(j=1…m)

It is known from the formula that only when the requested resource is less than the remaining resource, it will be allocated to the process resource, otherwise the process resource will not be allocated.

       Only when the above conditions are met, a new process Pn+1 is started (that is, the process is started only when the request of the new process and the maximum request amount of all current processes are met). This strategy is not optimal because it assumes Worst case: all processes simultaneously issue their max requests

Resource Allocation Denied

Famous algorithm to avoid deadlock - banker's algorithm (Dijkstra)

Analogy question:

The number of customers applying for a loan from the bank is limited. Each customer should declare the maximum amount of funds required to complete the project when applying for a loan for the first time. When all loan requirements are met, the customer should promptly return the loan banker applied for by the customer. When the quantity does not exceed the maximum value you own, you should try your best to meet the needs of customers

example:

Suppose the banker has 100,000 yuan in funds, and there are three customers A, B, and C, who need loans of 80,000 yuan, 30,000 yuan, and 90,000 yuan respectively to complete the project. The customer requires an installment loan. At present, A has already borrowed 40,000 yuan. At this time, B needs to apply for 20,000 yuan. , C wants to apply for 40,000. The banker needs to evaluate the safety of the loan request to avoid bad debts. B borrows 20,000, and C borrows 40,000. Can they borrow? In this example the banker is the operating system, the money is the resource, and the client is the process that requests the resource.

Allow processes to dynamically apply for resources, and before the system allocates resources, first calculate whether the allocation is safe

    This allocation will not cause the system to enter an unsafe state, then allocate, otherwise, the process requesting the resource this time waits

security status

    After this allocation, there is a process security sequence {P1, ..., Pn}, and for each Pi, the amount of resources it needs does not exceed the current remaining resources of the system and all Pj (j<i) occupied sum of resources

unsafe state

    No security sequence exists

Data structures used in the algorithm

 Available resource array

    Available[j] = k Currently there are k resource j

Max maximum demand matrix

    Max[i,j] = l Process Pi's maximum demand for resource j is l

Allocation allocation matrix  

    Allocation[i, j] = m The number of processes Pi allocated to resource j is m

Need demand matrix

    Need[i, j] = n process Pi also needs n resources j

Request[i] resource request

    If Request[i, j] = q, the number of resource j requested by process Pi is q

The flow chart is as follows:

Allocation of resources flowchart

 

Release resource flow chart:

Work: the number of resources available during system operation, initial value = Available

Finish[i]: Whether the system has enough resources allocated to the process Pi to make it run, initially false

 

Guess you like

Origin blog.csdn.net/peng_lv/article/details/127605317