2. Introduction to task scheduling and task status of FreeRTOS system

Table of contents

1. Basic knowledge

1. FreeRTOS task status

(1) Running state

(2) Ready state

(3) Blocking state

(4) Suspend state

2. Introduction to task scheduling

1. Preemptive scheduling

2. Time slice scheduling

3. Coroutine scheduling


1. Basic knowledge

1. FreeRTOS task status

        There are four task states in FreeRTOS, which are running state, ready state, blocked state and suspended state. When FreeRTOS is running, the state of the task must be one of these four states.

(1) Running state

        If a task gets the right to use the CPU, that is, when the task is actually executed, then the task is in the running state. If the MCU running the RTOS has only one processor core, there can only be one task processing the running state at any time.

(2) Ready state

        If a task has been able to be executed (not in the blocked state or suspended state), but is not currently executed (a task with the same priority or higher priority is holding the CPU usage right), then the task is ready state.

(3) Blocking state

        If a task is delayed for a period of time or waiting for an external event to occur, then the task is blocked. For example, if a task calls the function vTaskDelay() to delay for a period of time, the task will be in the blocking state before the delay times out. Tasks can also be blocked waiting for external events such as queues, semaphores, event groups, notifications, or semaphores. Normally, tasks in the blocking state have a blocking timeout period. After the task blocking reaches or exceeds the timeout period, even if the external event that the task is waiting for has not occurred, the blocking state of the task will be released.

(4) Suspend state

        Tasks generally enter and exit the suspended state through the function vTaskSuspend() and function vTaskResums(), which is the same as the blocking state, and tasks in the suspended state cannot be executed.

The transition diagram of their four task states is shown in the following figure:

Summarize:

1. Only the ready state can be transformed into the running state

2. To run tasks in other states, they must first change to the ready state.

3. There are only four states in FreeRTOS, running state, ready state, blocked state, and suspended state. In these four states, except for the running state, tasks in other task states have their corresponding task state lists.

  • Ready list: pxReadyTasksLists[x], where x represents the task priority value

How do we know that there are tasks in x?

We define a variable. When a certain value is set to 1, it means that there are tasks in the sequence list corresponding to the priority.

  • Blocking list: pxDelayedTaskList
  • Suspended list: xSuspendedTaskList

2. Introduction to task scheduling

        The scheduler uses related scheduling algorithms to determine which task needs to be executed currently.

FreeRTOS supports three task scheduling methods, namely preemptive scheduling, time slice scheduling and coroutine scheduling.

1. Preemptive scheduling

        Preemptive scheduling is mainly for tasks with different priorities. Each task has a priority. A task with a higher priority can preempt a task with a lower priority. Only when a task with a higher priority is blocked or suspended, the task with a lower priority Level tasks can be run. The higher the configured number, the higher its priority.

Next, let's take a look at the operating conditions and operating process of preemptive scheduling

Operating conditions

1. Create three tasks: Task1, Task2, Task3

2. The priorities of Task1, Task2, and Task3 are 1, 2, and 3 respectively; the larger the task setting in FreeRTOS, the higher the priority, and the priority of Task3 is the highest.

The operation process is as follows:

1. During the running of Task1, Task2 is ready during this process. Under the action of the preemptive scheduler, Task2 will preempt the running of Task1.

2. During the running of Task2, Task3 is ready. Under the action of the preemptive scheduler, Task3 will preempt the running of Task2.

3. During the running of Task, Task3 is blocked (system delay or waiting for semaphore, etc.), at this time, in the ready state, Task2 with the highest priority is executed.

Summarize:

1. High priority tasks, priority execution

2. High-priority tasks do not stop, and low-priority tasks cannot be executed

3. The preempted task will enter the ready state

2. Time slice scheduling

        Time slice scheduling is mainly for tasks with the same priority. When multiple tasks have the same priority, the task scheduler will switch tasks every time the system clock ticks. That is to say, the CPU runs tasks with the same priority in turn. The running time of a task is one system clock tick.

        Tasks of equal priority enjoy the same CPU time in turn (can be set), which is called a time slice. In FreeRTOS, a time slice is equal to the SysTick interrupt cycle.
Next, let's take a look at the working process and operating conditions of time slice scheduling

Operating conditions"

1. Create three tasks: Task1, Task2, Task3

2. The priorities of Task1, Task2, and Task3 are all 1; that is, the three tasks have the same priority

The operation process is as follows:

1. First, after Task1 runs a time slice, switch to Task2 to run

2. After Task2 runs a time slice, switch to Task3 to run

3. During the running of Task3 (less than one time slice), Task3 is blocked (system delay or waiting for semaphore, etc.), and then directly switch to the next task Task1

4. After Task1 runs a time slice, switch to Task2 to run.

Summarize:

1. Tasks with the same priority are executed in turn; time slices flow

2. The size of a time slice depends on the tick timer interrupt period.

3. Note that unused time slices will not be used again, and the next task Task3 will be executed according to the clock beat of a time slice.

3. Coroutine scheduling

        The official is no longer updated, so just understand

The scheduler always chooses the task with the highest priority to execute among the tasks in the ready list.

Q: What if task1, task2, and task3 all have a priority of 1?

Answer: Tasks with the same priority will be connected to the same ready list, task1 will run a time slice, task2 will run a time slice, and task3 will run a time slice.

Guess you like

Origin blog.csdn.net/zywcxz/article/details/131484403