Linux thread deadlock

deadlock

Deadlock is a state in which an operating system or software is running: in a multitasking system, when one or more processes wait for system resources, and the resources are occupied by the process itself or other processes, a deadlock is formed. Because the resource occupation is mutually exclusive, when a process requests resources, the relevant process will never be able to allocate the necessary resources and cannot continue to run without external assistance, which produces a special phenomenon.

Four necessary conditions for deadlock to occur

  • Mutual exclusion condition: The resource is used exclusively and exclusively, and the use of the resource by the process is mutually exclusive. At the same time, only one process is allowed to obtain the resource.
  • Inalienable condition: The resource obtained by a process cannot be forcibly deprived by other processes before it is used up, and can only be released by the process that obtains the resource.
  • Request and hold conditions: Each time a process re-applies for resources, it still occupies the allocated resources.
  • Circular waiting condition: When a deadlock occurs, there must be a process waiting queue, the previous process occupies the resources needed by the latter process, and the last process occupies the resources needed by the first process, forming a process waiting loop.

For deadlock to occur, all of the above conditions must be met.

Deadlock Prevention

Now that we know the four necessary conditions for deadlock, then if we want to prevent deadlock, we can make it fail to meet these conditions.

Mutual exclusion of resources is an inherent feature of resource usage that we cannot change, so we can only target the remaining three conditions.

  • Destruction of the inalienable condition: a process is in a waiting state before obtaining the requested resource. During the waiting period, the resources it occupies will be implicitly released and re-added to the system's resource list, which can be used by other processes, and this process can only re- Restart only after obtaining your own original resources and applying for resources.
  • Break request and hold condition:
  1. Static allocation: each process allocates the space it needs at the very beginning.
  2. Dynamic allocation: each process does not occupy system resources when applying for the required resources.
  • Destroy the circular waiting condition: Number the resource process, assign a larger number to the scarce resource, and proceed in sequence when applying for resources.

deadlock avoidance

The system checks each resource request issued by the process that can be satisfied by the system, and decides whether to allocate resources according to the check results. If a deadlock may occur after allocation, it will not be allocated. If the system can guarantee that all processes get all the resources needed in a limited time, it is in a safe state, otherwise it is not safe.

Guess you like

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