1.思维导图
2.重新实现一遍任务调度算法的代码。
(1)抢占式调度
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include <stdio.h>
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
//比默认任务优先级高的参数
osThreadId_t myTask02Handle;
const osThreadAttr_t myTask02_attributes = {
.name = "myTask02",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal1,
};
void StartDefaultTask(void *argument);
void myTask02(void *argument);
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for(;;)
{
printf("StartDefaultTask is start\r\n");
//创建一个任务优先级比StartDefaultTask要高的任务
if(myTask02Handle==NULL)
{
myTask02Handle=osThreadNew(myTask02,NULL,&myTask02_attributes);
}
printf("StartDefaultTask is end\r\n");
osDelay(500);
}
}
void myTask02(void *argument)
{
/* USER CODE BEGIN StartTask02 */
/* Infinite loop */
for(;;)
{
printf("myTask02 is running\r\n");
osDelay(500);
}
/* USER CODE END StartTask02 */
}
(2)时间片轮转
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for myTask02 */
//优先级一样
osThreadId_t StartTask02Handle;
const osThreadAttr_t StartTask02_attributes = {
.name = "StartTask02",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
void StartDefaultTask(void *argument);
void StartTask02(void *argument);
/* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of myTask02 */
StartTask02Handle = osThreadNew(StartTask02, NULL, &StartTask02_attributes);
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
int i=0;
for(;;)
{
printf("StartDefaultTask is start\r\n");
//创建一个任务优先级比StartDefaultTask要高的任务
for(i=0;i<90000000;i++)
{
;//执行,消耗时间
}
printf("StartDefaultTask is end\r\n");
osDelay(500);
}
/* USER CODE END StartDefaultTask */
}
//实现任务2调度函数
void StartTask02(void *argument)
{
/* USER CODE BEGIN StartTask02 */
/* Infinite loop */
for(;;)
{
printf("StartTask02\r\n");
osDelay(500);
}
/* USER CODE END StartTask02 */
}
3.协作式调度:一般使用osDelay实现,两个任务协商运行。