ΜCOS-II Learning Notes 2_ task management

Source: Internet
Author: User

Ii. Task Management

Task management is the core content of the UCOS-II operating system. This chapter provides an overview of the following processes to introduce and summarize the knowledge of task management.

To achieve complex task management, it is necessary to define a large number of data to describe the state of the task, in order to streamline and establish a number of different data structures, so the first step is to understand the data structure and composition. To avoid boredom, it is easier to understand what each structure does, and to feel the subtleties of it.

The necessary data structures are established, the program initializes the TCB in Os_tcbinit (), and initializes the other important data structures of the operating system in Osinit ().

During startup and operation, the operating system invokes a series of task management related functions, and we further understand the specific implementation of task management through the functional understanding of these functions.

Finally, we'll comb the whole process of task management from initialization to task switching during the run.

1. To understand the data structure 1> task control block TCB

Task control block is the most core data structure, defined as a structure, each task corresponding to a TCB, containing the stack pointer, the next TCB pointer, the event block pointer, task status, task priority and a series of information.

2> Idle linked list and ready linked list

The idle and ready linked lists are linked lists of the various TCB, all of which have been created to be assigned to the ready linked list, others to the idle list, each to create a task from the idle list to a TCB to the ready linked list, each delete a task to the task TCB released back to the Idle list.

3> task priority pointer table Ostcbpriotb1[os_lowest_prio+1]

The task priority pointer array, which is a pointer to the TCB that is used to obtain the TCB address for a priority task. For example, the task priority is 5, and its TCB address is credited to Ostcbpriotb1[5].

4> Task Stack

Each task has its own stack space, which is used to save the CPU register data and private data when the task is switched or when the response is interrupted. The stack must be declared as a OS_STK type and consists of contiguous memory space.

5> Task Readiness Table osrdytab1[] and readiness Group OSRDYGRP

In order to quickly find the task Readiness table and Readiness Group for the highest priority tasks in the current ready task, each bit in the Ready table is 1 ready, and 0 means not ready. By setting the osmaptb1[] and osunmaptb1[] Two constant arrays and a given operation, you can quickly get the highest priority in a ready task. The concrete structure is as shown.

6> initialization

Os_tcbinit (): TCB initialization function, which assigns a TCB when the task is created and initializes it;

Osinit (): The initialization function of the operating system, which is divided into several sub-functions, including the initialization of global variables, ready table, Ready Group, Task priority pointer table, event flag Group, memory, message queue, etc.

2. Introduction to main related functions 1> os_taskcreate () basic task Create;2> Os_taskcreateext () Expand Task Create;3> ostaskdel () Delete task (task returned and in hibernation, Instead of deleting the code);4> ostaskdelreq () request Delete task (typically frees resources before deleting tasks);5> Ostasksuspend () suspend task (used to temporarily stop the execution of a task and block it);6> Ostaskr Esume () Resume task (restores the suspended task to the ready state);7> Ostimetick () Task Scheduler, executed once at intervals (e.g. 20ms);8> os_sched () task switching function, to determine the switching conditions to meet the execution of OS_TASK_SW ();9> OS_TASK_SW () assembly language writing, press stack and fallback operation, really go to new task execution;10> osintexit () interrupt task scheduling function, determine the switching conditions to meet the execution of OSINTCTXSW ()11> OSINTCTXSW () CPU-dependent, enabling task switching in interrupt programs3. Task management and scheduling process

The task state transitions as shown:

The task switches between different states through different function calls and the current situation. The task is created in the diagram, and the delete task corresponds to the second section 1> os_taskcreate (), 2> Os_taskcreateext () and 3> Ostaskdel (), 4> ostaskdelreq (). Event waits are included but not limited to ostasksuspend (), and the task is blocked into a blocking state when it waits for semaphores, mailboxes, or system delays. The wait takes place including but not limited to ostaskresume (), the task receives the semaphore, the mailbox and the delay is complete, the task scheduling will go from the blocking state to the ready state.

Interrupted interrupts in a task run go into a suspend state, and the execution of a task scheduler at the end of the interrupt service program causes the current highest priority to be run on a continuation task. The suspended task is still the highest priority and is returned.

Ostimetick () is the Task Scheduler , which is actually a timer interrupt, at each time slice starts to traverse each task, will be set the time delay task delay time minus 1, set the task that satisfies the condition to enter the ready state. Perform a task schedule when a higher priority is found for a task to be resumed.

os_sched () performs general task switching (task scheduling when task creation, self-deletion and self-blocking), first to determine the switching conditions, if the ISR is not completed, the scheduler is locked or the current task is the highest priority will not be switched. The OS_TASK_SW () in os_sched () is used to save the context, enabling a real go-to-new task execution.

osintexit () is used for task scheduling in clock interrupts, similar to os_sched () to determine the switching condition first, and to perform a task switch if the OSINTCTXSW () is satisfied. The Ostimetick () Task Scheduler is an interrupt-mode dispatch.

The Idle Task Os_taskidle () (operating system required) and statistical task Os_taskstart () (not required) are two of the two system's own tasks, ensuring that the CPU does nothing to dry and monitor the CPU running.

4. Summary:

task management from task control block TCB The initialization of the operating system is further performed, such as data structure definition and initialization. Task creation, deletion, suspension, recovery, and other event information operations, after multi-task startup, allow tasks to switch between different states. For the CPU , each state transition is accompanied by a task schedule, and the kernel chooses the task that is currently the highest priority in the continuation task and is in dynamic operation.

ΜCOS-II Learning Notes 2_ task management

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.