Causes, prerequisites, and scenario analysis of deadlock generation ·

Source: Internet
Author: User

Deadlock is the short name of a process deadlock, which is a deadlock caused by competing for resources during the course of the process, and when the process is in such a stalemate, they will no longer be able to move forward without external forces. It is one of the most difficult problems to be dealt with in computer operating system and even concurrent programming.

As a simple example, there is only one CD-ROM drive and one printer in the system, one process occupies the CD-ROM drive, the printer is applied, the other process occupies the printer, and the CD-ROM is applied. As a result, two processes are blocked and can never be lifted on their own. This is the deadlock.

The main cause of the deadlock is three points:

(i) Insufficient system resources;

(ii) The progress of the process is not in the right order;

(iii) Improper allocation of system resources.

It is easy to see that the reason for the deadlock in the above example is the improper allocation of resources. If the system has sufficient resources, the resource requests of the process can be met, the likelihood of deadlocks is very low, otherwise it will be locked into a deadlock because of the contention for limited resources. In addition, the process can run in a different order and speed, and may also generate a deadlock.

There are four necessary conditions for generating a deadlock:

(i) mutually exclusive condition: A resource can only be accessed by one process at a time. That is, a resource can only be occupied by one process for a period of time, and cannot be occupied by two or more than two processes simultaneously. Such an exclusive resource, such as a CD-ROM drive, a printer, and so on, must be actively released by the process that owns the resource before other processes can take possession of the resource. This is determined by the properties of the resource itself.

(ii) Request and hold conditions: When a process is blocked by a request for resources, the resources that have been obtained are persisted. The process already occupies at least one resource, but it also applies for new resources; Because the resource is already occupied by another process, the process is blocked at this time, but it continues to occupy the resources it has already occupied while waiting for new resources.

(iii) Conditions of deprivation: the resources already acquired by the process cannot be forcibly deprived until it is exhausted and can only be released by the possessor process of that resource.

(iv) Cyclic waiting conditions: A number of resources to form a tail-to-toe cyclic waiting resource relationship.

As long as the system has a deadlock, these conditions must be established, and as long as one of the above conditions is not satisfied, there will be no deadlock. This provides the possibility for us to solve the deadlock problem. In general, the method of resolving deadlocks is divided into three types of deadlock prevention, avoidance, detection and recovery (note: Detection and recovery of deadlocks is a method).

Prevention of deadlock >>

The prevention of deadlock is a strategy to ensure that the system does not enter the deadlock state. Its basic idea is to require the process to request resources to follow some kind of protocol, thus breaking the four necessary conditions to create a deadlock in one or several, to ensure that the system will not enter the deadlock state.

(i) breaking the mutex: allowing the process to access some resources at the same time. However, because some resources are not allowed to be accessed concurrently, this method is not very practical.

(ii) Breaking the conditions of deprivation: allowing the process to forcibly deprive some resources from the resources occupied by the possessor. That is, when a process takes up some resources, it goes to the application for new resources, but it cannot be satisfied immediately. It must release the resources it occupies and wait until later to reapply. The resources it frees can be allocated to other processes. This is equivalent to the resources occupied by the process being covertly usurped. This method of preventing deadlocks is difficult to achieve and can degrade system performance.

(iii) Breaking the request and maintaining the condition: the resource pre-allocation strategy can be implemented. That is, the process requests the system all the resources it needs at once before it runs. If all the resources required by a process are not met, no resources are allocated and the process is not running. Only when the system is able to meet the full resource requirements of the current process will all of the requested resources be allocated to the process at once. Because a running process already occupies all of the resources it needs, it does not take possession of resources and request resources, so deadlocks do not occur. However, in many cases, it is impossible for a process to know all the resources it needs before it executes. This is due to the fact that the process is dynamic and unpredictable at execution time, and that resource utilization is low. No matter when the resource is used, a process can only be executed if it occupies all the resources it needs. Even though some resources are eventually used by the process, the process has been occupying them for the duration of its existence, resulting in a prolonged period of unused conditions. This is obviously a huge waste of resources, reducing the concurrency of the process. Because of the limited resources, coupled with the existence of waste, the number of processes that can be allocated to all the required resources must be less.

(iv) Breaking the cyclic waiting condition: Implementing the strategy of orderly allocation of resources. Using this strategy, the resources are pre-categorized, assigned by number, so that the process does not form a loop when it requests and consumes resources. All process requests for resources must be made strictly in the order in which they are incremented by the resource number. The process takes up a small amount of resources to request a large resource, and no loops are generated, thus preventing deadlocks. This strategy compared with the previous strategy, the utilization of resources and the system throughput has been greatly improved, but there are disadvantages: that is, restricting the process of resource requests, while the system is a reasonable number of all resources is a difficult thing, and increase the system overhead, in order to follow the order of the number of applications, temporary unused resources need to apply in advance , which increases the time it takes for resources to process.

Avoidance of deadlock >>

Deadlock avoidance, which does not restrict the process to request resources for the command, but the process of each request resource issued by the command to dynamically check, and based on the results of the check to determine whether to allocate resources. That is to say, in the process of resource allocation, if the possibility of deadlock is predicted, it is avoided. The key to this approach is to determine the security of the resource allocation.

Detection and recovery of deadlocks >>

Generally speaking, because the operating system has the characteristics of concurrency, sharing and randomness, it is difficult to eliminate the deadlock by means of prevention and avoidance. This requires significant overhead and does not make the best use of resources. A convenient way to do this is for the system to allocate resources for the process without taking any restrictive measures, but providing a means of detecting and unlocking deadlocks: The deadlock can be found and recovered from the deadlock state. Therefore, deadlock detection and recovery methods are often used in the actual operating system to eliminate deadlocks.

Deadlock detection and recovery refers to the system has a special mechanism, when the deadlock occurs, the mechanism can detect the location and cause of the deadlock, and can destroy the necessary conditions of the deadlock through external forces, so that the concurrent process from the deadlock state recovery.


This article is from the "July boreas" blog, please be sure to keep this source http://luminous.blog.51cto.com/10797288/1826513

Causes, prerequisites, and scenario analysis of deadlock generation ·

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.