Nucleus PLUS Task Scheduling

Source: Internet
Author: User
Overview

The main purpose of the kernel of the Nucleus PLUS is to manage the competition (sharing CPU) of real-time tasks, which provides various convenience for applications and quickly responds to external events. As shown in System Structure 1 of Nucleus PLUS, it can be seen that thread control is the core of the entire kernel and communication between tasks is realized through mailboxes, queues, and pipelines, tasks are synchronized through semaphores, event groups, and signals.


The thread control component is used to manage the execution of real-time tasks and advanced interrupt services. It is the core part of the nucleus embedded real-time operating system. To control the execution process, tasks are usually assigned a priority. The task priority ranges from 0 to 255. Priority 0 has the highest priority. Unless the preemption flag is set to invalid, tasks with a lower priority will be preemptible by tasks with a higher priority. To ensure real-time response to external events, nucleus designs an advanced interrupt service hisr with a priority ranging from 0 to 2, with the highest priority of 0.

The task scheduling thread is responsible for the scheduling and management of preemptible real-time tasks and hisr. Each user application is composed of multiple tasks. A task is a semi-independent program segment with a specific purpose. A task is in one of five states: running, ready, suspended, terminated, and completed, see table 1. Tasks have different priorities. High-priority tasks can seize low-priority tasks. tasks with the same priority are scheduled in the "ready" order, with the priority decreasing from 0 to 25.

Table 1 five States of a nucleus task


Status meaning

Run executing

The task is currently being executed by the CPU.

Ready for ready

The task is ready, but another task is currently running.

Suspend suincluded

The task is sleeping because the service is waiting for demand. When the requirement is met, the task changes to the ready state. By default, all newly created tasks are in

Terminate terminated

The task is terminated. When a task is in this status, it will not be executed until it is reset.

Finished finished

The task is completed. Exit from the entry function. When the task is in this status, it will not be executed until it is reset.

(1) Task Scheduling Algorithm

The nucleus scheduler is used to determine whether to switch tasks. If you need to switch tasks, the process to which to switch. The task scheduling algorithm of the real-time nucleus operating system is very simple. It includes the time slice Rotation Algorithm and the polling algorithm.

The time slice Rotation Algorithm distributes all tasks with the same priority to the same time slice. After the time slice is used up, it is transferred to the next task and executed in turn until all tasks with the same priority are completed, then go to the next priority. The round-robin algorithm is used to execute all tasks of this priority in the order of readiness time. When all tasks of this priority are completed, the next priority task is executed.

In addition, the real-time operating system of the nucleus is preemptible, and the process scheduling is based on the order of priority from high to low. When a low-priority task is running, it is preemptible when the high-priority task is ready for execution, forcing the low-priority task to be suspended.

The scheduling time of nucleus includes:

(1) the time when the Process status changes, that is, Process Termination and process sleep;

(2) When a new process is added to the running queue;

(3) the time slice of the current process is used up;

(4) The process is returned from the system call to the user State;

(5) After the kernel is interrupted, the process returns to the user State.

(2) Nucleus PLUS Task Management1. Main Task Control Structure

Each task of Nucleus PLUS has a control structure called thread control block (TCB). The task supports dynamic creation and deletion. TC manages all tasks through a two-way linked list tcd_created_tasks_list, the global variable tcd_total_tasks indicates the total number of tasks that have been created.

Tcd_priority_list is a 256-sized TCB pointer array tc_tcb (* tcd_priority_list) [256] (the array size is the same as the priority level of the Nucleus PLUS). array elements are indexed by task priority. Each element in the array is the header of a priority-ready task linked list. If an element is null, it indicates that the priority task is not ready. For the list of ready tasks with each priority, TCB is stored in a bidirectional list. That is, Nucleus PLUS maintains a pointer array to schedule the linked list of tasks with different priorities.

Figure 2 tcd_priority_list

2. task priority management

In order to quickly calculate the highest priority of a ready task, Nucleus PLUS introduces a priority group + query table mechanism.

2.1 tcd_priority_groups

The task has the highest priority from 0 to 255, and has the lowest priority. The 256 priorities are divided into 32 groups, each of which has eight levels. For example, the priority of the 0th group is 0 ~ 7. The 1st group corresponds to 8 ~ 15 ,.... Each bit of the 32-bit integer variable tcd_priority_groups indicates whether a task in the priority group is ready. If one is set to 1, at least one task in the group is ready.

Figure 3 tcd_priority_groups

2.2 tcd_sub_priority_groups []

The child priority array tcd_sub_priority_groups [32] indicates the specific eight priorities in a group. The array element is an 8-bit integer, each of which corresponds to a priority in the group. For example, tcd_sub_priority_groups [0] indicates the readiness status of The 0th group (Priority 0-7) tasks, 1 indicates that the task with priority 0 is ready, and 1 with priority 0th indicates that the task with priority 7 is ready.

Figure 4 tcd_sub_priority_groups

If the priority of a task is tc_priority, The subpriority mask of the task is tc_sub_priority = 1 <(tc_priority & 7 ). The priority group mask of the task is: tc_priority_group = 1 <(tc_priority)> 3 ).

2.3 tcd_lowest_set_bit []

Tcd_lowest_set_bit [] is a query table (the element has been calculated in advance ), this table is the sequence number of the 8-bit integer data and the first position (from high to bottom) of the 8-bit data, such as tcd_lowest_set_bit [N] = value, N refers to the value of an 8-digit integer (that is, the serial number), and value refers to the first position (from low to high) as 1 in the 8-digit integer ). For example, the binary value of unsigned char 7 is 0000 0111, and the first bit of 1 is 0, value = 0, tcd_lowest_set_bit [7] = 0.

In this way, we can obtain the table tcd_lowest_set_bit [256]. For example, the data with the digit 0 being 1 in an eight-digit integer includes 1, 3, 5, 7, 9, 11, 13, 15,..., 253,255, so there are:

TCD_Lowest_Set_Bit[1]= 0,   TCD_Lowest_Set_Bit[3]= 0,   TCD_Lowest_Set_Bit[5]= 0,   TCD_Lowest_Set_Bit[7]= 0,   ...,   TCD_Lowest_Set_Bit[253]= 0,   TCD_Lowest_Set_Bit[255]= 0。  

2.4 basic process of task creation and priority management

Task priority management global variables

Unsigned tcd_priority_groups; data_element values [tc_max_groups]; unsigned_char tcd_lowest_set_bit [256] = {0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0}; int tcd_highest_priority; // the highest priority of the ready state task.
I. When creating a task (TCC. c) -- tcc_create_task funtion

Extern unsigned tcd_priority_groups; extern data_element values [tc_max_groups]; extern unsigned_char tcd_lowest_set_bit []; extern int values; task-> tc_priority = priority; task-> tc_priority_head = & (tcd_priority_list [Priority]); task-> tc_sub_priority = (data_element) (1 <(Priority & 7); // group subpriority calculation, 0-7 priority = Priority> 3; // priority group calculation, with 32 groups of tasks> tc_priority_group = (unsigned) 1) <priority; // position 1 task-> tc_sub_priority_ptr = & (tcd_sub_priority_groups [Priority]) of the Response Group of the priority group

Ii. Resume the operation of a task (TCC. c) -- tcc_resume_task fuction

Groups = tcd_priority_groups | (Task-> tc_priority_group); // Save the group number to the global variable * (Task-> tc_sub_priority_ptr) = (* (Task-> tc_sub_priority_ptr )) | task-> tc_sub_priority; // Save the group value to the global variable.

Iii. Obtain the current highest priority (TCC. c)

If (distinct & tc_highest_mask) Index = 0; else if (tcd_priority_groups & distinct) Index = 8; else if (tcd_priority_groups & distinct) Index = 16; else Index = 24; index = index + tcd_lowest_set_bit [(INT) (tcd_priority_groups> index) & tc_highest_mask)]; // obtain the lowest ranking of 1 in group number. The value range is 0 ~ 31 temp = keys [Index]; // corresponds to priority_group and has 32 sub_priority_groups instances. The corresponding sub_priority_groups records = (index <3) + tcd_lowest_set_bit [temp] are found through the index. // obtain the actual highest priority
After obtaining the actual highest priority, the task with the highest priority is mounted to tcd_priority_list []. After the scheduling is completed, the task bit is cleared and the next task priority is obtained cyclically.

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.