【Thread】-------Understanding deadlock

1. Deadlock

1) It is proposed that
multi- threading and multi-process improve the utilization of system resources, but concurrent execution will also bring some problems, such as deadlock.
2) Concept
Deadlock refers to a phenomenon of blocking caused by competition for resources or communication between two or more processes during the execution process. If there is no external force, they will not be able to advance. At this time, the system is said to be in a deadlock state or the system has a deadlock, and these processes that are always waiting for each other are called deadlock processes.
3) Examples of deadlock in real life
In a computer system, there is only one printer and one input device. Process 1 is occupying the input device and at the same time requesting to use the printer, while process 2 is occupying the printer and requesting to use the input device at the same time. In this way, the two processes can only wait endlessly, and the two processes will fall into a deadlock. condition

Second, the necessary conditions for deadlock

The root cause of the thinking:
competition for resources and illegal process advancement sequence The following four conditions must be met at the same time to
generate a deadlock:
1) Mutual exclusion condition
means that the process uses the allocated resources exclusively, that is, at a certain time, a certain resource can only be used. Occupied by one process, if other processes request resources, let other processes wait until the occupied process is released.
2) Request and hold condition
means that a process has already occupied at least one resource, but a new resource request has been made, and the resource has been occupied by other processes. At this time, the requesting process is blocked, but other resources that it has obtained are kept. .
3) Non-deprivation condition
means that after a process occupies a resource, the resource cannot be deprived without its own initiative to release it.
4) The loop waiting condition
means that when a deadlock occurs, there must be a process—a circular chain of resources, one waiting for another occupied resource.
write picture description here

Third, solve the deadlock

Special treatment method: ostrich strategy
Just like ostrich, when it encounters danger, it will bury its head in the ground, pretend not to see it, and ignore the danger. Although it is not an algorithm, it is widely used in actual systems, so in the computer operating system, when a deadlock occurs and affects the normal operation of the system, manual intervention - restart.
1. Prevent deadlock
By setting certain restrictions, one or more of the four necessary conditions for deadlock are destroyed. This method is easier to implement and has been widely used, but it will reduce the utilization of system resources.
To prevent deadlocks, the four conditions that lead to deadlocks are destroyed in advance. Mutual exclusion cannot be destroyed, so there are three methods as follows:
1), destroy the request and hold conditions, the
process must wait for all the resources to be requested to be idle before applying for resources, this method will cause serious waste of resources (some resources may only be used at the beginning or end of the run, or not at all).
After the process is allowed to obtain the resources it needs at the initial stage, it will start running, and then gradually release the resources it occupies during the running process. For example, a process's task is to copy the data to the disk and then print it. In the early stage, it only needs to obtain the disk resources and does not need to obtain the resources. Printer resources, and release disk resources after copying is complete. This method is better than the first method and will increase the resource utilization.
2) Destruction of the non-preemptive condition
This method is expensive and complicated to implement.
3) Destroy the cyclic waiting
condition Make a regulation on the order in which each process requests resources to avoid waiting for each other. This method has a higher utilization rate of resources than the first two, but in the early stage, the serial number must be assigned to the device. There will be a problem when adding a new device, and secondly, there are restrictions on user programming.
2. Avoid deadlock
In the process of dynamic allocation of resources, use some method to prevent the system from entering an unsafe state.
Two methods of deadlock avoidance:

  • * If a process's request would cause a deadlock, do not start the process.
  • * This allocation is not allowed if a process's increased resource requests would cause a deadlock.

The advantage of deadlock avoidance is that it does not require preempting and rolling back processes as in deadlock prevention, and is less restrictive than deadlock prevention. However, it also has many limitations in use:

  • *The maximum resources requested by each process must be declared in advance.

  • * Processes under consideration must be unrelated, that is, the order in which they execute must be free from any constraints imposed by synchronization requirements

  • *The number of allocated resources must be fixed.

  • * Processes cannot exit while occupying resources.

3. Deadlock Detection and Removal
Deadlock prevention strategies are very conservative, they solve deadlock problems by restricting access to resources and imposing constraints on processes.
The deadlock detection strategy is the exact opposite, it does not restrict resource access or constrain process behavior.
For deadlock detection, whenever possible, the requested resource is granted to the process. The operating system periodically executes an algorithm to detect the preceding cyclic wait condition.
Through the detection mechanism set by the system, the processes and resources related to the deadlock can be found in time, and then the process can be released from the deadlock state. The common measure is to revoke or suspend some processes (deprivation) in order to recover some resources. , and then assign these resources to the process that is already in the blocking state, make it ready, and make it continue to run. This method is difficult to implement.

Fourth, common algorithms to avoid deadlock

1. Using the banker's algorithm to avoid deadlock
Dijkstra's banker's algorithm is the most representative algorithm to avoid deadlock.
2. Data Structures in the Banker's Algorithm

1) The available resource vector Available
is an array with m elements, each element of which represents the number of available resources of a class. If Available[j]=K, it means that there are K Rj resources in the system.
2) Maximum demand matrix Max
This is an n×m matrix, which defines the maximum demand for m-type resources for each of the n processes in the system. If Max[i,j]=K, it means that the maximum number of Rj resources required by process i is K.
3) Allocation matrix Allocation
This is also an n×m matrix, which defines the number of resources currently allocated to each process for each type of resource in the system. If Allocation[i,j]=K, it means that the number of Rj resources allocated by process i is K.
4) Requirement matrix Need.
This is also an n×m matrix, which is used to represent the number of various resources that each process still needs. If Need[i,j]=K, it means that process i still needs K resources of Rj class to complete its task.

3. Banker's Algorithm

In the method of avoiding deadlock, the constraints imposed are weaker, and it is possible to obtain satisfactory system performance. In this method, the state of the system is divided into a safe state and an unsafe state. As long as the system is always in a safe state, deadlock can be avoided.
The basic idea of ​​the banker's algorithm is to judge whether the system is safe before allocating resources; if so, allocate it. It is the most representative algorithm to avoid deadlock.
Assuming that the process cusneed makes a request REQUEST [i], the banker's algorithm judges according to the following rules.
(1) If REQUEST [cusneed] [i]<= NEED[cusneed][i], go to (2); otherwise, error.
(2) If REQUEST [cusneed] [i]<= AVAILABLE[i], go to (3); otherwise, wait.
(3) The system tries to allocate resources and modify related data:
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
NEED[cusneed][i] -=REQUEST[cusneed][i];
(4) The system performs a security check, if it is safe, the allocation is established; otherwise, the exploratory allocation is invalid, the system returns to its original state, and the process waits.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485550&siteId=291194637