LiteOS learning the second chapter--Basic introduction

Source: Internet
Author: User
Tags semaphore
Basic Concepts

From the system point of view, the task is the smallest running unit of competing system resources. Tasks can use or wait on the CPU, use system resources such as memory space, and run independently of other tasks.

Huawei Liteos's Task module provides users with multiple tasks, enabling switching and communication between tasks, and helping users manage business process processes. This allows the user to devote more effort to the implementation of the business functions.

Huawei Liteos is a multitasking-enabled operating system. In Huawei Liteos, a task represents a single thread.

The task in Huawei Liteos is to preempt the scheduling mechanism while supporting the time-slice rotation scheduling method.

High-priority tasks can interrupt low-priority tasks, and low-priority tasks must be scheduled after a high-priority task is blocked or ended.

Huawei Liteos has a total of 32 priorities (0-31), with a maximum priority of 0 and a minimum priority of 31.

task-related concepts

Task Status

Each task in the Huawei Liteos system has several operational states. Once the system is initialized, the created task can compete for certain resources in the system and be dispatched by the kernel.

Task status is usually divided into the following four types: ready: The task is in the Ready list, waiting for the CPU only. Run (Running): The task is executing. Blocking (Blocked): The task is not in the Ready list. Includes tasks being suspended, tasks being delayed, tasks waiting for semaphores, reading and writing queues, or waiting for read and write events. Exit State (Dead): The task runs to the end and waits for the system to reclaim resources. Figure 1 task status diagram

Task State Migration Description: Ready state → Run state:

When the task is created, it enters the ready state, and when a task switch occurs, the highest priority task in the Ready list is executed to enter the running state, but the task is still in the ready list at the moment. Operating state → blocking state:

When a running task is blocked (suspend, delay, read semaphore wait), the task is removed from the Ready list, the task state becomes blocked from the run, and then a task switch occurs, leaving the highest priority tasks remaining in the Ready list. Blocking state → Ready state (blocking State → operating state):

When a blocked task is restored (task recovery, delay time timeout, read semaphore timeout, or read semaphore), the recovered task is added to the Ready list, which turns the blocked state into a ready state, and if the restored task takes precedence over the priority of the running task, a task switch occurs. Turn the task from ready state to run state. Ready state → blocking state:

The task may also be blocked (suspended) in the ready state, when the task State is ready to be turned into a blocking state, and the task is removed from the ready list and will not participate in task scheduling until the task is resumed. Operating state → Ready state:

Task scheduling occurs when a higher priority task is created or restored, and the highest priority task in the Ready list becomes a run state, so the previously running task is changed from run state to ready state, still in the Ready list. Operating state → Exit state

The running task ends and the task status changes from run state to exiting state. The exit state contains the normal exit and impossible status of the end of the task run. For example, a task that does not set the Detach attribute (los_task_status_detached), which renders the impossible state after the end of the run, is the exiting state. Blocking state → Exit state

The blocked task calls the delete interface, and the task status changes from blocked state to exiting state.

task ID

The task ID, which is returned to the user by parameter when the task is created, as a very important identification of the task. The task ID allows the user to perform tasks such as task suspend, task recovery, query task name, and so on for the specified task.

Task Priority

The priority represents the order of precedence for task execution. The priority of a task determines the task that is about to be performed when a task switch occurs. The highest priority task in the Ready list will be executed.

Task Entry Function

The function to be executed after each new task has been dispatched. This function is implemented by the user and is specified by the task creation structure when the task is created.

Task control block TCB

Each task contains a task control block (TCB). The TCB contains information such as the task context stack pointer (stack pointer), task status, task priority, task ID, Task name, task stack size, and so on. The TCB can reflect the operation of each task.

Task Stack

Each task has a separate stack space, which we call the task stack. The information stored in the stack space contains local variables, registers, function parameters, function return addresses, and so on. The task switches the context information of the cut-out task into its own task stack space when the task is switched, so that the site is restored when the task resumes, so that it resumes execution at the cut-out point after the task has been resumed.

Task Context

Some of the resources, such as registers, that the task uses during the run, are called task contexts. When the task is suspended, other tasks continue to execute, and if the task context is not saved after the task is resumed, it is possible that the task switch modifies the value in the register, leading to an unknown error.

As a result, Huawei Liteos will keep the task context information of the task in its own task stack when the task is suspended, so that when the task resumes, the context information from the stack space is resumed, and the code that is interrupted when it is suspended continues to execute.

Task Switching

The task switch contains actions such as getting the highest priority task in the Ready list, cutting out the task context save, and entering the task context recovery. operating mechanism

Huawei Liteos Task Management module provides task creation, task delay, task suspend and task recovery, lock task scheduling and lock task scheduling, query task ID based on task control block, Query task control block information function based on ID.

Before the user creates a task, the system will first request the memory space required by the task control block, and the task module will fail to initialize if the available memory space is less than the memory space required by the system. If the task is initialized successfully, the system initializes the contents of the task control block.

When a user creates a task, the system initializes the task stack and presets the context. In addition, the "Task entry function" Address is also placed in the appropriate location. This will execute the task entry function when the task is first launched into the run state.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.