Deadlock and priority reversal

Source: Internet
Author: User

Deadlock
In a multi-channel Program System, multiple concurrent execution programs share system resources, thus improving resource utilization and system throughput. However, a deadlock may occur. A deadlock is a deadlock formed by multiple processes competing for resources. Without external force, these processes will never be able to move forward.
(1) causes and necessary conditions for deadlock
A. Cause of deadlock
The main reasons for the death rate are as follows:
(1) competitive resources
(2) Improper process Promotion order.

B. Conditions for deadlock
(1) mutex Conditions
A resource can only be used by one process for a period of time, which is exclusive.
(2) request and retention conditions
When a process blocks a request for a new resource, it does not store the acquired resources.
(3) Non-deprivation conditions
Resources obtained by the process cannot be deprived before they are used up. They can only be released by themselves when they are used up.
(4) loop wait Conditions
When a deadlock occurs, there must be a process -------- a circular chain of resources. that is, the process set {p1, p2 ,...., p1 in PN} is waiting for resources occupied by P2, P2 is waiting for resources occupied by P3 ,...., PN is waiting for resources occupied by P1.
As long as the above four conditions are met, the system will experience a deadlock. As long as the above conditions are not met, the system will not experience a deadlock.
C. method for handling deadlocks
Since the emergence of the deadlock status will bring serious consequences to the entire system, the issue of how to solve the deadlock has aroused widespread attention. Currently, the following methods are commonly used:
(1) prevent deadlocks
In order to prevent deadlocks in the system, some constraints are selected at the initial stage of the system design to damage one or several of the four conditions required to generate a deadlock. In this way, there will be no deadlock in the system. This method is very effective for preventing deadlocks, but may reduce the utilization of system resources.
(2) avoid deadlocks
On the one hand, the method to prevent deadlocks will reduce the utilization rate of system resources, and on the other hand, the deadlock must be indicated, so in order to improve the utilization rate of system resources, avoid deadlocks. Avoiding deadlocks does not strictly limit the existence of the conditions necessary for deadlocks. Instead, you can use some method to prevent the system from entering an insecure state during the dynamic resource allocation process, so as to avoid the close and final occurrence of deadlocks.
(3) detect and remove deadlocks
Since the probability of deadlock is always relatively small, in some relatively simple systems, to save or avoid the system overhead added in the deadlock, the system allows the deadlock status. In this system, a detection mechanism is set up to detect deadlocks at any time and determine the processes and resources related to deadlocks, then, remove the deadlock status in the system in an appropriate way.

Two methods are commonly used to remove deadlocks: one is to forcibly cancel some deadlock processes and deprive their resources of other time periods; another is to use a valid suspension and suspension mechanism to suspend some processes, so that some resources can be acquired from the suspended process and used to lift the deadlock.

Deadlock Prevention
(1) Breaking the "request and retention" Conditions
To break the "request and persistence" condition, that is, all resources required during the process are allocated to the process at one time before the process starts to run. If one resource cannot meet the requirement, the process has to wait. In this way, the process no longer needs new resources during the running process. This method is also called the pre-static allocation method.
(2) Breaking the "no deprivation" Condition
Break the "do not deprive" condition, that is, force those processes that request new resources without immediate satisfaction to release other resources it maintains. This means that a process can temporarily release its occupied resources during the running process, that is, allow other processes to deprive them of the resource, thus undermining the appearance of the "no deprivation" condition.
(3) break the "loop wait" Condition
When a deadlock occurs, there must be a loop chain of processes and resources. Breaking the "loop wait" condition is to restrict resource requests during resource allocation, making the loop impossible.

 

1. priority inversion)
Because multi-process resources are shared, the process with the highest priority is blocked by the low-priority process. Instead, the process with the medium priority is executed before the high-priority process, leading to system crash. This is the so-called priority inversion ).

2. Cause
In fact, priority inversion is blocked when a high-priority task (assumed as a) accesses resources occupied by a low-priority task (assumed as C. at this time, when there are tasks with a higher priority than (c) that occupy resources, and tasks with a lower priority than (B) of the blocked task (, A task that occupies a resource is suspended (the occupied resource is still occupied). Because the priority of a task that occupies the resource is very low, it may be suspended by another task all the time. the resources it occupies cannot be released, which causes task a to be unable to execute. tasks with lower priority can be executed.

Therefore, a solution is to increase the priority of a resource-consuming task, so that it can be executed normally, and then release the resource so that task a can obtain the resource and execute it normally.

3. solution (Priority Inheritance/priority ceiling)

Currently, there are many ways to reverse the priority. There are two commonly used methods: Priority Inheritance and priority ceilings ).

A. Priority Inheritance)
Priority Inheritance refers to raising the priority of a low-priority task to the highest priority task waiting for its resources. when a high-priority task is blocked due to waiting for resources, the priority of the resource owner is automatically increased.


B. Priority ceilings)
The priority ceiling refers to raising the priority of a task applying for a resource to the highest priority of all tasks that may access the resource. (This priority is called the priority ceiling of the resource)

Differences between A and B:

Priority Inheritance improves the priority of a resource-occupying task only when the tasks with a low priority are blocked. The priority ceiling increases, regardless of whether the task is blocked or not.

 

1. priority inversion)
Because multi-process resources are shared, the process with the highest priority is blocked by the low-priority process. Instead, the process with the medium priority is executed before the high-priority process, leading to system crash. This is the so-called priority inversion ).

2. Cause
In fact, priority inversion is blocked when a high-priority task (assumed as a) accesses resources occupied by a low-priority task (assumed as C. at this time, when there are tasks with a higher priority than (c) that occupy resources, and tasks with a lower priority than (B) of the blocked task (, A task that occupies a resource is suspended (the occupied resource is still occupied). Because the priority of a task that occupies the resource is very low, it may be suspended by another task all the time. the resources it occupies cannot be released, which causes task a to be unable to execute. tasks with lower priority can be executed.

Therefore, a solution is to increase the priority of a resource-consuming task, so that it can be executed normally, and then release the resource so that task a can obtain the resource and execute it normally.

3. solution (Priority Inheritance/priority ceiling)

Currently, there are many ways to reverse the priority. There are two commonly used methods: Priority Inheritance and priority ceilings ).

A. Priority Inheritance)
Priority Inheritance refers to raising the priority of a low-priority task to the highest priority task waiting for its resources. when a high-priority task is blocked due to waiting for resources, the priority of the resource owner is automatically increased.


B. Priority ceilings)
The priority ceiling refers to raising the priority of a task applying for a resource to the highest priority of all tasks that may access the resource. (This priority is called the priority ceiling of the resource)

Differences between A and B:

Priority Inheritance improves the priority of a resource-occupying task only when the tasks with a low priority are blocked. The priority ceiling increases, regardless of whether the task is blocked or not.

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.