Deadlock condition Mutual exclusion: Resources cannot be shared and can only be used by one process. Hold and wait: a process that has acquired some resources but is blocked by requests for other resources will Hold on to the obtained resources. No pre-emption condition: Some system resources are not preemption. After a process has obtained the resource, the system cannot reclaim it. It can only be released when the process is used up. Circular wait: Several processes form a Circular chain, each occupying the next resource requested by the other. Policies for handling deadlocks
1. Ignore the problem. Take the Ostrich algorithm.
2. Detect deadlocks and resume. Allocate resources dynamically carefully to avoid deadlocks. 4. Prevent deadlocks by breaking one of the four necessary conditions for deadlocks.
Ostrich algorithm:
The algorithm can be applied in the case of rare deadlock. Why is it called the ostrich algorithm? Because it is said that the ostrich will bury its head in the ground when it sees danger. It’s kind of like stealing a bell.
Banker algorithm:
The so-called banker algorithm is to see whether the allocation of resources will lead to a system deadlock before allocating resources. If it is deadlocked, it is not allocated, otherwise it is allocated.
According to the banker algorithm, when a process requests a resource, the system allocates system resources according to the following principles:
(1) A process can be accepted when its maximum demand for resources does not exceed the number of resources in the system.
(2) The process can request resources in stages, when the total number of requests does not exceed the maximum demand.
(3) When the existing resources of the system cannot meet the number of resources still needed by the process, the allocation of the request for the process can be postponed, but the process can always get resources in a limited time.
(4) When the existing resources of the system can meet the number of resources still needed by the process, it must test whether the existing resources of the system can meet the maximum number of resources still needed by the process. If so, the resources will be allocated according to the current number of applications; otherwise, the allocation will be postponed.
Strategies for resolving deadlocks
The main strategies for dealing with deadlocks are:
(1) Deadlock prevention: Breaking any of the conditions necessary to cause deadlocks can prevent deadlocks. For example, requiring users to apply for all resources at once breaks hold and wait conditions; The loop waiting condition is destroyed by layering resources to obtain resources at the upper layer before applying for resources at the next layer. Prevention often reduces the efficiency of the system.
(2) Deadlock avoidance: Avoidance means that the process determines whether these operations are safe each time it requests a resource, for example, using the banker algorithm. Execution of the deadlock avoidance algorithm increases the overhead of the system.
(3) Deadlock detection: deadlock prevention and avoidance are prior measures, and deadlock detection is to judge whether the system is in a deadlock state, if so, the implementation of deadlock release strategy.
(4) Deadlock release: this is used in combination with deadlock detection, which is used by stripping. To forcibly reclaim resources owned by a process and allocate them to other processes.
Deadlock avoidance: The prevention of deadlocks is to prevent the occurrence of deadlocks by breaking the generation condition, but this approach destroys the parallelism and concurrency of the system. The first three conditions for deadlock generation are the necessary conditions for deadlock generation, that is, the existence of the three conditions must be met, rather than the existence of these three conditions must produce deadlock, so as long as the fourth condition is logically avoided to avoid deadlock. Deadlocks are avoided by allowing the first three conditions to exist, but by using a reasonable resource allocation algorithm to ensure that a closed chain of processes never forms a circular wait. This method supports parallel execution of multiple processes. To avoid deadlocks, the system dynamically determines whether to allocate a resource to the requesting process. The method is as follows: 1. If the resource requested by a process causes a deadlock, the system refuses to start the process. 2. If the allocation of a resource causes a deadlock in the next step, the system rejects the allocation. Obviously, to avoid deadlocks, you must know in advance how many resources your system has and their attributes