Conversion-uC/OS Task Scheduling

Source: Internet
Author: User

I. kernel Overview:
In a multi-task system, the kernel is responsible for managing tasks, or allocating CPU time for each task, and communicating between tasks. The basic service provided by the kernel is task switching. The use of the Real-Time Kernel greatly simplifies the design of the application system because the real-time kernel allows applications to be divided into several tasks and managed by the real-time kernel. The kernel itself also increases the application load, the code space increases the ROM usage, and the data structure of the kernel itself increases the RAM usage. But what's more important is that each task needs to have its own stack space, and this memory is quite powerful. The CPU usage of the kernel is generally between 2 to 5 percentage points. Ucos ii has a sophisticated kernel scheduling algorithm, which features low real-time kernel precision, high execution efficiency, clever algorithms, and little code space.

Ii. ucos ii kernel scheduling features:

  1. Only preemptible scheduling algorithms based on priorities are supported, and time slice training is not supported;
  2. 64 priorities. You can only create 64 tasks. You can only create 56 tasks;
  3. Each task has different priorities.
  4. Priority reversal is not supported;
  5. The ready queue uses a memory ing table for fast query. High Efficiency;
  6. Supports clock beats;
  7. Supports semaphores, message queues, event control blocks, event flag groups, and message and email task communication mechanisms;
  8. Supports interruption nesting. The number of layers of interruption nesting can reach 255. the interruption uses the stack of the current task to save the context;
  9. Each task has its own stack, and the stack size is set by the user;
  10. Task priority can be dynamically modified;
  11. The task TCB is a static array, and the Creation task only obtains a TCB from it, without dynamic allocation, releasing the memory;
  12. The task stack is created statically or dynamically by the user. It is completed outside the task creation stage, and the task creation itself does not carry out dynamic memory allocation;
  13. The total number of tasks (OS _max_tasks) is determined by the user;
  14. The highest priority is 0, and the lowest priority is 63;
  15. There is a idle task with the lowest priority and runs when no user task is running.

Iii. task control block OS _tcb description:
The TCB data structure of ucos ii is simple, the content is easy to understand, the most basic task information is saved, and the reduction is also supported to reduce memory consumption. TCB is configured according to the user in advance, the structure array of the static allocated memory. It can be added, searched, or deleted by the priority number. Reduce dynamic memory allocation and release. Because TCB is assigned by priority, each task must have its own priority and cannot have the same priority as other tasks.

Typedef struct OS _tcb
{
OS _stk * ostcbstkptr;
# If OS _task_create_ext_en
Void * ostcbextptr;
OS _stk * ostcbstkbottom;
Int32u ostcbstksize;
Int16u ostcbopt;
Int16u ostcbid;
# Endif
Struct OS _tcb * ostcbnext;
Struct OS _tcb * ostcbprev;
# If (OS _q_en & (OS _max_qs> = 2) | OS _mbox_en | OS _sem_en
OS _event * ostcbeventptr;
# Endif
# If (OS _q_en & (OS _max_qs> = 2) | OS _mbox_en
Void * ostcbmsg;
# Endif
Int16u ostcbdly;
Int8u ostcbstat;
Int8u ostcbprio;
Int8u ostcbx;
Int8u ostcby;
Int8u ostcbbitx;
Int8u ostcbbity;
# If OS _task_del_en
Boolean ostcbdelreq;
# Endif
} OS _tcb;

. Ostcbstkptr is a pointer to the top of the current job stack.
. * Ostcbextptr;: used by the task extension module;
. * Ostcbstkbottom;
. Ostcbstksize ;.
. Ostcbopt;
. Ostcbid;
. Ostcbnext and. ostcbprev are used for the dual link of the task control block OS _tcbs,
. Ostcbeventptr is a pointer to the Event Control Block.
. Ostcbmsg is a pointer to the message sent to the task.
. Ostcbdly this variable is used when the task needs to delay several clock beats, or the task needs to be suspended for a period of time to wait for an event to occur,
. Ostcbstat is the status word of the task.
. Ostcbprio is the task priority.
. Ostcbx,. ostcby,. ostcbbitx and. ostcbbity are used to accelerate the process of entering the ready state or waiting for the event to take place.

Ostcby = Priority> 3;
Ostcbbity = osmaptbl [Priority> 3];
Ostcbx = Priority & 0x07;
Ostcbbitx = osmaptbl [Priority & 0x07];
. Ostcbdelreq is a Boolean value used to indicate whether the task needs to be deleted.

4. Ready list ):
Ucos ii uses memory ing to add, search, and delete ready queues. Therefore, only 64 tasks are supported. Each task has its own priority and cannot have the same priority as other tasks.
 
The readiness flag of each task is placed in the readiness table. The readiness table has two variables: osrdygrp and osrdytbl []. In osrdygrp, tasks are grouped by priority, and eight tasks are a group. Each bit in osrdygrp indicates whether there are ready tasks in each group of 8 tasks. When the task enters the ready state, the corresponding bit of the corresponding element in the ready table osrdytbl [] is also set. The size of the ready table osrdytbl [] array depends on OS _lowest_prio (see OS _cfg.h ).
To determine which priority task to run next time, the kernel scheduler always places OS _lowest_prio in the corresponding byte 1 of the ready table. The relationship between osrdygrp and osrdytbl [] is shown in Figure 3.3, according to the following rules:
When any one of osrdytbl [0] is 1, The 0th position of osrdygrp is 1,
When any one of osrdytbl [1] is 1, the 1st position of osrdygrp is 1,
When any one of osrdytbl [2] is 1, the 2nd position of osrdygrp is 1,
When any one of osrdytbl [3] is 1, the 3rd position of osrdygrp is 1,
When any one of osrdytbl [4] is 1, the 4th position of osrdygrp is 1,
When any one of osrdytbl [5] is 1, the 5th position of osrdygrp is 1,
When any one of osrdytbl [6] is 1, the 6th position of osrdygrp is 1,
When any one of osrdytbl [7] is 1, the 7th position of osrdygrp is 1,

The code in program list 3.5 is used to put the task into the ready table. Prio is the priority of a task.
 
The program list l3.5 enables the task to enter the ready state (these two lines of code are just amazing !!!)

/*
This line of code is used to locate a column and set the column value to 1.
Assume that the PRIO value is 13, that is, the priority is 13. prio> 3 shifts the value of 3 places to 1. You can check the table t3.1 to find that the value of osmaptbl [1] is 0000 0010. use 0000 0010 and osrdygrp for exclusive or operation
*/
Osrdygrp | = osmaptbl [PRIO> 3];
/*
 
*/
Osrdytbl [PRIO> 3] | = osmaptbl [PRIO & 0x07];

 
The lower three digits of the task priority are used to determine the position of the task in the total ready table osrdytbl. The next three digits are used to determine the elements in the osrdytbl [] array. Osmaptbl [] is a blocked word in the ROM (see the OS _core.c file). It is used to limit the element subscript OF THE osrdytbl [] array to 0 to 7. See table 3.1.

Index Bit mask (Binary)
0 00000001
1 00000010
2 00000100
3 00001000
4 00010000
5 00100000
6 01000000
7 10000000

If a task is deleted, use the code in program list 3.6 for reverse processing.

Program list l3.6 delete a task from the readiness table
 

If (osrdytbl [PRIO> 3] & = ~ Osmaptbl [PRIO & 0x07]) = 0)
Osrdygrp & = ~ Osmaptbl [PRIO> 3];


The code above clears the corresponding bits of the corresponding elements in the ready task table array osrdytbl []. For osrdygrp, only when none of the tasks in the group in which the deleted task is located are in the ready state, the corresponding bit is cleared. That is to say, when all the bits of osrdytbl [PRIO> 3] are zero, the corresponding bits of osrdygrp are cleared. To find the task with the highest priority in the ready state, you do not need to scan the entire ready task table from osrdytbl [0]. You only need to check another table, osunmaptbl ([256]) (see OS _core.c ). The eight bits in each byte in osrdytbl [] indicate which of the eight tasks in this group have entered the ready state, and the low level has a higher priority than the high level. Use this Byte as the subscript to query the osunmaptbl table. The returned byte is the location of the task with the highest priority in the ready state task in the group of tasks. The returned value is between 0 and 7. The following code is used to determine the task with the highest priority in the ready state.

Find the task with the highest priority in the ready state

Y = osunmaptbl [osrdygrp];
X = osunmaptbl [osrdytbl [y];
Prio = (Y <3) + X;

 
For example, if the value of osrdygrp is binary 01101000, the value obtained by checking osunmaptbl [osrdygrp] is 3, which corresponds to the 3rd-bit bit3 in osrdygrp, assume that the rightmost bit0 is 0th bits. Similarly, if the value of osrdytbl [3] is binary 11100100, the value of osunmaptbl [osrdytbc [3] is 2, that is, 2nd bits. Therefore, the task priority is equal to 26 (3*8 + 2 ). Use this priority value. Query the task control block priority table ostcbpriotbl [] to obtain the task control block OS _tcb pointing to the corresponding task.

5. Task status:
Ucos ii mainly has five task states: the sleep state is the suspended state, and the blocking state and the delay state are both in the waiting state. Added an interrupted status. UC/OS-ⅱ always creates an idle task, which is put into operation when no other task enters the ready state. This idle task [ostaskidle ()] is always set to the lowest priority. The idle task ostaskidle () does nothing, but keeps adding 1 to a 32-bit counter named osidlectr, the statistical task uses this counter to determine the actual CPU time consumed by the current application. Idle tasks cannot be deleted by application software.
A sleep state (dormant) indicates that a task is resident in the program space and has not been handed over to the μC/OS-ⅱ for management, to assign a task to μC/OS-ⅱ is to call one of the following two functions: ostaskcreate () or ostaskcreateext (). Once a task is created, the task is ready for running. A task can be created dynamically or dynamically before a multi-task starts. If a task is created by another task, and the task has a higher priority than the task that created it, the task just created will immediately gain control of the CPU. A task can return to the sleep state by calling ostaskdel () or calling this function to bring another task to sleep state.
You can call osstart () to start multiple tasks. The osstart () function runs the task with the highest priority in the ready state. A ready task enters the running state only when all tasks with a higher priority are in the waiting state or are deleted.
A running task can delay itself by calling one of the two functions, which are ostimedly () or ostimedlyhmsm (). This task enters the waiting state. After this time, the next task with the highest priority and enters the ready state is immediately granted control of the CPU. After the waiting time passes, the system service function ostimetick () changes the delayed task to the ready state (see section 3.10, clock beat ).
A running task also waits for an event to occur by calling one of the following three functions: ossempend (), osmboxpend (), or osqpend (). After the call, the task enters the waiting state (waiting ). When a task is suspended due to a pending event (pend), the next task with the highest priority immediately gets control of the CPU. When an event occurs, the suspended task enters the ready state. The report of the event may come from another task or the interrupt service subroutine.
A running task can be interrupted unless the task is interrupted or μC/OS-ⅱ is interrupted. The interrupted task enters the interrupted service state (ISR ). In response to the interruption, the task being executed is suspended, and the interrupt service subprogram controls the CPU usage. Interrupt Service subprograms may report the occurrence of one or more events, and make one or more tasks ready. In this case, μ c/OS-ⅱ should determine whether the interrupted task has the highest priority in the ready state task before returning from the interrupt service subprogram. If the interrupted service subroutine enables a task with a higher priority to enter the ready state, the new task with a higher priority will be run, otherwise, the interrupted task can continue to run.
When all tasks are waiting for the occurrence of the event or the end of the delay time, μC/OS-ⅱ executes the idle task and the ostaskidle () function.
 
6. Task Switching:
In some books, context switch is translated into context switch, which means task switching or CPU register Content Switching. When the multi-task kernel decides to run another task, it stores the current state (context) of the running task, that is, all content in the CPU register. The content is stored in the current storage area of the task, that is, the stack area of the task. (See figure 2.2 ). After the stack entry is complete, reload the current status of the next task from the stack of the task to the CPU register and start running the next task. This process is called task switching. The task switching process adds additional load to the application. The more internal registers the CPU has, the heavier the extra load. The time required for Task Switching depends on the number of registers on the CPU that need to be written into the stack. The performance of the Real-Time Kernel should not be evaluated by the number of task switches per second.

VII. Task Scheduling analysis:
Ucos ii provides the simplest Real-Time Kernel task scheduling and simple algorithms. Therefore, it only supports priority preemption task scheduling. It does not support time slice round training scheduling algorithms and priority reversal.
Ucos ii always runs to the one with the highest priority in the ready state task. Determine which task has the highest priority and which task is running is completed by sched. Task-level scheduling is completed by the function ossched. Interrupt-level scheduling is completed by another function osintext (), which will be described later.
The time taken by ucos ii Task Scheduling is a constant, independent of the number of tasks created in the application.
In UCOS, ostcbhighrdy was obtained first and then compared with ostcbcur. Because this comparison is a comparison of two pointer variables, it is relatively slow in 8-bit and some 16-bit microprocessors. In μC/OS-ⅱ, we compare two integers. Besides, you do not need to use pointer variables to check ostcbhighrdy when querying the task control block priority table ostcbpriotbl. Combining these two improvements, we can use integer comparison to replace pointer comparison and look up the table when task switching is required, this allows ucos ii to be faster than UCOS on 8-bit and 16-bit microprocessors.
To Implement Task Switching, ostcbhighrdy must point to the highest priority task control block OS _tcb, this is achieved by assigning the element in the ostcbpriotbl [] array with ospriohighrdy as the underlying object to ostcbhighrdy [l3.8 (4)]. Finally, the macro calls OS _task_sw () to complete the actual task switching [l3.8 (6)].
Task switching is simple. The following two steps are completed: Push the microprocessor register of the pending task into the stack, and then restore the register value of the higher-priority task from the stack to the Register. In ucos ii, the stack structure of the ready task always seems to be the same as the previous interruption, and all the microprocessor registers are stored in the stack. In other words, all you need to do is recover all the CPU registers and run the interrupt return command. In order to perform task switching and run OS _task_sw (), an interruption is simulated manually. Most microprocessors use Soft Interrupt commands or trap command traps to perform the above operations. Trap hardler, also known as exception handler, must provide an interrupt vector to the compilation language function osctxsw (). In addition to OS _tcbhighrdy pointing to the pending task, osctxsw () also needs to point the current task control block ostcbcur to the pending task.
All ossched () code is a critical code segment. In the process of finding the task with the highest priority in the ready state, interruption is disabled to prevent the interruption of the Service subprogram from putting one or more ready positions for the task. To shorten the switching time, all ossched () code can be written in assembly language. To increase readability, portability, and minimal compilation language code, ossched () is written in C.

Functions related to task switching: Related to the CPU system, compiled.
1. osstarthighrdy () executes the task with the highest priority
2. osctxsw () completes context switching of the task
3. Context switching after osintctxsw () is interrupted
4. ostickisr () interrupt service program startup

8. ucos ii initialization:
Osinit () creates an idle task, which is always in the ready state. The priority of the idle task ostaskidle () is always set to the lowest.
The task control blocks (OS _tcbs) of these two tasks are linked together with a two-way linked list. Ostcblist points to the starting position of the linked list. When a task is created, the task is always placed at the beginning of the linked list. In other words, ostcblist always points to the last created task. The end point of the link is null (that is, zero ).
Because both tasks are in the ready state, the corresponding bit in the ready task table osrdytbl [] is set to 1. Also, because the corresponding bits of these two tasks are on the same row of osrdytbl [], that is, they belong to the same group, only one bits in osrdygrp are set to 1.
Ucos ii also initializes four empty Data Structure buffers, as shown in f3.8. Each buffer zone is a one-way linked list that allows ucos ii to quickly obtain or release elements in a buffer zone. The number of OS _tcb blocks is automatically determined. Of course, there are enough task control blocks allocated to statistics tasks and idle tasks.

Ucos ii kernel scheduling Analysis VxWorks kernel scheduling Analysis
1. Only preemptible scheduling algorithms based on priorities are supported, and time slice round training is not supported; Use workqword to schedule tasks;
2.64 priorities. You can only create 64 tasks. You can only create 56 tasks; You can dynamically allocate stacks based on your needs to create any number of tasks;
3. Each task has different priorities.  
4. Priority reversal is not supported; Priority reversal is supported. TCB stores two priorities;
5. The ready queue uses the memory ing table for fast query. High Efficiency; Supports Task Scheduling for preemptible and time slice round training;
6. Supports clock beats; The compilation switch is used to support the multi-CPU architecture.
7. Support for semaphores, message queues, event control blocks, event flag groups, and message and email task communication mechanisms; Queues are implemented using a FIFO or priority two-way linked list;
8. Supports interrupt nesting. The number of layers of the interrupt nesting layer can reach 255. the interruption uses the stack of the current task to save the context; Interrupt Nesting is supported, and the interrupt uses a dedicated stack to save the context;
9. Each task has its own stack, which is set by the user; Tasks are managed based on classes and objects;
10. The task priority can be dynamically modified; Task priority can be dynamically modified;
11. The task TCB is a static array, and the Creation task only obtains a TCB from it, without dynamic allocation, releasing the memory; The TCB of the task is saved in the stack of the task;
12. The task stack is created statically or dynamically by the user. It is completed out of task creation and does not allocate dynamic memory; Each task has its own stack, and the stack size is set by the user;
13. The total number of tasks (OS _max_tasks) is determined by the user;  
14.0 highest priority, 63 lowest priority; The task has the highest priority from 0 to, and multiple tasks can have the same priority;
15. There is a idle task with the lowest priority, which runs when no user task is running. The system has no idle tasks to execute;

 

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.