conditions for deadlocks
Mutex condition (Mutual Exclusion) : Resources cannot be shared and can only be used by one process.
request and hold condition (holds and wait) : .
no preemption condition (no pre-emption) : some system resources are not preempted, and when a process has obtained such a resource, the system cannot forcibly retract it, releasing the
cyclic wait condition (Circular wait) : several processes form a ring chain, each occupying the next resource requested by the other .
policy for deadlock management
1. Ignore the problem. For example Ostrich algorithm .
2. Detect the deadlock and recover.
3, carefully dynamic allocation of resources to avoid deadlocks.
4, by breaking the deadlock four one of the necessary conditions to prevent the deadlock.
Ostrich algorithm:
The algorithm can be applied in cases where there are few deadlocks. Why is called the ostrich algorithm, because the legend of the ostrich saw the danger of burying his head under the ground, perhaps the ostrich feel that there is no danger to see it. It's kind of like deceiving.
Banker algorithm:
The so-called banker algorithm, refers to the allocation of resources before the first to see whether the resource allocation will lead to the system deadlock. if it is deadlocked, it is not assigned, otherwise it is assigned.
According to the idea of the banker's algorithm, when the process requests resources, the system allocates system resources according to the following principles:
(1) This process can be accepted when a process has a maximum demand for resources that does not exceed the number of resources in the system.
(2) The process can request resources in phases when the total number of requests cannot exceed the maximum demand.
(3) When the system's existing resources do not meet the resource requirements of the process, requests for the process can be deferred, but the process is always able to get resources for a limited time.
(4) When the existing resources of the system can meet the required resources of the process, it is necessary to test whether the existing resources of the system can meet the maximum number of resources required by the process, and if so, allocate the resources according to the current application amount, or postpone the allocation.
Strategies for resolving deadlocks
The strategies for dealing with deadlocks include:
(1) Deadlock prevention: any one of the necessary conditions for a deadlock can prevent a deadlock. For example, requiring a user to request a resource for a one-time application of all the resources required, which destroys the hold and wait conditions, the resource is layered, to obtain a layer of resources before the next layer of resources can be applied, it destroys the loop wait condition. Prevention usually reduces the efficiency of the system.
(2) Deadlock avoidance: Avoidance is the process of determining whether these operations are safe each time a resource is requested, for example, using the banker algorithm. Deadlock avoidance algorithm execution increases the overhead of the system.
(3) Deadlock detection: Deadlock prevention and avoidance are pre-measures, and deadlock detection is to determine whether the system is in a deadlock state, if it is, the execution of deadlock cancellation policy.
(4) Deadlock cancellation: This is used in conjunction with deadlock detection, which is used in a way that is stripped. The resources owned by a process are forcibly retracted and allocated to other processes.
Avoidance of deadlocks:
The prevention of deadlock is to prevent the generation of deadlocks by destroying the conditions, but this method destroys the parallelism and concurrency of the system.
The first three conditions of the deadlock are the necessary conditions for the deadlock, that is, to produce a deadlock must have the condition, rather than the existence of these 3 conditions must produce a deadlock, so long as the logic to avoid the fourth condition can be avoided deadlock.
Avoid deadlocks by allowing the first three conditions to exist, but using a reasonable resource allocation algorithm to ensure that a ring-waiting closed process chain is never formed, thus avoiding deadlocks. The method supports parallel execution of multiple processes, and in order to avoid deadlocks, the system dynamically determines whether to allocate a resource to the requested process. Here's how:
1. If a process's current requested resource causes a deadlock, the system refuses to start the process;
2. If the allocation of a resource causes the next deadlock, the system rejects the allocation;
Obviously to avoid deadlocks, you must know beforehand how many resources the system has and its attributes
The cause of deadlock and the way to solve and avoid it