The necessary condition of deadlock and its treatment method

Source: Internet
Author: User
Tags mutex
Although a deadlock may occur while the process is running, a deadlock must have a certain condition, and the following four necessary conditions must exist for the deadlock to occur. 1 mutually exclusive conditions: refers to the process of allocating resources to the exclusive use, that is, for a period of time a resource is only occupied by a process.   If there are other processes requesting resources at this time, the requester can only wait until the process that occupies the resource is released.   2 Request and Retention conditions: The process has maintained at least one resource, but a new resource request has been made, and the resource has been occupied by other processes, at which point the request process is blocked, but the other resources that it has obtained remain in place.   (3) No deprivation of condition: the resources that the process has obtained, which cannot be deprived until it has been used, can only be released by itself when the use is complete. 4 loop (loop) waiting condition: In the event of a deadlock, there must be a process-a circular chain of resources, that is, the P0 in the process set {p0,p1,p2,,pn} is waiting for a P1 resource to occupy; P1 is waiting for the resource occupied by P2, ..., Pn is waiting for resources that have been P0 occupied , that is, the process of forming a kind of end-to-end circular waiting resource relationship between several processes. These four conditions are necessary for deadlock, as long as the system is deadlocked, these conditions must be set up, and as long as one of the above conditions is not satisfied, the deadlock will not occur. 1. The competition cannot preempt the row resources to cause the deadlock: Usually the system has the not the Preemption resources its quantity not sufficient to satisfy the multiple process operation the need, causes the process in the running process, will struggle for the resource to deadlock. 2. Competition for consumable resources leads to consumption of 3. Deadlock caused by improper process advance sequence the lifting and prevention of deadlockUnderstanding the causes of deadlocks, especially the four necessary conditions for deadlocks, is the most likely way to avoid, prevent, and unlock deadlocks. Therefore, in the system design, process scheduling and so on to pay attention to how to not let these four prerequisites, how to determine the rational allocation of resources algorithm, to avoid the process of permanent occupy the system resources. In addition, you want to prevent the process from consuming resources while it is in a waiting state. In the process of system operation, the process issued by each system can meet the resource request for dynamic check, and according to the inspection results to decide whether to allocate resources, if the system may occur after the allocation of deadlock, will not be allocated, otherwise allocated. Therefore, the allocation of resources should be given reasonable planning. (1) Ordered resource allocation method The algorithm resources are numbered in a rule system (for example, a printer is 1, tape drive is 2, disk is 3, etc.), and the application must be in ascending order. System Requirements Process: 1, for it must be used and belong to the same category of all resources, must be applied at once; 2, in the application of different types of resources, must be in accordance with the number of various types of equipment to apply.   For example: Process PA, the order of using resources is R1,R2, process PB, the order of using resources is R2,R1, if the dynamic allocation is possible to form loop conditions, resulting in deadlock. Using ordered resource allocation method: R1 Number of 1,R2 is numbered 2; PA: The Order of application should be: R1,R2 PB: The order of application should be: R1,R2 This destroys the loop condition and avoids the deadlock (2) the most representative algorithm in the bank algorithm to avoid deadlock algorithm is Dijkstra E.   The banker algorithm proposed by W in 1968: The algorithm needs to check the applicant's maximum demand for resources, and if the system's existing resources can satisfy the applicant's request, it satisfies the applicant's request. This allows the requester to quickly complete its calculations and then release the resources it occupies, thus ensuring that all processes in the system are complete, so that deadlocks can be avoided. method of deadlock exclusion1. Undo all processes that are deadlocked, 2, undo the process of deadlock one by one, until the deadlock does not exist, 3, and forcibly discard the resource occupied from the deadlock-locked process, until the deadlock disappears. 4, from other processes there to forcibly deprive a sufficient number of resources allocated to the deadlock process, to unlock the deadlock state
methods to handle deadlocks:1. Prevention of deadlock 2. Avoid deadlock 3. Deadlock 4. Unlock deadlock ways to prevent deadlocks: 1. Breach of "mutually exclusive" conditions: Is to cancel the mutex in the system. If the resource is not used exclusively by a process, then the deadlock is definitely not going to happen. In general, however, in the four conditions listed, the "mutex" condition cannot be broken. Therefore, in deadlock prevention, it is primarily to destroy several other necessary conditions without involving the breach of the "mutually exclusive" condition. 2. Destruction of "possession and waiting" conditions:The "Possession and wait" condition is not allowed in the system to request other resources in the event that the process has acquired a resource. The idea is to devise a way to prevent the process from requesting additional resources while holding resources. Method One: When creating a process, ask it to request all of the required resources, system or meet all its requirements, or give nothing to it. This is the so-called "one-time allocation" scheme. Method Two: Before each process presents a new resource request, it releases the resources it occupies. Thus, when a process needs resource s, it must first release its previously owned resource R before it can make a request for S, even though it may soon have to use resource R. 3. Destroy "non-preemption" conditions:To destroy the "not preemption" condition is to allow the seizure of resources. Method One: If a process that occupies some resources makes a request for further resources rejected, the process must release the resources it originally possessed and, if necessary, request those and additional resources again. Method Two: If a process requests a resource currently occupied by another process, the operating system can preempt another process and require it to release resources. Method Two can prevent deadlocks only if the priority of any two processes is not the same. 4. Break the "cycle Wait" condition:One way to break the "recycle waiting" condition is to unify all the resources in the system, and the process can request resources at any time, but all applications must be submitted in the Order of the resources (ascending). This will ensure that the system does not appear deadlocked.
unlock of deadlocks:Once the deadlock is detected, the appropriate measures should be taken immediately to unlock the deadlock. The main methods of deadlock removal are: 1. The method of resource deprivation. Suspend some deadlock processes and preempt its resources, assigning these resources to other deadlock processes. However, the suspended process should be prevented from getting resources for a long time and in a resource-poor state. (2) The procedure Law of revocation. Forces the revocation of partial or even total deadlock processes and deprives the resources of these processes. The principle of revocation can be carried out according to the process priority and the cost of the cancellation process. 3 Process fallback method. Let a (multiple) process fall back enough to avoid deadlock, voluntarily releasing resources instead of being stripped when the process is rolled back. Requires the system to keep the history of the process and set the restore point.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.