Task management and scheduling

This article is shared from the China Mobile OneOS WeChat public account " Task Management and Scheduling ".

Real-time operating systems have relatively high requirements on performance and response time. In order to meet real-time indicators, it is necessary to decompose a complex application into multiple small, schedulable, and serialized units, and ensure that the decomposed units can be correctly followed. Expected design execution.

<Kernel Object Framework>

<kernel object linked list structure>

Features of the task

In the IoT operating system, a task is the most basic scheduling unit . The properties of the task include the execution environment (also known as the context, including various variables and data, all register values, stacks, memory information, etc.), the priority of the task, the running time, etc. Real-time and important tasks can be set relatively High priority, other tasks can be set to lower priority. When the task is switched, the scheduler will first save the current task context, and then restore the context information of the task when switching back to this task, so as to ensure that a single task is running as if it is exclusively CPU and will not be lost. data.

There are the following four common task switching scenarios during system operation:

(1) Between tasks of different priorities, the task scheduler is preemptive . When the high-priority task is ready, it always preempts the CPU usage rights of the low-priority task.
(2) Between tasks of the same priority, the task scheduler is round-robin. The number of time slices to be executed is specified when each task is initialized. After the time is consumed, it switches to another task of the same priority.
(3) The current task is suspended due to waiting for a resource, and the task scheduler selects the highest priority task in the ready state to run.
(4) If the interrupt service routine makes a higher priority task meet the running conditions, when the interrupt is completed, the interrupted task is suspended, and the task with the higher priority starts to run.

How tasks work

(1) Priority of tasks

Indicates whether the task is scheduled to be prioritized in the scheduling list. The system supports a maximum of 256 task priorities (0~255). 0 is the highest priority, and the smaller the value, the higher the priority. Because the scheduling list needs to occupy some resources, if the system resources are insufficient, 8 or 32 priorities can be selected in the system configuration. The operating system is preemptive, and the current task will be swapped out as soon as a higher priority task is ready.

(2) Time slice

Tasks with the same priority are scheduled in a round-robin manner. The time slice specified when the task is created limits the maximum time for a single scheduled execution of the task, and its unit is a system tick. For example, if there are 3 ready tasks A, B and C with the same priority, the time slice of task A is set to 1, the time slice of task B is set to 2, and the time slice of task C is set to 3. The high-priority ready state task will switch back and forth between these three tasks, as shown in the following figure.

<Task running time slice diagram>

(3) Basic structure

The task control block is the basic data structure for task management. It is represented by the structure struct os_task. It stores some information about task operation and scheduling. The detailed definitions are as follows:


struct os_task
{
    os_object_t      parent                       /* object父结构体 */
    os_list_node_t   task_list;                  /* 任务链表 */
    void             *sp;                         /* 栈指针 */
    void             *entry;                      /* 任务入口函数 */
    void             *parameter;                  /* 入口函数参数 */
    void             *stack_addr;                 /* 栈地址 */
    os_uint32_t      stack_size;                  /* 栈大小 */
    os_err_t        error;                        /* 错误码 */
    os_uint8_t      stat;                         /* 任务状态 */
    os_uint8_t      current_priority;            /* 当前优先级 */
    os_uint8_t      init_priority;               /* 初始化优先级 */
    os_uint32_t      number_mask;
    . . . . . .
    os_ubase_t      init_tick;                   /* 初始化占用系统tick数 */
    os_ubase_t      remaining_tick;              /* 剩余系统tick数 */
    os_timer_t       task_timer;                  /* 任务定时器 */
    void (*cleanup)(os_task_handle_t *handle);  /* 任务退出清理函数 */
    os_uint32_t      user_data;                   /* 用户自定义数据 */
};

task_list is the task list pointer, which is used to connect tasks to the task list of the same priority;

stack_addr is the task stack space, which is passed in when the task is created or allocated by the system;

init_priority is the task priority specified when the task is created, and will not be changed during the task running process;

current_priority is the priority of the task running process, which is generally equal to init_priority, and will change when the priority is reversed;

cleanup is a task cleanup function that is called by idle tasks when the task exits.

(4) State transition

During the operation of the system, the task has a variety of different running states, including initial state, ready state, running state, suspended state, and closed state. The task scheduler will adjust the task state according to the running status of the task and the current system state. , as shown in the table.

task status

A detailed description

initial state

The task is in the initial state when the task is created but has not been called to start executing the interface function. At this time, the task does not participate in scheduling.

ready state

At this time, the tasks are queued according to the priority, waiting to be scheduled; once the task in the running state needs to be switched, the scheduler will find the task in the ready state with the highest priority to run.

Operating status

Running on CPU .

Suspended state

Because the required resources are unavailable or the task is actively delayed for a period of time and enters the suspended state, the task does not participate in the scheduling at this time.

Disabled

When the task execution ends, it will enter the closed state and will not participate in scheduling.

The transition between states is shown in the figure below.

<Task state transition diagram>

Task management interface design

(1) Create a task

(2) Destruction task

(3) Start the task

(4) Get the current task

(5) Swap out tasks

(6) Sleeping tasks

(7) Suspend the task

(8) Recovery tasks

(9) Control tasks

(10) Set the task idle callback function

(11) Delete task idle callback function

(12) Set the task scheduling callback function

OneOS is a lightweight operating system launched by China Mobile for the Internet of Things. It features tailorable, cross-platform, low power consumption, and high security. It supports mainstream CPUs such as ARM Cortex-M/R/A, MIPS, and RISC-V. The architecture is compatible with POSIX, CMSIS and other standard interfaces, supports Micropython language development, and provides graphical development tools, which can effectively improve development efficiency and reduce development costs, helping customers develop stable, reliable, safe and easy-to-use IoT applications. Official website address: https://os.iot.10086.cn/
OneOS software address: http://www.oschina.net/p/cmcc-oneos
OneOS project address: https://gitee.com/cmcc-oneos/ OneOS
OneOS technical exchange group: 158631242

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324149338&siteId=291194637