In a database, a deadlock is a situation where two or more threads are blocked (hang) forever, waiting indefinitely for each other's conditions to complete and unlock data resources. In this case, the tasks are not completed and are always waiting; therefore it is considered one of the most feared complications in DBMS. Let's take a look at the conditions under which deadlock occurs and how to prevent deadlock.
Conditions for deadlock to occur
If all of the following conditions are met, a deadlock may occur.
1. Mutual exclusion condition: There must be at least one resource that cannot be used by multiple processes at a time.
2. Holding and waiting conditions: A process holding resources can request other resources held by other processes in the system.
#3. No preemption conditions: Resources cannot be forcibly obtained from the process before use is completed. The resources held by the process can be released only after it has finished using them.
4. Circular waiting conditions: A process is waiting for resources held by a second process and the second process is waiting for a third process... Wait, the last process is waiting Wait for the first process, thus making a circular chain of waits.
How to prevent deadlock
We have learned that if all the above conditions are true, a deadlock will occur, so prevent it One or more can prevent deadlock.
1. Avoid mutually exclusive conditions: All resources must be shareable, which means that multiple processes can obtain resources at one time. But this approach is almost impossible to achieve.
2. Avoid hold and wait conditions: This condition can be avoided if the process obtains all the resources it needs before starting. Another way to avoid this condition is to not execute the rules that request the resource while the process is holding it.
3. Seizing resources: Seizing resources from the process may cause rollback, so this situation needs to be avoided to maintain the consistency and stability of the system.
4. Avoid circular wait conditions: This situation can be avoided if the resources are maintained in a hierarchy and the process can save the resources in order of increasing priority. This avoids loop waiting. Another approach is to force a resource per process rule - a process can request a resource when it releases the resource currently held by it. This avoids loop waiting.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of What is deadlock in DBMS? Deadlock occurrence conditions. For more information, please follow other related articles on the PHP Chinese website!