Summary of test sites for deadlock in the interview (definition, cause, four necessary conditions, how to prevent deadlock)

Deadlock

Definition of deadlock : Deadlock refers to a deadlock in which multiple processes are waiting for each other due to competition for resources. Without external force, these threads cannot move forward, resulting in deadlock.

Causes : 1. Competing for resources 2. Improper sequence of process request and release of resources, leading to deadlock

Necessary conditions for deadlock
1. Mutually exclusive conditions : a resource can only be accessed by one thread at the same time, if other processes request the resource, they can only wait
Insert picture description here

2. Inalienable condition : before the resource obtained by the process is not used up, it cannot be forcibly taken away by other threads, and can only be released by the process that obtained the resource
Insert picture description here

3. Request and retention conditions : The process has retained at least one resource, but it has made a new resource request. If the new resource is already occupied, it can only wait, but it will not release the resource occupied by itself.
Insert picture description here

4. Cyclic waiting conditions : a situation where several processes are waiting for resources in a concatenated loop
Insert picture description here

Prevention of deadlock ---- destroy the necessary conditions for deadlock to prevent deadlock (mainly to avoid the occurrence of two conditions 3 and 4)
1. Destroy mutually exclusive conditions : change resources that can only be used exclusively for mutual exclusion to allow Shared resources, such as spooLing technology, logically transform the exclusive device into a shared device
2. Destruction of the inalienable condition : When a process requests a new resource that cannot be met, it immediately releases all the retained resources, and needs to be re-needed later. Reapply
3. Destroy the request and keep the condition : adopt the static allocation method, that is, the process applies for the resources it needs once before running, and will not let him run until the resources are not met. Once running, these resources belong to it. All, other resources will not be requested at this time.
4. Disrupt the cyclic waiting condition : using sequential resource allocation method, first number the resources in the system, and stipulate that the process must request resources in the order of increasing number. For example, a process can apply for a resource with a larger number only if it already has a resource with a smaller number.

The famous banker algorithm uses the fourth method to avoid deadlock

Guess you like

Origin blog.csdn.net/qq_44443986/article/details/115349328