当前位置:网站首页>[FreeRTOS] 08 mutex semaphores and priority inversion
[FreeRTOS] 08 mutex semaphores and priority inversion
2022-06-29 10:50:00 【xiaobaibai_ two thousand and twenty-one】
This section continues freeRTOS The amount of signal , Let's start with a classic problem related to preemptive scheduling and semaphores —— Priority reversal , Let's talk about mutually exclusive semaphores .
1) What is priority reversal
Suppose such a situation :
In an operating system with preemptive scheduling , There are three tasks A、B、C, Their priority from high to low is A>B>C; Mission A and C You will need to obtain semaphores in S. See the following figure for an example of priority inversion :

initial 0 moment , The lowest priority task C Running , And get the semaphore S;
after , stay 1 moment , High priority tasks A Enter the ready state , Because it has the highest priority , Take over CPU,A Began to run ;
To 2 moment , Mission A Execute to acquire semaphore S when , It is found that the semaphore has been occupied , So blocked waiting ; Mission A After being blocked ,C Restart execution ;
To 3 moment , Medium priority tasks B Entered the ready state , Because of the mission B Task comparison C High priority , be B Take over CPU,B Enter the running state ;
Only when the task B completion of enforcement , Mission C To get the right to execute ; Mission C Task after releasing semaphore A To perform .
At this time , And that's what happened High priority tasks A, Because semaphores S( Or other resources ) The relationship between , You have to wait for a medium priority task B performed , Can be executed . This phenomenon is called priority inversion . If the mission B It takes a long time , Then it will lead to high priority A It also takes a long time .
Priority reverse transfer causes low priority tasks to be executed before high priority tasks , Poor real-time performance of high priority tasks .
2) How to solve priority inversion
that , How to solve the priority inversion problem ? There are generally two ways : Priority ceiling and priority inheritance .
Priority inheritance : When a high priority task appears A Need to wait for low priority tasks C When using resources , take C The priority of is raised to A equally ( When there are multiple tasks waiting , Raise to the highest priority of the task waiting for it to consume resources ).
Priority ceiling : When low priority tasks C When using resources , hold C The priority of is raised to the highest priority of the task that may access the resource ( This is called the resource priority ceiling ).
The core idea of these two methods , It is to temporarily increase the priority of tasks occupying semaphores , So that it will not be arbitrarily preempted ; The difference is that priority inheritance only increases the priority of low priority tasks when high priority tasks are blocked , The priority ceiling raises its priority when low priority tasks acquire resources .
We can use the following two functions to modify 、 Priority of the recovery task :
UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask ); // Gets the priority of the task
void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority ); // Modify the priority of the task
such as , To avoid priority inversion , We use the priority ceiling method :C When the task gets the semaphore , hold C The priority of the task is raised to A The task is the same ; When releasing the semaphore , And then C Change the priority of the task to the original one .
3) Mutex semaphore
freeRTOS Mutex semaphores provided , Is a special binary semaphore :
First , It has the same function as binary semaphore , Mutual exclusive access to resources can be realized (A Got B No more access , Only when the A Other tasks can only get after releasing );
secondly , it It has its own priority inheritance function , When mutex semaphores have been used by low priority tasks C obtain , If high priority tasks A You need to wait for this semaphore , The task will be automatically C To the same priority as yourself .
The function of mutually exclusive semaphores makes it possible to avoid the priority inversion problem .
Of mutually exclusive semaphores Create using the following two functions :
SemaphoreHandle_t xSemaphoreCreateMutex( void )
SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer )
Release semaphore 、 Get the function of semaphore and count semaphore mentioned before 、 The binary semaphores are the same , The function will automatically determine whether it is a mutually exclusive semaphore , If it is , It performs priority inheritance related adjustments .
Release semaphore :
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore ); // Release semaphore
BaseType_t xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken); // Release semaphores from interrupts , The return value of the latter parameter indicates whether a high priority task is ready , If there is , You need to switch tasks
Acquisition semaphore :
BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait ); // Acquisition semaphore , The latter parameter is the blocking timeout
BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore, BaseType_t *pxHigherPriorityTaskWoken); // Get semaphore in interrupt
4) Programming test
Let's use two examples to demonstrate priority inversion , And using mutually exclusive semaphores to avoid priority inversion .
First, let's look at the experiment of priority reversal :
We define three tasks :
DefaultTask The lowest priority ,Task02 Medium priority ,Task03 The highest priority :

stay Defaulttask Tasks , Running 0 moment , After defining the binary semaphore , Immediately occupy the semaphore 100ms, Then release :
Mission Task02 stay 50ms when , Start execution , It needs to be implemented 500ms, Independent of semaphore :

Task03 stay 100ms Start running when , It needs to wait for semaphores :

The results are shown in the following figure :

You can find , Start time to 50ms when ,defaulttask After the semaphore is occupied , By Task02 Take over CPU;
here we are 100ms,Task03 Run after starting , It has the highest priority ; But it runs until it waits for the semaphore , Because you can't get , I entered a blocking state ;
After that, because the highest priority task is Task02, It has been occupying 500ms Of CPU, Until it releases CPU,defaulttask To be implemented ;
Defaulttask After releasing the semaphore ,Task03 To be implemented .
It can be seen that due to the influence of semaphore , Priority reversal occurred , The highest priority task A Until a medium priority task B Only after the execution can we execute .
Examples of priority inheritance functions for mutually exclusive semaphores
Next , We replace the binary semaphores in the above example with mutually exclusive semaphores , Let's take a look at the execution results .
Because mutually exclusive semaphores are different from binary semaphores only when they are defined , So the code in the above example only needs to modify the semaphore definition line :

Then look at the execution results :

You can see , In this case ,Task03 Execute to wait for semaphore ,defaulttask The semaphore is given directly ;
The one with the lowest priority defaulttask Not because task02 Take up CPU But can't , It means that its priority has been temporarily raised , It plays the role of priority inheritance , Mutually exclusive semaphores are effective in solving the priority inversion problem .
Okay , That's all for this section .
If you find it useful, you can pay attention to the author's wechat “ Xiaobai learns Electronics ”, You can find the code and data download address in the official account :

边栏推荐
- 他98年的,我玩不过他...
- 你的项目需要自动化测试吗?
- Reading notes of CLR via C -clr boarding and AppDomain
- Ora-01950 does not have permission on tablespace
- The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month
- 如何优雅的写 Controller 层代码?
- By asp Net core downloading files according to the path
- 二叉树
- Comment terminer rapidement une partition de disque
- 高效工作必备:测试人如何提高沟通技能?
猜你喜欢
随机推荐
Reading notes of CLR via C -clr boarding and AppDomain
UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. note
Summary after reading how to read a Book
&和&&的区别
JS post download file
Agctfb partial solution
Getting started with the lvgl Library - Animation
1-数据库了解
区域工业互联网市场成绩单,百度智能云开物第二
With this tool, automatic identification and verification code is no longer a problem
Cs231n-2022 module1: overview of key points of neural network (2)
拼图小游戏中学到的Graphics.h
Reprint: five methods to determine whether an object has attributes
美国EB-5移民再现利好,区域中心再授权政策被叫停
Daily question brushing record (VII)
AGCTFb部分题解
Alibaba cloud server is installed and configured with redis. Remote access is unavailable
有人遇到FlinkCdc同步MySQL时候出现的这个问题吗?
How to quickly complete disk partitioning
Real test = "half product + Half development"?









