To investigate the deadlock problem

Source: Internet
Author: User
Tags rollback

The main points of this article:

1. The concept of deadlock

2. Why the deadlock is generated.

3. Deadlock conditions.

4. How to resolve the deadlock.


1. Concept of deadlock:

Defined as a "permanent" block between a set of competing system resources or processes that communicate. You can generalize that all deadlock issues involve conflicting resource requirements between two or more processes.

If you look at the above concept, you may still not be able to understand what is called a deadlock.

So we'll explain it in layman's words.


2. Why does the deadlock occur?

Deadlock: Refers to two or more than two processes (threads) in the execution process, because of contention for resources caused by a mutual wait for the phenomenon, without external forces, they will not be able to push down.

It is said that the system is in a deadlock state or the system has a deadlock, and these processes (threads) that are always waiting for each other are called deadlock processes (threads).


With the deadlock problem, we need to find a way to solve the problem.


--------------------------------------------------------------------------------------------------------------- -----------------------3. Deadlock-generated four essential requirements:
①, mutual exclusion. (Only one process can use one resource at a time, and other processes cannot access resources that have been assigned to other processes.)
②, possession and waiting. (Request and persist) when a process waits for another process (other resources), it continues to occupy the allocated resources.
③, not preemption. You cannot forcibly preempt a resource already occupied by a process. (When a process takes precedence over a process that occupies a resource, it cannot be preempted). The first three conditions are only necessary for the existence of deadlocks, but they are not sufficient conditions. A fourth condition is required for the creation of deadlocks.
④, Cycle waiting. (depending on the process request and the order in which resources are freed) there is a closed process chain, which makes each process occupy at least one resource required by the next process in the chain. As shown in figure:

These four conditions are linked together to form a sufficient and necessary condition for deadlock.


--------------------------------------------------------------------------------------------------------------- ----------

4, how to solve the deadlock.
Three solutions in the operating system: deadlock detection, prevention, and avoidance.
①, Prevention: Resource allocation Strategy: Conservative, pre-submission resources. Use a strategy to eliminate the occurrence of one of the 4 conditions above to prevent deadlocks. Prevention

②, avoid: In the detection and prevention of intermediate (do not need to preempt) based on the current state of resource allocation to do dynamic selection to avoid deadlock.

③, detection: very free, whenever possible, the requested resources are allowed.

An attempt was made to detect the presence of a deadlock and attempt to recover from the deadlock.


The following diagram is a summary of the above three methods:




Try to break one of four conditions:


Break mutually exclusive conditions: If a resource is shared for multiple processes and can be accessed concurrently, no deadlock occurs. But generally speaking, there must be mutually exclusive access to a resource, so this condition is generally impossible to break. If a resource is required to be mutually exclusive access, the operating system must support mutexes.
Prevention of occupancy and waiting conditions: To prevent this condition, the process can request all required resources at once and block the process until all requests are met. This approach is inefficient in two ways. * A process may be blocked for a long time to wait for all of its resource requests to be met. In fact, as long as there is a portion of the resources, it can continue to execute. * Resources that are allocated to processes may not be used for a considerable period of time, and in the meantime they cannot be used by other processes. * A process may not know in advance all the resources it needs.
Prevent non-preemption conditions: ① If a process that occupies some resources makes further resource requests rejected, the process must release the resources it originally possessed and, if necessary, request those and additional resources again. ②, if a process requests a resource currently occupied by another process, the operating system can preempt another process and require it to release resources. (Requires that processes have different priorities). This approach is practical only if the resource state can be easily saved and restored.

Prevent loop wait conditions: You can prevent by defining the linear order of resource types. If a process is already assigned to a resource of type R, then the resource that it requests next can only be those types of resources that are ranked after the R type.

--------------------------------------------------------------------------------------------------------------- -------

another way to resolve the deadlock problem is deadlock avoidance. (Distinguishing between deadlock prevention)
In deadlock prevention, prevent the occurrence of at least one of the 4 deadlock conditions by constraining resource requests. This can be done indirectly by preventing one of the three necessary policy conditions from occurring, or by preventing loop waiting, but this can lead to inefficient resource usage and inefficient process execution.
Deadlock avoidance In contrast, it allows three of the necessary conditions, but with judicious choice to ensure that the deadlock point is never reached, deadlock avoidance allows more concurrency than deadlock prevention.
In the deadlock avoidance, whether the current resource-allocation request is allowed is determined by determining whether the request can lead to a deadlock. Therefore, deadlocks avoid the need to know about future requests for process resources.
Two methods of deadlock avoidance: * If a process's request causes a deadlock, the process is not started. * This assignment is not allowed if an increased resource request from a process causes a deadlock.
The advantage of deadlock avoidance is that it does not require preemption and rollback processes in deadlock prevention and is less restrictive than deadlock prevention. However, it has many limitations in its use: * You must declare the maximum resources requested by each process in advance. * The processes to be considered must be irrelevant, that is, they must be executed in the order that they are not constrained by any synchronization requirements. * The number of resources allocated must be fixed. * The process cannot exit when the resource is in possession.


Deadlock detection: Deadlock prevention policies are very conservative, and they resolve deadlock problems by restricting access to resources and imposing constraints on the process.
The deadlock detection policy is quite the opposite, and it does not restrict resource access or constraint process behavior. For deadlock detection, the requested resource is authorized to the process whenever possible. The operating system periodically executes an algorithm to detect the previous loop wait condition.


Recovery: Once a deadlock is detected, a policy is required to restore the deadlock. The following list of possible methods: 1. Cancel all deadlock processes. (The longest used in the operating system) 2, roll back each deadlock process to some of the previously defined checkpoints, and restart all processes. This requires that the rollback and restart mechanisms be constructed in the system. The risk of this method is that the original deadlock may occur again. However, the uncertainty of the concurrency process usually guarantees that this will not happen. 3. Cancel the deadlock process continuously until there is no longer a deadlock. The order in which the cancellation process is chosen is based on a minimum cost principle. After each cancellation, the detection algorithm must be called back to test for the existence of a deadlock. 4, continuous preemption of resources until there is no longer deadlock. You need to use a cost-based selection method, and you need to recall the detection algorithm after each preemption. A process in which a resource is preempted must be rolled back to a state prior to obtaining the resource.
For the selection algorithms in 3 and 4 above, you can use one of the following: * So far the minimum amount of processor time is consumed * so far the output is at least * so far the minimum number of resources allocated is the least * priority.


A comprehensive deadlock strategy: all of the above strategies for resolving deadlocks have their advantages and disadvantages, and instead of designing an operating system to adopt only one of these strategies, it is better to use different policies in different situations. * Divide resources into groups of different resource classes. * To prevent deadlocks between resource classes due to cyclic waiting, you can use the linear sorting strategy defined earlier. * In a resource class, use the algorithm that is most appropriate for this class.

As an example of this technique, consider the following resource classes: * exchangeable space: A block of memory used in a process exchange. You can use the following strategy: to prevent deadlocks by requiring a one-time allocation of all the requested resources, just as you would occupy and wait for a prevention policy. This strategy is reasonable if you know the maximum storage requirements. Deadlock avoidance is also possible.
* Process resources: Devices that can be allocated, such as tape devices and files. Strategies you can use: For such resources, deadlock avoidance strategies are often effective because processes can declare in advance which resources they will need. It is also possible to adopt a precautionary strategy of resource sequencing. * Memory: Can be allocated to the process by page or by segment. For memory, preemption based prevention is the most appropriate strategy. When a process is preempted, it is simply swapped to the external memory to free up space to resolve the deadlock.
Internal resources: such as I/O channel. You can use a resource-sorted prevention strategy.

--------------------------------------------------------------------------------------------------------------- -----------------------

Summarize:

A deadlock is a phenomenon in which a group of competing system resources or processes that communicate with each other are blocked. Blocking is permanent unless the operating system takes certain extraordinary actions, such as killing one or more processes, or forcing one or more processes to return along the original path.
There are usually three ways to handle deadlocks: prevention, detection, and avoidance. Deadlock prevention is not satisfied by destroying one of the three necessary requirements of a deadlock, thus ensuring that no deadlock occurs. Deadlock detection is required if the operating system is always requested by the same resource. The operating system must periodically detect deadlocks and take action to destroy the deadlock.

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.