The task control block of uCOS-II.

Source: Internet
Author: User

After the OS initialization function osinit is executed, you can call the ostaskcreate or ostaskcreateext functions to create user tasks. These two functions are used by the kernel to create tasks and cannot be modified, therefore, it is called a system service. The user task program is written as a function and is called a user function. It has a line with the services provided by the operating system. Obviously, you must pass the User Function address to the system service that creates the task. In User Tasks, you can also call the ostaskcreate or ostaskcreateext functions to create other tasks, or call the ostaskdel function to delete other tasks or call the function requesting the deletion task to submit a request for the deletion task, you can also call ostasksuspend to temporarily cancel the execution of a task or other tasks.

Because uCOS-II uses a preemptible multi-task scheduling algorithm, the priority of a task is crucial and the only identifier of the task. Therefore, the priority of each task must be different. Task priority and other information, information such as the stack address, task status, task wait time pointer, and task delay time are stored in a data structure named task control block, if you do not know the task control block (TCB), you cannot study and master task management. In addition to the task control block, uCOS-II also has a ready group and a ready table designed to mark the ready tasks. There may be more than one ready task, but there is a maximum of 63 !. The task scheduling program (the clock interruption subprogram) runs once every other time. The specific time depends on the user's settings for clock interruption. If it is set to 20 ms, the task is scheduled every 20 ms. This requires you to find the highest priority ready task from the ready group and the ready table. Because it is an operating system with high real-time performance, it is not allowed to search for tasks with the highest priority for a long time. It cannot be slow because there are more tasks to find, and the task can be quickly searched if it is less. In fact, the scheduling time of uCOS-II tasks is determined in a specified embedded environment. To achieve this, we have designed clever data structures and algorithms to implement this process. To locate various attributes of a task based on the unique identifier of the task priority and perform different processing, the ostcbpriotbl table is designed to quickly locate the control block of the task. To quickly obtain the control block of the current task, a task fast pointer ostcbcur is defined to indicate the current task. The data structure of task management includes the task control block, the idle list of tasks, the ready list of tasks, the task priority index table, and the task stack. It is one of the core contents of the UCOS kernel and leaves the data structure, kernel functions cannot be completed. 1. Definition of a task control block the task control block is the core data structure of task management. When the operating system starts, a certain number of task control blocks are created in the memory. The number of task control blocks equals to the maximum number of tasks simultaneously managed by the operating system. UCOS-II divides the task control block into two linked lists: Ready linked list and idle linked list. To create a task, you can extract an idle task control block from the idle linked list, add various attributes of the task to the control block, and move the task control block to the ready table, when you change the readiness table and on-demand group, the task changes from sleep to readiness. When a task with no higher priority is running, the task can be run. To end a task, you need to move the task control block of the task from the ready linked list to the idle linked list, modify the ready group and ready table, and cancel the Ready Mark of the task, the task is converted from the ready state to other States, and it can be run only when the ready state is returned. Before providing the ready linked list and idle linked list, you must first master the basic structure of the task control block, which is in the ucos_ii.h file. The following code defines the task control block struct:

Typedef struct OS _tcb {
OS _stk * ostcbstkptr;/* pointer to the top of the current task stack */

# If OS _task_create_ext_en> 0
Void * ostcbextptr;/* extension block pointer */
OS _stk * ostcbstkbottom;/* pointer to the bottom of the task stack */
Int32u ostcbstksize;/* task stack size */
Int16u ostcbopt;/* extended options */
Int16u ostcbid;/* task id. Because the priority of the task is the unique identifier of the task, the value of this item is usually equal to the task priority */
# Endif

Struct OS _tcb * ostcbnext;/* pointer to the next task control block */
Struct OS _tcb * ostcbprev;/* refers to the pointer to the control block of the previous Task */

# If OS _event_en | (OS _flag_en> 0)
OS _event * ostcbeventptr;/* pointer to the Event Control Block */
# Endif

# If (OS _q_en> 0) & (OS _max_qs> 0) | (OS _mbox_en> 0)
Void * ostcbmsg;/* message pointer from osmboxpost () orosqpost */
# Endif

# If (OS _flag_en> 0) & (OS _max_flags> 0)
# If OS _task_del_en> 0
OS _flag_node * ostcbflagnode;/* event flag node pointer */
# Endif
OS _flags ostcbflagsrdy;/* indicates the event where the task is ready and running */
# Endif

Int16u ostcbdly;/* Number of clock beats for task latency or the timeout time for waiting for the event */
Int8u ostcbstat;/* task status: Sleep, ready, running, waiting, interrupted */
Int8u ostcbstatpend;/* event wait sign */
Int8u ostcbprio;/* The task priority, 0 is the highest */

Int8u ostcbx;/* bit position in group corresponding to task priority */
Int8u ostcby;/* index into ready table corresponding to task priority */
# If OS _lowest_prio <= 63
Int8u ostcbbitx;/* bit mask to access bit position in ready table */
Int8u ostcbbity;/* bit mask to access bit position in ready Group */
# Else
Int16u ostcbbitx;/* bit mask to access bit position in ready table */
Int16u ostcbbity;/* bit mask to access bit position in ready Group */
# Endif

# If OS _task_del_en> 0
Int8u ostcbdelreq;/* task deletion request flag */
# Endif

# If OS _task_profile_en> 0
Int32u ostcbctxswctr;/* Number of times the task is switched */
Int32u ostcbcyclestot;/* Total Number of clock cycles that the task has run */
Int32u ostcbcyclesstart;/* snapshot of cycle counter at start of task resumption */
OS _stk * ostcbstkbase;/* start address of the task stack */
Int32u ostcbstkused;/* space used in the task stack */
# Endif

# If OS _task_name_size> 1
Int8u ostcbtaskname [OS _task_name_size]; // Task Name
# Endif
} OS _tcb;

Description of the task control block in the program: (1) * ostcbstkptr is a pointer to the OS _stk type. OS _stk defines typedef unsigned int OS _stk in the OS _cpu.h file. Therefore, OS _stk is an unsigned integer, OS _stk is the type of each data item in the task stack. It is different for different hardware systems and needs to be modified during migration. OS _cpu.h defines the CPU-related data structure and global variable Task Stack operations at the bottom layer, which must be written in assembly language. Therefore, this pointer is defined in the first sentence of the entire struct, after defining the object of the struct, the zero address of the control block stores the stack top address of the task stack, facilitating the operations of the assembly language. In other words, ostcbstkptr is a pointer to the top of the job stack. Each job has its own job stack. The job stack is the key data structure for job switching and the CPU environment for job running, the address of the code including the task is saved in the task stack. (2) The task status is ostcbstat. The value range and corresponding macro of the task status are shown in the table below. When the task status value is OS _stat_rdy, that is, 0x00, it is in the ready state and can be scheduled to run. Macro Value Meaning OS _stat_rdy 0x00 task not waiting for event and task not pending OS _stat_sem 0x01 task waiting for semaphore OS _stat_mbox 0x02 task waiting for message mailbox OS _stat_q 0x04 task waiting for Message Queue OS _stat_suspend 0x08 task suspension OS _stat_mutex 0x10 task wait mutex semaphores OS _stat_falg 0x20 task wait event flag (3) task priority: ostcbprio. Each task has a unique priority. Therefore, uCOS-II uses priority as the identifier of an event and as the primary key for task management. The priority of a task can be 0 ~ 63, but priority 62 and 63 are occupied by statistical tasks and idle tasks. The available priority can be set to 0 ~ 61. The lower the number, the higher the priority.

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.