Operating system ~ deadlock concept, prevention, detection, and release

What is a deadlock

Each process waits for resources in each other's hands, causing each process to be blocked and unable to move forward

The difference between deadlock, starvation, and infinite loop

Deadlock: At least two processes are deadlocked together, and the deadlocked process is in a blocking state.
Starvation: Only one process can be hungry. The starving process may be blocked or ready.
Infinite loop: Only one process may have an infinite loop, and the infinite loop process can be on. Processor

Deadlock and starvation are the problems that the operating system has to solve, and the infinite loop is the problem that application programmers have to solve

Four necessary conditions for deadlock

Mutually exclusive conditions
for the use of resources must be mutually exclusive of competition would lead to a deadlock, such as the consumption of resources and non-preemptive resources
without deprivation
of resources to maintain the process can only take the initiative to release, not to deprive the
request and keep the condition
to maintain a While these resources are not released, request other resource
cyclic waiting conditions.
There is a cyclic waiting chain of process resources.
Cyclic waiting is not necessarily deadlocked, and deadlocking must have cyclic waiting

When will deadlock occur

Unreasonable allocation of inalienable resources may lead to deadlock

Deadlock processing strategy

Prevent deadlocks
Destroy the four necessary conditions for
deadlocks
to avoid deadlocks Avoid the system from entering an unsafe state (Banker's algorithm)
Deadlock detection and removal
Allows deadlocks to occur, and the system is responsible for detecting deadlocks and removing them

Prevent deadlock

Break the mutually exclusive condition

Mutually exclusive conditions : Only contention for resources that must be mutually exclusive will cause a deadlock.

If the resources that can only be used exclusively mutually are transformed to allow shared use, the system will not enter a deadlock state. For example: sPoOLing technology. The operating system can use SPOQLing technology to logically transform an exclusive device into a shared device. For example, using SPooLing technology to transform printers into shared devices...
Insert picture description here
The disadvantage of this strategy: Not all resources can be transformed into shared resources. And for system security, this mutual exclusion must be protected in many places. Therefore, the mutual exclusion condition cannot be broken in many cases.

Undermine the non-deprivation condition

Non-deprivation conditions : The resources acquired by the process cannot be forcibly taken away by other processes before being used up, but can only be released actively.

Conditions
for destruction and non-deprivation : Option 1: When a process requests new resources that are not met, it must immediately release all the resources it holds, and then reapply when needed in the future. That is to say, even if some resources have not been used up, they need to be released actively, thus destroying the inalienable condition.
Scheme 2: When the resources required by a certain process are occupied by other processes, the operating system can assist in forcibly depriving the desired resources. This method generally needs to consider the priority of each process (for example: deprivation of the scheduling method, which is to forcibly deprive the processor resources to be used by the process with higher priority)

Disadvantages of this strategy:
1. It is more complicated to implement.
⒉Release the acquired resources may cause the failure of the previous stage of work. Therefore, this method is generally only suitable for resources that are easy to save and restore state, such as CPU.
3. Repeated application and release of resources will increase system overhead and reduce system throughput.
4. If option one is adopted, it means that as long as a certain resource is temporarily unavailable, those previously obtained resources need to be given up and re-applied later. If this happens all the time, it will lead to process starvation.

Break request and hold condition

Request and hold conditions : The process has reserved at least one resource, but has made a new resource request, and the resource is occupied by other processes. At this time, the requesting process is blocked, but it keeps holding on to its existing resources.

A static allocation method can be used , that is, a process applies for all the resources it needs at one time before running, and it is not allowed to run until its resources are not met. Once put into operation, these resources have been owned by it, the process will not request any other resources.

This strategy is simple to implement, but it also has obvious shortcomings:
some resources may only need to be used for a short time, so if all resources are maintained during the entire running period of the process, it will cause serious waste of resources and extremely low resource utilization. . In addition, this strategy may also cause starvation of certain processes.

Break the loop waiting condition

Circular waiting condition : There is a circular waiting chain of process resources, and the resources obtained by each process in the chain are requested by the next process at the same time.

Sequential resource allocation method can be used . First, number the resources in the system, and stipulate that each process must request resources of the same type (that is, resources with the same number) in the order of increasing number to complete the application at one time.

Principle analysis: A process is only eligible to apply for a resource with a larger number if it already has a resource with a smaller number. According to this rule, it is impossible for a process that already holds a resource with a large number to come back and apply for a resource with a small number in the reverse direction, so there will be no loop waiting.

Suppose there are 10 resources in the system, numbered 1,2,...10
Insert picture description here
. Disadvantages of this strategy:
1. It is not convenient to add new devices, because all numbers may need to be re-allocated;
2. The order and number of resources actually used by the process may be Increasing order is inconsistent, which will lead to waste of resources;
3. Resources must be applied for in the prescribed order, which is troublesome for users to program.

Avoid deadlock

Banker's algorithm

The banker algorithm is designed by the Dutch scholar Dijkstra for the banking system to ensure that when banks issue cash loans, they will not fail to meet the needs of all customers. Later, the algorithm was used in the operating system to avoid deadlocks.

Core idea : When the process submits a resource application, first predict whether the allocation will cause the system to enter an unsafe state. If it enters an insecure state, temporarily refuse to grant this request, and let the process block and wait first.
Insert picture description here

Is the system in a safe state at this time?
Idea: Try to find a safe sequence...{P1, P3, PO, P2, P4} sequentially check whether the remaining available resources (3, 3, 2) can meet the needs of each process
can meet P1 As required, add P1 to the security sequence, and update the remaining available resource values ​​to (5, 3, 2)
and check in turn whether the remaining available resources (5, 3, 2) can meet the remaining processes (not including processes that have joined the security sequence) The demand can meet the requirements of P3, add P3 to the safety sequence, and update the remaining available resource value to (7,4,3) to
check whether the remaining available resources (7,4,3) can meet the remaining processes (not including the added safety sequence) The requirements of the process)...
....... By
analogy, a total of five loop checks can add all 5 processes to the security sequence, and finally a security sequence can be obtained. This algorithm is called a security algorithm. The above process can be easily implemented with code, and each round of inspection starts with the process with the smaller number. In practice, the security sequence can be obtained more quickly.

Core steps

Data structure:
a one-dimensional array of length m Available indicates how many resources are available n m matrix Max indicates the maximum number of resources required by each process
n
m matrix Allocation indicates how many resources have been allocated to each process
Max-Allocation = Need matrix indicates How many resources each process needs at most? Use a one-bit array Request of length m to indicate the number of various resources requested by the process this time

The banker algorithm steps:
①Check whether the application exceeds the previously declared maximum demand. ②Check whether the remaining available resources in the system can still meet the request at this time. ③Try to allocate and change the data structure
④Check with the security algorithm Will this allocation cause the system to enter an unsafe state

Security algorithm steps:
Check whether the current remaining available resources can meet the maximum demand of a process, if possible, add the process to the security sequence, and reclaim all the resources held by the process.
Repeat the above process continuously to see if all processes can be added to the security sequence in the end.

Deadlock detection

In order to detect whether the system has deadlocked, it is necessary to:
①Use a certain data structure to save resource request and allocation information;
②Provide an algorithm that uses the above information to detect whether the system has entered a deadlock state.

Insert picture description here
Insert picture description here

If the number of remaining available resources in the system is sufficient to meet the needs of the process, then the process will not be blocked for the time being and can be executed smoothly. If the execution of this process ends and the resources are returned to the system, some processes that are waiting for resources may be activated and executed smoothly.
Correspondingly, after these activated processes are executed, some resources will be returned, which may activate other blocked processes...
If you analyze the above process and eventually eliminate all edges, it is said that the graph can be completely simplified. At this time, there must be no deadlock (equivalent to finding a safe sequence).
If all edges cannot be eliminated in the end, then deadlock has occurred at this time.
In the end, the processes that are still connected to the edges are the processes that are in a deadlock state.

Insert picture description here

Algorithm to detect deadlock:

1) In the resource allocation graph, find the process Pi that is neither blocked nor isolated point (that is, find a directed edge connected to it, and the number of applications for resources corresponding to the directed edge is less than or equal to the free resources in the system Quantity. As shown in the figure below, R1 has no free resources and R2 has one free resource. If all the edges connected to the process meet the above conditions, the process can continue to run until it is completed, and then release all the resources it occupies). Eliminate all its request edges and allocation changes, making it an isolated node. In the figure below, P1 is the process node that meets this condition, so all edges of P1 are eliminated.
2) The resources released by the process Pi can wake up some processes that are blocked by waiting for these resources, and the original blocking process may become a non-blocking process. In the figure below, P2 satisfies this condition. After a series of simplifications according to the method in 1), if all the edges on the way can be eliminated, the graph is said to be completely simplified.
Insert picture description here
Deadlock theory : If the resource allocation diagram of the system at a certain moment cannot be completely simplified, then the system is deadlocked at this time

Removal of deadlock

Once the deadlock is detected, the deadlock should be removed immediately.
Supplement: Not all processes in the system are deadlocked. After the deadlock detection algorithm is used to simplify the resource allocation graph, those processes that are still connected to the edge are deadlocked processes.

The main methods to remove the deadlock are as follows:
1. Resource Deprivation Law. Suspend (temporarily put on the external memory) some deadlocked processes, and seize its resources, and allocate these resources to other deadlocked processes. However, the suspended process should be prevented from being starved of resources for a long time.
2. The revocation process method (or called the termination process method). Forcibly revoke some or even all deadlocked processes and deprive these processes of resources. The advantage of this approach is that it is simple to implement, but the price paid may be high. Because some processes may have been running for a long time and are close to the end, once they are terminated, they will fall short, and they will have to start all over again in the future.
3. Process rollback method. Let one or more deadlock processes fall back enough to avoid deadlocks. This requires the system to record the historical information of the process and set a restore point.

So the key is to do it to whom
1. Process priority, to low priority
2. How long it has been executed.
3. How long will it take to complete
4. How much resources have been used by the process, and how long it has been executed Hands-on with few resources
5. Is the process interactive or batch-based, hands-on to batch processing

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/113801826