【FreeRTOS学习03】小白都能懂的Task Management 任务管理基本概念介绍
在FreeRTOS中,线程的术语又可以被称之为任务,或许这样更加合适,本文将介绍任务的创建/删除,任务参数的使用,以及任务优先级;
1 软实时和硬实时
硬实时系统的任务运行正确性与响应时限是紧密相关的,一旦超过时限将导致严重的后果,比如导弹控制系统、高铁自动驾驶系统等,都是需要严格的响应时限的。
软实时系统中,虽然也存在时限指标,但是如果输出响应超过时限,一般不会造成严重后果,比如Windows桌面任务,DVD播放机的视频播放。
大多数嵌入式系统不仅能满足硬实时要求,也能满足软实时要求。
软实时:
Windows;Linux系统通常为软实时,当然有补丁可以将内核做成硬实时的系统,不过商用没有这么做的。
硬实时:
VxWorks,uCOS,FreeRTOS,WinCE,RT-thread等实时系统;
2 任务概述
2.1 基本写法
FreeRTOS是多任务的实时系统,其最基本的运行单元为任务,其表示形式为由C语言函数实现的,该函数原型要求必须返回 void,并且带一个 void 类型指针的参数;具体如下所示;
void ATaskFunc(void *args);
每个任务都是在自己权限范围内的一个小程序。其具有程序入口,通常会运行在一个死循环中,也不会退出,具体如下;
void ATaskFunc(void *args){
while(1){
//TODO
}
}
FreeRTOS 任务不允许以任何方式从实现函数中返回——它们绝不能有一条return语句,也不能执行到函数末尾,如果不再需要,则在任务中调用删除任务的API,具体如下所示;
```c
void ATaskFunc(void *args){
vTaskDelete( NULL );
}
2.2 TCB
TCB为任务控制块,或者是线程控制块,另外操作系统中还有PCB为进程控制块,主要封装了一个任务在系统调度中所需要的所有资源,FreeRTOS中TCB的成员,具体如下所示;
任务状态如下所示;
typedef struct xTASK_STATUS
{
/* The handle of the task to which the rest of the information in the structure relates. */
TaskHandle_t xHandle;
/* A pointer to the task's name.*/
const char *pcTaskName;
/* A number unique to the task. */
UBaseType_t xTaskNumber;
/* The state in which the task existed when the structure was populated. */
eTaskState eCurrentState;
/* The priority at which the task was running (may be inherited) when the structure was populated. */
UBaseType_t uxCurrentPriority;
UBaseType_t uxBasePriority;
uint32_t ulRunTimeCounter;
StackType_t *pxStackBase;
uint16_t usStackHighWaterMark;
} TaskStatus_t;
每一个任务都会拥有一个自己的TCB,具体如下图所示;
typedef struct tskTaskControlBlock
{
volatile StackType_t *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
#if ( portUSING_MPU_WRAPPERS == 1 )
xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
#endif
ListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
StackType_t *pxStack; /*< Points to the start of the stack. */
char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
#if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
StackType_t *pxEndOfStack; /*< Points to the highest valid address for the stack. */
#endif
#if ( portCRITICAL_NESTING_IN_TCB == 1 )
UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
#endif
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
#endif
#if ( configUSE_MUTEXES == 1 )
UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
UBaseType_t uxMutexesHeld;
#endif
#if ( configUSE_APPLICATION_TASK_TAG == 1 )
TaskHookFunction_t pxTaskTag;
#endif
#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
#endif
#if( configGENERATE_RUN_TIME_STATS == 1 )
uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
#endif
#if ( configUSE_NEWLIB_REENTRANT == 1 )
struct _reent xNewLib_reent;
#endif
#if( configUSE_TASK_NOTIFICATIONS == 1 )
volatile uint32_t ulNotifiedValue;
volatile uint8_t ucNotifyState;
#endif
/* See the comments above the definition of
tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */
uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
#endif
#if( INCLUDE_xTaskAbortDelay == 1 )
uint8_t ucDelayAborted;
#endif
} tskTCB;
3 任务状态
任务顶层存在两种状态,运行态和非运行态;
但是非运行的任务状态又可以分为:堵塞状态 / Blocked,挂起状态 / Suspend,就绪状态 / Ready,下面简单做一下介绍;
运行状态 /
Running
运行态的任务完全占用CPU的使用权,如果当前CPU只有一个内核,那么在某个时刻只能运行一个任务,就是所谓的单核单线程;堵塞状态 /
Blocked;
用户可以主动调用vTaskDelay(T)将任务进入堵塞状态,直到任务堵塞时间已经达到T;或者该任务在等待队列,信号量,事件组,通知或信号量事件时,也将处于堵塞状态;处于堵塞状态的任务不再占用CPU,同样也不能直接进入运行状态,而是先进入就绪状态;挂起状态 /
Suspend;
任何状态的下的任务都可以通过调用vTaskSuspend函数进入挂起状态,并且无法直接进入运行态,只能通过调用xTaskResume函数进入就绪状态;就绪状态 /
Ready;
被抢占的任务将处于就绪状态,挂起的任务被回复的会处于就绪状态,堵塞的任务收到相应事件也会处于就绪状态,如果当前没有更高优先级的任务处于运行,则当前就绪状态的任务进入运行状态;
其主要的状态转换关系如下图所示;

FreeRTOS使用一个枚举封装了任务的状态,具体如下所示;
typedef enum
{
eRunning = 0,/* A task is querying the state of itself, so must be running. */
eReady, /* The task being queried is in a read or pending ready list. */
eBlocked, /* The task being queried is in the Blocked state. */
eSuspended, /* The task being queried is in the Suspended state*/
eDeleted, /* The task being queried has been deleted, but its TCB has not yet been freed. */
eInvalid /* Used as an 'invalid state' value. */
} eTaskState;
4 任务优先级
FreeRTOS的最低优先级是0,分配给了空闲任务,空闲任务的优先级使用宏定义tskIDLE_PRIORITY进行表示,最大的优先级为需要用户进行配置;在FreeRTOS.h中可以看到预编译指令,具体如下;
#ifndef configMAX_PRIORITIES
#error Missing definition: configMAX_PRIORITIES must be defined in FreeRTOSConfig.h. \
See the Configuration section of the FreeRTOS API documentation for details.
#endif
#if configMAX_PRIORITIES < 1
#error configMAX_PRIORITIES must be defined to be greater than or equal to 1.
#endif
configMAX_PRIORITIES需要用户在FreeRTOSConfig.h进行定义,则任务最大的优先级为configMAX_PRIORITIES - 1。
5 相关函数
任务管理的函数声明和一些基本类型都封装在源码tasks.h中;
5.1 创建任务
使用xTaskCreate创建一个任务,具体如下所示;
// Task to be created.
void vTaskCode( void * pvParameters )
{
for( ;; )
{
// Task code goes here.
}
}
// Function that creates a task.
void vOtherFunction( void )
{
static uint8_t ucParameterToPass;
TaskHandle_t xHandle = NULL;
// Create the task, storing the handle. Note that the passed parameter ucParameterToPass
// must exist for the lifetime of the task, so in this case is declared static. If it was just an
// an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
// the new task attempts to access it.
xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
configASSERT( xHandle );
// Use the handle to delete the task.
if( xHandle != NULL )
{
vTaskDelete( xHandle );
}
}
5.2 函数删除
使用函数vTaskDelete对函数进行删除;
void vTaskDelete( TaskHandle_t xTaskToDelete ) PRIVILEGED_FUNCTION;
void vOtherFunction( void )
{
TaskHandle_t xHandle;
// Create the task, storing the handle.
xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
// Use the handle to delete the task.
vTaskDelete( xHandle );
}
5.3 堵塞任务
使用vTaskDelay函数可以将任务堵塞一定时间;
void vTaskDelay( const TickType_t xTicksToDelay ) PRIVILEGED_FUNCTION;
void vTaskFunction( void * pvParameters )
{
// Block for 500ms.
const TickType_t xDelay = 500 / portTICK_PERIOD_MS;
for( ;; )
{
// Simply toggle the LED every 500ms, blocking between each toggle.
vToggleLED();
vTaskDelay( xDelay );
}
}
5.4 挂起和恢复
使用函数vTaskSuspend可以将函数挂起,通过vTaskResume(xHandle)函数可以将挂起的函数恢复到就绪状态;
void vTaskSuspend( TaskHandle_t xTaskToSuspend ) PRIVILEGED_FUNCTION;
void vAFunction( void )
{
TaskHandle_t xHandle;
// Create a task, storing the handle.
xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
// ...
// Use the handle to suspend the created task.
vTaskSuspend( xHandle );
// ...
// The created task will not run during this period, unless
// another task calls vTaskResume( xHandle ).
//...
// Suspend ourselves.
vTaskSuspend( NULL );
// We cannot get here unless another task calls vTaskResume
// with our handle as the parameter.
}
6 总结
先了解任务之前的状态,先学会使用FreeRTOS的常用接口,后续对于其调度算法和内存管理的算法可以分析和学习一下。
文中难免有错误和纰漏之处,请大佬们不吝赐教
创作不易,如果本文帮到了您;
请帮忙点个赞【FreeRTOS学习03】小白都能懂的Task Management 任务管理基本概念介绍的更多相关文章
- 【FreeRTOS学习04】小白都能懂的 Queue Management 消息队列使用详解
消息队列作为任务间同步扮演着必不可少的角色: 相关文章 [FreeRTOS实战汇总]小白博主的RTOS学习实战快速进阶之路(持续更新) 文章目录 相关文章 1 前言 2 xQUEUE 3 相关概念 3 ...
- 搭建分布式事务组件 seata 的Server 端和Client 端详解(小白都能看懂)
一,server 端的存储模式为:Server 端 存 储 模 式 (store-mode) 支 持 三 种 : file: ( 默 认 ) 单 机 模 式 , 全 局 事 务 会 话 信 息 内 存 ...
- 【FreeRTOS学习05】深度解剖FreeRTOSConfig.h实现对系统的自定义剪裁
ROM/RAM太小,因此要对系统进行剪裁: 相关文章 [FreeRTOS实战汇总]小白博主的RTOS学习实战快速进阶之路(持续更新) 文章目录 相关文章 1 系统的剪裁 2 FreeRTOSConfi ...
- 【FreeRTOS学习02】源码结构/数据类型/命名规则总结
个人不是很喜欢FreeRTOS的编程风格,但是没办法,白嫖人家的东西,只能忍了,这里先简单总结一下: 相关文章 [FreeRTOS实战汇总]小白博主的RTOS学习实战快速进阶之路(持续更新) 文章目录 ...
- JavaScript学习03 JS函数
JavaScript学习03 JS函数 函数就是包裹在花括号中的代码块,前面使用了关键词function: function functionName() { 这里是要执行的代码 } 函数参数 函数的 ...
- Java虚拟机JVM学习03 连接过程:验证、准备、解析
Java虚拟机JVM学习03 连接过程:验证.准备.解析 类被加载后,就进入连接阶段. 连接就是将已经读入到内存的类的二进制数据合并到虚拟机的运行时环境中去. 连接阶段三个步骤:验证.准备和解析. 类 ...
- WEB架构师成长之路-架构师都要懂哪些知识 转
Web架构师究竟都要学些什么?具备哪些能力呢?先网上查查架构师的大概的定义,参见架构师修炼之道这篇文章,写的还不错,再查查公司招聘Web架构师的要求. 总结起来大概有下面几点技能要求: 一. 架构师有 ...
- WEB架构师成长之路之三-架构师都要懂哪些知识
Web架构师究竟都要学些什么?具备哪些能力呢?先网上查查架构师的大概的定义,参见架构师修炼之道这篇文章,写的还不错,再查查公司招聘Web架构师的要求. 总结起来大概有下面几点技能要求: 一. 架构师有 ...
- ThinkPhp学习03
原文:ThinkPhp学习03 一.ThinkPHP 3 的输出 (重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出 想分配变量可以使用as ...
随机推荐
- 2019应届生,用python爬虫记录自己求职经历,分享求职中的一些坑
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...
- ASE课程总结 by 冯晓云
开始的开始,采访往届ASE班的blog:http://www.cnblogs.com/legs/p/4894362.html 和北航软工M1检查:http://www.cnblogs.com/legs ...
- PrestoSPI安全扩展
由于Presto官方文档和谷歌搜索都没有相关的内容,git项目中也没有支持sentry的安全插件扩展,因此只能从源码中寻找答案,在梳理完SPI包的安全相关源码结构后,已实现了一个自定义的安全插件,经验 ...
- JavaScript之浅谈内存空间
JavaScript之浅谈内存空间 JavaScipt 内存自动回收机制 在JavaScript中,最独特的一个特点就是拥有自动的垃圾回收机制(周期性执行),这也就意味者,前端开发人员能够专注于业余, ...
- 5、flink常见函数使用及自定义转换函数
代码地址:https://gitee.com/nltxwz_xxd/abc_bigdata 一.flink编程方法 获取执行环境(execution environment) 加载/创建初始数据集 对 ...
- 前端基础-HTML(1)
1.浏览器: 1.1 浏览器内核: 渲染引擎和JS引擎 渲染引擎:负责页面内容的在(html,xml,图像等).整理讯息(加入css等),以及计算网页的显示方式,然后输出至显示器后者打印机 JS引擎: ...
- phpstorm破解版
查看下载:https://www.7down.com/soft/229568.html 破解:https://www.7down.com/article/305640.html 主题更换和下载:htt ...
- fail-safe fail-fast知多少
目录 简介 Fail-fast Iterator Fail-fast 的原理 Fail-safe Iterator 总结 fail-safe fail-fast知多少 简介 我们在使用集合类的时候,通 ...
- C# 基础知识系列- 14 IO篇 文件的操作
0. 前言 本章节是IO篇的第二集,我们在上一篇中介绍了C#中IO的基本概念和一些基本方法,接下来我们介绍一下操作文件的方法.在编程的世界中,操作文件是一个很重要的技能. 1. 文件.目录和路径 在开 ...
- java 之 javaBean
什么是JavaBean? JavaBean是特殊的Java类,使用J ava语言书写,并且遵守JavaBean API规范. JavaBean与其它Java类相比而言独一无二的特征: 提供一个默认的无 ...