Four necessary conditions for deadlock
Mutual exclusion conditions: The resource is exclusive and used exclusively, and the process uses the resource mutually. That is, a resource can only be used by one process at any time. If other processes apply for a resource and the resource is occupied by another process, the applicant waits until the resource is released by the occupier. (Recommended learning: MySQL video tutorial)
Non-deprivable condition: The resources obtained by the process will not be forcibly deprived by other processes before they are used up, but only The resource can be released by the process that obtained the resource.
Request and retention conditions: Each time a process applies for a part of the resources it needs, it will continue to occupy the allocated resources while applying for new resources.
Loop waiting conditions: When a deadlock occurs, there must be a process waiting queue {P1, P2,...,Pn}, in which P1 is waiting for the resources occupied by P2, and P2 is waiting for the resources occupied by P3. Resources,...,Pn wait for the resources occupied by P1, forming a process waiting loop. The resources occupied by each process in the loop are applied for by another process at the same time, that is, the previous process occupies the resources occupied by the latter process.
The above gives four necessary conditions that lead to deadlock. As long as a deadlock occurs in the system, at least one of the above four conditions is true. In fact, the establishment of loop waiting implies the establishment of the first three conditions. It seems that there is no need to list them. However, considering these conditions is beneficial to the prevention of deadlock, because the occurrence of deadlock can be prevented by destroying any one of the four conditions. .
Deadlock prevention
We can prevent deadlock by destroying the four necessary conditions for deadlock. Since resource mutual exclusion is an inherent feature of resource use, it cannot changed.
Destruction of the "inalienable" condition: When a process cannot obtain all the resources it needs, it is in a waiting state. During the waiting period, the resources it occupies will be implicitly released and re-added to the system's resource list. You can It is used by other processes, and the waiting process can only be restarted and executed if it regains its original resources and newly applied resources.
Destroy the "request and hold conditions": The first method is static allocation, that is, each process applies for all the resources it needs when it starts executing. The second is dynamic allocation, which means that each process does not occupy system resources when it applies for the required resources.
Destroy the "circular waiting" condition: adopt the orderly allocation of resources. The basic idea is to number all resources in the system sequentially, and use larger numbers for those that are in short supply and rare. When applying for resources, you must follow the numbering In order, a process can apply for a process with a larger number only if it obtains a smaller number.
For more MySQL related technical articles, please visit the MySQL Tutorial column to learn!
The above is the detailed content of Necessary conditions for deadlock to occur. For more information, please follow other related articles on the PHP Chinese website!