当前位置:网站首页>[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 :

边栏推荐
猜你喜欢

【FreeRTOS】08 互斥信号量、优先级反转问题

Offensive and defensive world re insfsay

Bug的描述、定级、生命周期

Essential for efficient work: how can testers improve their communication skills?

全面理解Volatile关键字

What happened during the MySQL installation?

SQL Server 数据库的连接查询

Linux下Redis安装及集群搭建

Vmware的下载与安装(基本思路+详细过程)

由ASP.NET Core根据路径下载文件异常引发的探究
随机推荐
Mongodb tutorial Chapter 02 mongodb installation
【C语言进阶】文件操作(二)
Offensive and defensive world re insfsay
C language printf family
Vmware的下载与安装(基本思路+详细过程)
Ikvm Net project progress
1- database understanding
Analysis on the specific execution process of an insert statement in MySQL 8.0 (2)
CLR via C reading notes - loading and AppDomain
The use and difference of watch listening and computed calculation attributes
Cs231n-2022 module1: overview of key points of neural network (2)
Print 9*9 multiplication formula table (C language)
October 17, 2020: question brushing 1
Design of intelligent test paper generation system
企业竞争分析的几种方法:SWOT、波特五力、PEST「建议收藏」
flink sql cdc 并行度 会不会影响顺序呀,是不是数据同步的话一般只能设置1。
共2600页!又一份神级的面试手册面世~
BUUCTF--reverse2
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
AGCTFb部分题解