一、优先级翻转
void Led_Task(void const * argument)
{
/* USER CODE BEGIN Led_Task */
/* Infinite loop */
for(;;)
{
printf("Normal Task is Runing\r\n");
osDelay(500);//1ms时基
}
/* USER CODE END Led_Task */
}
void Delay_Task(void const * argument)
{
/* USER CODE BEGIN Delay_Task */
uint32_t i;
/* Infinite loop */
for(;;)
{
printf("Low Task Take Sem\r\n");
if(xSemaphoreTake(PrBinarySemHandle,portMAX_DELAY) == pdPASS){
printf("Low Task is Runing\r\n");
}
for(i=0;i<2000000;i++){
taskYIELD();
}
printf("Low Task Give Sem\r\n");
xSemaphoreGive(PrBinarySemHandle);
osDelay(500);
}
/* USER CODE END Delay_Task */
}
/* USER CODE BEGIN Header_High_Task */
/**
* @brief Function implementing the HighTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_High_Task */
void High_Task(void const * argument)
{
/* USER CODE BEGIN High_Task */
/* Infinite loop */
for(;;)
{
printf("High Task Take Sem\r\n");
if(xSemaphoreTake(PrBinarySemHandle,portMAX_DELAY) == pdPASS){
printf("High Task is Runing\r\n");
}
printf("High Task Give Sem\r\n");
xSemaphoreGive(PrBinarySemHandle);
osDelay(500);
}
/* USER CODE END High_Task */
}
二、互斥信号量
互斥信号量
1.普通互斥信号量
void Led_Task(void const * argument)
{
/* USER CODE BEGIN Led_Task */
/* Infinite loop */
for(;;)
{
printf("Normal Task is Runing\r\n");
osDelay(500);//1ms时基
}
/* USER CODE END Led_Task */
}
void Delay_Task(void const * argument)
{
/* USER CODE BEGIN Delay_Task */
uint32_t i;
/* Infinite loop */
for(;;)
{
printf("Low Task Take Mutex\r\n");
if(xSemaphoreTake(PrMutexHandle,portMAX_DELAY) == pdPASS){
printf("Low Task is Runing\r\n");
}
for(i=0;i<2000000;i++){
taskYIELD();
}
printf("Low Task Give Mutex\r\n");
xSemaphoreGive(PrMutexHandle);
osDelay(500);
}
/* USER CODE END Delay_Task */
}
void High_Task(void const * argument)
{
/* USER CODE BEGIN High_Task */
/* Infinite loop */
for(;;)
{
printf("High Task Take Mutex\r\n");
if(xSemaphoreTake(PrMutexHandle,portMAX_DELAY) == pdPASS){
printf("High Task is Runing\r\n");
}
printf("High Task Give Mutex\r\n");
xSemaphoreGive(PrMutexHandle);
osDelay(500);
}
/* USER CODE END High_Task */
}
2.递归互斥信号量
void Led_Task(void const * argument)
{
/* USER CODE BEGIN Led_Task */
/* Infinite loop */
for(;;)
{
printf("Normal Task is Runing\r\n");
osDelay(500);//1ms时基
}
/* USER CODE END Led_Task */
}
void Delay_Task(void const * argument)
{
/* USER CODE BEGIN Delay_Task */
uint32_t i;
/* Infinite loop */
for(;;)
{
printf("Low Task Take RecursiveMutex\r\n");
if(xSemaphoreTakeRecursive(myRecursiveMutexHandle,portMAX_DELAY) == pdPASS){
printf("Low Task is Runing\r\n");
}
for(i=0;i<2000000;i++){
taskYIELD();
}
printf("Low Task Give RecursiveMutex\r\n");
xSemaphoreGiveRecursive(myRecursiveMutexHandle);
osDelay(500);
}
/* USER CODE END Delay_Task */
}
void High_Task(void const * argument)
{
/* USER CODE BEGIN High_Task */
/* Infinite loop */
for(;;)
{
printf("High Task Take RecursiveMutex 1\r\n");
if(xSemaphoreTakeRecursive(myRecursiveMutexHandle,portMAX_DELAY) == pdPASS){
printf("High Task is Runing\r\n");
}
printf("High Task Take RecursiveMutex 2\r\n");
if(xSemaphoreTakeRecursive(myRecursiveMutexHandle,portMAX_DELAY) == pdPASS){
printf("High Task is Runing\r\n");
}
printf("High Task Give RecursiveMutex 1\r\n");
xSemaphoreGiveRecursive(myRecursiveMutexHandle);
printf("High Task Give RecursiveMutex 2\r\n");
xSemaphoreGiveRecursive(myRecursiveMutexHandle);
osDelay(500);
}
/* USER CODE END High_Task */
}
总结
二值信号量解决资源分配,普通信号量解决优先级翻转(普通两次获取会死锁),递归解决死锁