Today, the content we share is mainly about task management in the IoT operating system.
Before starting to read the sharing posts officially, as usual, we need to think about a few questions:
1. Process and thread, do you know the difference and connection?
2. What is a task?
3. What are the main aspects of task management in the IoT operating system?
If you have considered the above questions seriously, please read the following sharing, and hope you can get your own blind spot knowledge!
1. Basic concepts of task management
Process: an instance of a running program
Thread: an entity in the process, which is the basic unit of system scheduling and dispatch
The difference between process and thread
Thread is the smallest unit of program execution, and process is the smallest unit of operating system allocation of resources
A process is composed of one or more threads, which are different execution routes of code in a process
The processes are independent of each other, and each thread in the same process shares the memory space of the program
Task concept
From a system perspective, a task is the smallest unit of operation that competes for system resources; a task can use or wait for CPU, use memory space and other system resources, and run independently of other tasks
The task module of Huawei LiteOS can provide users with multiple tasks, realize switching and communication between tasks, and help users manage business program processes.
Huawei LiteOS is an operating system that supports multitasking. In LiteOS, a task represents a thread
The task in Huawei LiteOS is a preemptive scheduling mechanism and supports time slice round-robin scheduling
Second, the principle of task management
Task: an entity composed of a set of elements
Elements (used to manage each task)
Identifier: a unique identifier associated with the task
Task name: task name
Task status: used to indicate that the current task is executing or waiting
Priority: represents the priority order of task execution
Context stack pointer: the address of the next instruction where the task will be executed
Task Control Block (TCB: Task Control Block)
Task status
Used to describe the behavior exhibited by the task
Ready state (Ready): The task is in the ready list, and the ready task has the ability to execute. It just waits for the scheduler to schedule it. The newly created task will be initialized to the ready state
Running state: This state indicates that the task is being executed. At this time, it occupies the processor. The LiteOS scheduler always chooses to run the ready state task with the highest priority. When the task is run, its task state changes Become running
Blocked: If the task is currently waiting for a certain sequence or external interrupt, we say that the task is in a blocked state and the task is not in the ready list. Including tasks are suspended, tasks are delayed, tasks are waiting for semaphores, read and write queues, or waiting for read and write events, etc.
Exit state (Dead): the task is finished, waiting for the system to reclaim resources
Migration between task states
Ready—>Run state
After the task is created, it enters the ready state. When a task switch occurs, the highest priority task in the ready list is executed and enters the running state, but the task is still in the ready list at the moment
Run -> blocking state
When a running task is blocked (suspended, delayed, acquired mutex lock, read message, read semaphore waiting, etc.), the task will be deleted from the ready list, and the task state will change from running state to blocking state, and then occur Task switching, the highest priority task remaining in the ready list
Blocking -> ready state
After the blocked task is resumed (task resume, delay time timeout, read semaphore timeout or read semaphore, etc.), the resumed task will be added to the ready list, thereby changing from the blocked state to the ready state; if the task is restored If the priority of the task is higher than the priority of the running task, a task switch will occur, and the task will be changed from the ready state to the running state
Ready -> blocking state
The task may also be blocked (suspended) in the ready state. At this time, the task state will change from the ready state to the blocked state. The task is deleted from the ready list and will not participate in task scheduling until the task is restored
Run—>Ready
After a higher priority task is created or restored, task scheduling will occur. At this moment, the highest priority task in the ready list becomes the running state, and the originally running task changes from the running state to the ready state and remains in the ready list
Run—>Exit
When the running task ends, the kernel automatically deletes this task, and the task state changes from running state to exiting state
Blocking -> exit state
The blocked task calls the delete interface, and the task state changes from blocking state to exit state
Task stack
The size of the task stack is aligned by 8 bytes
effect
When the task is cut off or in response to an interrupt, the task stack is used to save the contents of the CPU register
When the task is running, the task stack is used to save some local variables, function parameters, etc.
Task priority
Priority indicates the priority order of task execution
The priority of the task determines the task to be executed when a task switch occurs
The highest priority task in the ready list will be executed
High-priority tasks can interrupt low-priority tasks, and low-priority tasks can only be scheduled after high-priority tasks are blocked or finished
Huawei LiteOS tasks have a total of 32 priorities (0-31), the highest priority is 0, and the lowest priority is 31
Three, task scheduling
Task context
Huawei LiteOS will save the task context information of the task in its own task stack when the task is suspended, so that after the task is resumed, the context information of the suspension will be restored from the stack space, so that the execution will continue when the task is suspended. Interrupted code
Task switching
Task switching includes actions such as obtaining the highest priority task in the ready list, saving the task context, and restoring the task context.
Scheduling Algorithm
Preemptive scheduling mechanism
Time slice rotation scheduling mechanism
Four, task operation
Create task
LOS_TaskCreateOnly: The created task enters the suspend state without task scheduling
LOS_TaskCreate: Create a task and enter the ready state for task scheduling
Terminate task
LOS_TaskDelete: delete the specified task
Five, inter-task communication
Information transmission between tasks through message queues to achieve communication between tasks
message queue
Data structure for inter-task communication
Used to receive non-fixed length messages from tasks or interrupts
You can choose whether to store the message in your own space according to different interfaces
Click to follow and learn about Huawei Cloud's fresh technology for the first time~