Task Management Data Structure:
The data structure of task management includes:Task Control Block, idle list, ready list, priority list, task stack, etc,Is μC/OS-IIOne of the core components of the kernel.
The Declaration of the task control block entity is as follows:
OS _tcb ostcbtbl [OS _max_tasks + OS _n_sys_tasks]
OS _max_tasksThe maximum number of user tasks,OS _n_sys_tasksIs the number of system tasks, usually2.
Task priority table
OS _tcb * ostcbpriotbl[OS _lowest_prio + 1]
OS _lowest_prio is the priority of the lowest priority task. because the value of the low priority task is the largest, and the task priority starts from 0, OS _lowest_prio + 1 is the number of tasks.
The array ostcbpriotbl has a maximum of several elements of a task. Its type is a pointer to the task control block.
Task Stack definition:
# Define task_stk_size 512
Typedef unsigned int OS _stk;
OS _stk taskstk [OS _max_tasks] [task_stk_size];
Task_stk_size is the size of each task stack. Set it to 512 here. You can modify this value when porting the task. OS _max_tasks is the number of user tasks.
Task Control Block Initialization
PrioPriority of the created task
PtosTask Stack top address
PbosThe address at the bottom of the task stack.Ostaskcreate ()There is no extended function for the task to be created, and the stack check is not available. This parameter can be passedNull
IDTaskID,16Bit. The value range is0To65535
Stk_sizeStack size
PextThe address of the extended block of the task control block.
OPTOther options
Returned value: OS _err_noneSuccessful call
OS _err_task_no_more_tcbIf there is no idle task control block
Task deletion:
When the priority of other tasks is used as the parameter,OstaskdelThe task is deleted roughly, which is valid in some cases, but not required. It is better to notify the target task and tell it to delete you. In this way, the task can discard its own resources before deleting itself, such as the buffer zone, semaphore, mailbox, queue, and so on. If you always useOstaskdelDelete a task. If the resources occupied by this task cannot be released, the system will generate a memory leak. When the Memory Leak accumulates to a relatively large value, the system crashes because there is no available memory.
OstaskdelreqAlthough the name is a request, it is set request and response in a segmentCode. The function of this Code is:
1.Request to delete a task2.Check whether there are tasks to delete yourself
Task Scheduler:
μC/OS-IIIt is a real-time operating system based on priority scheduling. Therefore, after a multi-task is started, each clock is interrupted and the task scheduling is executed. If the time slice is20In milliseconds20Execute a task scheduling in milliseconds. The task scheduling function isOstimetick.
Idle task:
Idle task isμC/OS-IIBecause it occupies the lowest priority63So only when other tasks are blocked due to waiting for an event can they be run. .
The code of the idle task shows that the idle task does not do anything except to add the value of osidlectr of the idle counter to 1. When no other task can run, the operating system will execute this code. By default, ostaskidlehook is just an empty function. We don't need to fill it out without special requirements. Another function is used to take a moment, give the system enough time to respond to interruptions. Next, let's take a look at another system task with a higher priority than the idle task, and count the task.
μC/OS-IISpecifies that a user applicationProgramThis idle task must be used and cannot be deleted using software.
Statistical task
OS _taskstat is another important system task of μ c/OS-II , we can cancel a statistical task through macro settings, but we generally do not, because the statistical task is important. The main functions of a statistical task are computing CPU usage . Without statistical tasks, we cannot know whether the system runs well in a multi-task environment.
μC/OS-IIAnother system task provided is a statistical task.Ostaskstat (). This statistical task is calculated once per second.CPUThe time used within the unit time, and the calculation result is in the form of a percentage.Stored in VariableOscpusageSo that the application can understand it by accessing it.CPUSo this system taskOstaskstat (
)It is called a statistical task.
User task:
The system automatically assigns the lowest priority OS _lowest_prio to idle tasks. If a statistical task is also used in the application, the priority OS _LOWEST_PRIO-1 is automatically assigned to the statistics task becauseThis user task can use Priority: 0, 1, 2... OS _LOWEST_PRIO-2, A total of OS _LOWEST_PRIO-1
Task creation:
Void main (void)
{......
Osinit (); // initialize the μC/OS-II
......
Ostaskcreate (taskstart ,......); // Create task taskstart
Osstart (); // start multi-task scheduling
}
Void taskstart (void * pdata)
{
...... // Install and start the μC/OS-II clock at this location
Osstatinit (); // initialize a statistical task
...... // Create other tasks in this location
For (;;)
{
Code for starting task taskstart
}
}
Task Switching:
The processor uses two pointer registers (PC and SP) to establish a connection with the task code and task stack and run it.
The system assigns the address of the program to be run to the program counter PC to implement program switching.
In fact, the key to program switching is to assign the private Stack pointer of the program to the stack pointer SP of the processor.
In essence, The system implements program switching through SP switching.
Create a concept: a program with control blocks is a task that can be run by the system. Program code, private stack, and task control block are the three requirements of a task. The task control block provides the storage location of the runtime environment.
Task Scheduling is the core task of a multitasking operating system.
The so-called scheduling is throughAlgorithmThe function used to determine the running task among multiple tasks is called a scheduler.
μC/OS _iiThe idea of task scheduling is: "Almost always puts the highest priority ready tasks in the running state at all times"
. To ensure this, it always calls the scheduler when the system or user task calls the system function and executes the interrupted service program,To determine the task to be run and run it.
The task scheduling is based on the task readiness table,
Do not use the push and pop commands to press the stack and exit the stack of the program counter PC, because there is no such command. I had to make some changes.The interrupt action and process call command can enable the PC to press the stack; the interrupt return command can enable the PC to exit the stack.
Therefore, switching an osctxsw () task must be an interrupted service program.
MacroOS _task_sw ()To cause an interruption or call to enable osctxsw () to execute a task switchover.
Priority Inheritance Mechanism:
The Priority Inheritance Mechanism upgrades the priority to optimize system scheduling. For example, the priority of the current task is relatively low, for example, the priority is 50. When a task with a priority of 3 requests a mutex semaphores, it is blocked because the semaphores are occupied. At this time, a task with a priority of 20 is ready without requesting the mutex semaphores. Therefore, a task with a priority of 20 runs first. If another task with a priority of 30 or 40 runs, the semaphore cannot be released if the task with a priority of 50 fails to run, what's even worse is that the task with priority 3 is still waiting for the semaphore. In this way, the priority is reversed. The solution in the code is to increase the priority of the task that occupies the semaphore, for example, to 2, so as to ensure that the processing of the mutex resource is complete, and after the resource is released, his original priority is restored to 50, for a task with a priority of 3, you do not need to wait for the running of those medium-priority tasks with a priority of 20, 30, and 40. The priority reversal is corrected!
PortUCOSConditions to be met:
Details:Http://note.youdao.com/share? Id = 5552e547025dfa2953ce22d244fec058 & type = Note