In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to achieve LiteOS task management". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve LiteOS task management"!
1. Task management of LiteOS kernel
The Huawei LiteOS kernel provides the functions of task creation, deletion, delay, suspension and recovery, as well as locking and unlocking task scheduling, and supports preemptive scheduling of tasks according to priority and rotation scheduling of time slices with the same priority.
1.1. Task
In LiteOS, a task is a thread, and multiple tasks are preemptively scheduled according to priority to achieve the purpose of running multiple tasks "at the same time".
1.2. Status of the task
Each task in the Huawei LiteOS system has multiple running states. When the system initialization is completed and the scheduler is started, all the tasks created in the system are scheduled by the kernel, switch between different running states, and compete for certain resources in the system.
There are four states of a task:
Ready: this task is in the ready list and only waits for CPU
Run (Running): this task is in progress
Blocked: this task is not in the ready list. Includes tasks that are suspended, tasks that are delayed, tasks that are waiting for semaphores, read-write queues, or read-write events, etc.
Dead: the task ends and waits for the system to reclaim resources.
1.3. Task ID
The task ID is returned to the user through parameters when the task is created, as a very important identity of the task.
Users can perform task suspension, task recovery, query task name and other operations on a specified task through the task ID.
1.4. Task priority
Priority represents the priority in which tasks are executed. The priority of the task determines the task to be executed when the task switch occurs, and the task with the highest priority in the ready list will be executed.
Huawei LiteOS tasks have a total of 32 priorities (0-31), with the highest priority being 0 and the lowest priority being 31.
Because the kernel of LiteOS is a preemptive scheduling kernel, so:
A high-priority task can interrupt a low-priority task, and a low-priority task can only be scheduled after the high-priority task is blocked or terminated.
1.5. Task entry function
The task entry function is the function that will be executed after each new task is scheduled, which is implemented by the user and specified by the task creation structure when the task is created.
1.6. The mechanism behind multitasking
In the kernel of multitasking operating system, in order to manage each task conveniently, each task has a task control block (TCB), which contains task context stack pointer (stack pointer), task status, task priority, task ID, task name, task stack size and other information. TCB is equivalent to the ID card of each task in the kernel and can reflect the running situation of each task.
So, how are so many tasks in the operating system managed by the system depending on TCB?
In fact, each task is equivalent to a bare metal program, and each task is independent of each other. This "independence" means that each task has its own running environment-stack space, which is called task stack. The information stored in the stack space includes local variables, registers, function parameters, function return addresses, and so on.
However, there is only one CPU in the system. Even if the task stack of each task is independent, multiple tasks need to be executed by the same CPU. The resources of CPU are shared.
Yes, the resources of CPU are shared by multiple tasks, and these CPU registers are used only when the task is executed, which is called the task context. Therefore, when the task is switched, the kernel will save the context information of the cut-out task in its own task stack space, so that the task can be restored to the scene when the task is restored, so that execution continues at the cut-out point after the task is restored.
When a user creates a task, the system will first apply for the memory space required by the task control block. After the application is successful, the system will initialize the task stack and preset the context. In addition, the system will also put the "task entry function" address in the appropriate location. In this way, the "task entry function" will be executed the first time the task starts into the running state.
two。 Task Management API
The Huawei LiteOS task management module provides the functions of task creation, task deletion, task delay, task suspension and task recovery, changing task priority, locking task scheduling and unlocking task scheduling, querying task ID according to task control block and task control block information according to ID.
The API provided by Huawei LiteOS task management starts with LOS, but it is complicated to use these API. Therefore, we use the unified API interface provided by Huawei IoT Link SDK to experiment. The underlying layer of these interfaces has been implemented using API provided by LiteOS, which is more concise for users. The list of API is as follows:
The api API of osal is declared in, and the header file needs to be included to use the related API. For more information on the parameters of the function, please refer to the declaration of the header file.
Task-related interfaces are defined in osal.c, and LiteOS-based interfaces are implemented in liteos_imp.c files:
API name function description osal_task_create creation task osal_task_kill delete task (non-self) osal_task_exit task exits osal_task_sleep task hibernation 2.1. Osal_task_create
The interface of osal_task_create is used to create a task, and the interface prototype is as follows:
Void* osal_task_create (const char * name,int (* task_entry) (void* args),\ void* args,int stack_size,void * stack,int prior) {void* ret = NULL If ((NULL! = s_os_cb) & & (NULL! = sroomosroomcb-> ops) & & (NULL! = scompresosroomcb-> ops- > task_create)) {ret = scompresosroomcb-> ops- > task_create (name, task_entry,args,stack_size,stack,prior);} return ret;}
The parameters of this API are described in the following table:
Parameter name parameter description name task name tsak_entry task entry function function pointer args task entry function parameter list stack_size task stack size stack task stack address prior task priority return value task ID2.2. Osal_task_kill
Osal_task_kill is used to delete some other task (not itself). The API prototype is as follows:
Int osal_task_kill (void * task) {int ret =-1; if ((NULL! = s_os_cb) & & (NULL! = sroomosroomcb-> ops) & & (NULL! = slicosroomcb-> ops- > task_kill)) {ret = slicosroomcb-> ops- > task_kill (task);} return ret;}
The parameters of this API are described in the following table:
The parameter name parameter indicates that the task task ID return value 0-delete success return value-1-delete failure 2.3. Osal_task_exit
API osal_task_exit is used to exit a task (itself). It is not supported at this time, and can be exited directly by return.
2.4. Osal_task_sleep
API osal_task_sleep is used for active hibernation of tasks in ms. The API prototype is as follows:
Void osal_task_sleep (int ms) {if ((NULL! = s_os_cb) & & (NULL! = sroomosroomcb-> ops) & & (NULL! = scompresosroomcb-> ops- > task_sleep)) {scompresosroomcb-> ops- > task_sleep (ms);} return;} 3. Hands-on experiment-- experience task creation and switching experiment content
In this experiment, we will create two tasks, a low priority task task1 and a high priority task task2. Both tasks will print their own task id number in the serial port every 2s, and observe the operation of the two tasks in the serial port terminal.
Experimental code
First, open the previously created HelloWorld project, and experiment based on this project.
Right-click the Demo folder and select New folder:
Create a new osal_kernel_demo folder to store the kernel experiment files:
Next, create the first experimental file, osal_task_demo.c file, in this osal_kernel_demo folder, and start writing code:
/ * to use the osal interface, you need to include the header file * / # include / * Task priority macro definition (the priority of the shell task is 10) * / # define USER_TASK1_PRI 12 / / low priority # define USER_TASK2_PRI 11 / / High priority / * Task ID * / uint32_t user_task1_id = 0 / * Task task1 entry function * / static int user_task1_entry () {int n = 0; / * print on the serial port every 2s, print 5 times and then actively end * / for (n = 0; n)
< 5; n++) { printf("task1: my task id is %ld, n = %d!\r\n", user_task1_id, n); /* 任务主动挂起2s */ osal_task_sleep(2*1000); } printf("user task 1 exit!\r\n"); /* 任务结束 */ return 0;}/* 任务task2入口函数 */static int user_task2_entry(){ /* 每隔2s在串口打印一次,不结束 */ while (1) { printf("task 2: my task id is %ld!\r\n", user_task2_id); /* 任务主动挂起2s */ osal_task_sleep(2*1000); }}/* 标准demo启动函数,函数名不要修改,否则会影响下一步实验 */int standard_app_demo_main(){ /* 创建任务task1 */ user_task1_id = osal_task_create("user_task1",user_task1_entry,NULL,0x400,NULL,USER_TASK1_PRI); /* 创建任务task2 */ user_task2_id = osal_task_create("user_task2",user_task2_entry,NULL,0x400,NULL,USER_TASK2_PRI); return 0;} 编写完成之后,要将我们编写的osal_task_demo.c文件添加到makefile中,加入整个工程的编译: 这里有个较为简单的方法,直接修改Demo文件夹下的user_demo.mk配置文件,添加如下代码: #example for osal_task_demo ifeq ($(CONFIG_USER_DEMO), "osal_task_demo") user_demo_src = ${wildcard $(TOP_DIR)/targets/STM32L431_BearPi/Demos/osal_kernel_demo/osal_task_demo.c} user_demo_defs = -D CONFIG_OSAL_TASK_DEMO_ENABLE=1 endif 添加位置如图: 这段代码的意思是: 如果 CONFIG_USER_DEMO 宏定义的值是osal_task_demo,则将osal_task_demo.c文件加入到makefile中进行编译。 那么,如何配置 CONFIG_USER_DEMO 宏定义呢?在工程根目录下的.sdkconfig文件中的末尾即可配置: 因为我们修改了mk配置文件,所以点击重新编译按钮进行编译,编译完成后点击下载按钮烧录程序。 实验现象 程序烧录之后,即可看到程序已经开始运行,在串口终端中可看到实验的输出内容: linkmain:V1.2.1 AT 11:30:59 ON Nov 28 2019 WELCOME TO IOT_LINK SHELLLiteOS:/>Task 2: my task id is 5 task task 1: my task id is 4, n = 0 task task 2: my task id is 5 task task 1: my task id is 4, n = 1 task task 2: my task id is 5 task task 1: my task id is 4, n = 2 task 2: my task id is 5 task task 1: my task id is 4, n = 3 task task 2: my task id is 5 task task 1: my task id is 4, n = 4 task task 2: my task id is 5 task task 2: my task id is 5!
As you can see, after the system starts, the version number is printed first, the priority of the serial port shell is 10, the shell information is printed first, and then the task1 is created first, but the priority is low, so the task2 preemptive execution that is created later is executed, and the task2 printing is actively suspended for 2s, when the task1 starts to execute, and after 5 times of execution, the task1 ends, and the task2 keeps running.
At this point, I believe you have a deeper understanding of "how to achieve LiteOS task management". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.