Embedded OS entry notes-use RTX as a case: IX. About priority-flip, inheritance and ceiling

Source: Internet
Author: User
Embedded OS entry notes-use RTX as a case: IX. About priority-flip, inheritance and ceiling

 

When scheduling involves priority, there will be many problems. This article describes the main problems of Priority Scheduling and some coping strategies. There are several main concepts: priority inversion, Priority Inheritance and priority ceiling.


1. priority inversion)

Most RTOS support assigning priority to different processes, which makes scheduling and time management more flexible. However, when some code critical sections are involved, problems may occur. Priority flip is a typical problem. To explain this concept, let's look at a practical example:


First, we should consider three processes: T1, T2, and T3. The priority of T3 is greater than that of T2, and that of T2 is greater than that of T1. The scheduling algorithm is used to interrupt low-priority processes with high priority, and to block high-priority processes in critical zone waiting. Consider that the following events occur in sequence on a timeline:

  1. T1 created
  2. T1 requires entry into the resource R Critical Zone
  3. T2 created
  4. T3 created
  5. T3 requires entering the resource R Critical Zone

Simply reading the text may be abstract. We draw every event:



Check all events from left to right in chronological order. Do you think there is anything wrong?

If we do not find anything inappropriate, let's look at each process. Process 1 is first created, then executed in the critical section, blocked, and then re-executed in the critical section. The blocking time in the middle is the longest for the three processes. Process 2 is created. Process 1 is interrupted when it is created until it is blocked and then executed until the process is eliminated. Process 3 is finally created. process 2 is interrupted when it is created, and the request to enter the critical section is blocked, and then the execution of the critical section is resumed. The blocking time is the second length of the three processes.


The problem occurs here: what is blocking process 3? Process 3 requires the critical zone of resource R. At this time, the critical zone of resource R is occupied by process 1. In the ideal state, process 1 should be executed as soon as possible to exit the critical zone, so that process 3 (current highest priority) can run early. In fact, because process 1 is blocked by process 2 (process 2 has a higher priority than process 1), process 2 blocks process 1 in the critical section until process 2 is completed!


Therefore, process 3 is actually blocked by process 2. low-priority processes are blocked by high-priority processes. This is the issue of priority flip. (Blocking in the critical section does not mean priority blocking, but resource blocking .) In the above example, if we extend the execution time of process 2 infinitely, process 3 will have no way to continue execution. That is to say, ideally, we hope that the maximum blocking time of a high-priority process is limited by the sum of the execution time of the critical section of the low-priority process. In fact, in this simplest setting, the maximum blocking time with high priority is the sum of the execution time in the critical section of the low priority process plus the execution time of the process with a higher priority than that in the critical section.


A simple solution is to disable interruption in the critical section. Although this solution can solve the priority flip problem, we should consider the following situations:


This is equivalent to blocking process 2 and process 3 unconditionally, obviously not the result we want.


2. Priority Inheritance)

A better solution is to prioritize inheritance. Priority Inheritance means that the priority of processes in the critical section is increased to the priority of the highest priority processes in the processes blocked by the process. This may be difficult to say. Let's take a look at how this solution solves the problems we mentioned above:


When process 3 is required to enter the r critical zone for execution, because process 1 is blocked, process 1 is in the critical zone, and process 3 is blocked, at this time, the priority of process 3 will be increased to the priority of process 3, that is, it inherits the priority of process 3. After exiting the critical execution zone, the process 1 priority will be reduced to the original priority.


This solves the problem of unconditional blocking of high-priority processes. In the above example, the blocking time of process 3 is actually only the execution time of process 1 in the critical section.


But this will also cause deadlocks. Let's look at the example below:

  1. T1 created
  2. T1 requires entry into resource a critical zone
  3. T2 created
  4. T2 requires entering resource B Critical Zone
  5. T2. required to enter resource a critical zone
  6. T1 requires entry into resource B Critical Zone

When process 2 requires a critical zone of resource A, because process 1 is in the critical zone of resource A, its priority inherits the priority of process 2, however, after process 1 is executed for a period of time, it is required to enter the resource B critical section to complete the current task and exit the critical section. Both processes are waiting for the other side to exit the critical section, but they do not take the initiative to exit the critical section they occupy, so the deadlock occurs.


3. Priority ceiling)

This policy is used to solve the deadlock problem of priority inheritance. Specifically, define a priority ceiling for each resource. The priority ceiling of a resource is the highest priority for all processes that may need to mutually exclusive use of the resource. This policy only allows a process that meets one condition to enter the critical section. The condition is that the priority of the process isGreater(Equal to not enough), the highest priority of the ceiling of all resources occupied by other processes. Meanwhile, the Priority Inheritance policy is retained. Let's take a look at the above example and how to handle the priority ceiling:


When process 2 wants to enter the critical zone of resource B, it is blocked because the priority of process 2 is not greater than that of other processes (process 1) that occupy resources () the priority ceiling (priority of process 2) is blocked by process 1. At the same time, the priority of process 1 is increased to the priority of process 2 and continues to be executed until the two critical zones exit.


4. Summary

The mutex lock and semaphore of RTX have embedded Priority Inheritance policies, but there is no embedded priority ceiling policy. Therefore, if your Code involves more than two exclusive resources, avoid deadlocks.





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.