Source Interpretation · RT-Thread multi-task scheduling algorithm

* This article based on RT-Thread the then-current version of the 4.0.1 version of the source code

RT-Thread operating system is a multitasking real-time operating system based on priority and round-robin of. Its scheduling algorithm uses 256 levels of priority, and support the same priority tasks exist. Using different priority task priority scheduling, the task of the same priority round-robin scheduling is used. In fact, this scheduling algorithm is the same in most systems, like I know μCos and freertos alike. But here need to understand a problem, but also a novice when I was troubled by the question - when there are multiple scheduling algorithm scheduling algorithm so when and what? And how to coexist with each other and coordinated by? After this will be read and understand the source code scheduling algorithms considered to understand which principles. In fact, the scheduling algorithm based primarily on the priority scheduling, to a secondary round-robin basis. That is only the case when there is no higher priority task ready, will want to adopt the same round-robin scheduling scheduling between priority task.

Priority Scheduling

What priorities are a multi-task scheduling? In fact, the task is to assign a numerical value, the smaller the value, the higher the priority of priorities. The low priority task scheduling algorithm directly in the reaction, the higher the priority of the priority response.

In the RT-Thread priority scheduling algorithm 256 the priority, the configuration can be defined by the macro. Typically cutting process will be defined according to the number of priority needs. However, in the source code it will reflect two different priority number differences are not less than 32 and 32. RT-Thread priority calculated using the bitmap algorithm. bitmap algorithm is the embodiment of a binary bit computing with the perfect combination. I believe we will then understand the code to a "horizontal slot, since this can also operate!" Really worship Gangster bitmap algorithm of the invention, but I do not know who first invented, the first contact is learning μCos time.

As mentioned earlier, RT-Thread priorities according to the number divided into (priority 32 and 32 above), differences between the two codes bitmap algorithm slightly, mainly in order to optimize resource usage. The case where not more than 32 with a variable priority only a 32bit after over 32 uses a priority byte length of the array elements 32, plus a variable for 32bit packet. Where no matter how many priorities, each priority only need to use a bit to indicate whether the corresponding priority task ready state (1 indicates Ready, 0 representing pending), it supports up to 256 levels of priority.

bitmap algorithm

To understand bitmap algorithm used to calculate priority, we must first master the decimal and binary conversion, and also need to have bit computing. RT-Thread with the situation in priority greater than 32 as an example to illustrate its 32 and a lower priority number of ways simpler, will simply be explained later. Look at the RT-Thread the source bitmap algorithm on several variables will be used:

rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
struct rt_thread *rt_current_thread;
rt_uint8_t rt_current_priority;

#if RT_THREAD_PRIORITY_MAX > 32
    /* Maximum priority level, 256 */
    rt_uint32_t rt_thread_ready_priority_group;
    rt_uint8_trt_thread_ready_table[32];
#else
    /* Maximum priority level, 32 */
    rt_uint32_t rt_thread_ready_priority_group;
#endif

 

RT-Thread In the case where the priority is greater than 32, the task priority is divided into 32 groups of eight. Rt_thread_ready_priority_group variable with its packet management, this is a 32bit variable representative of each bit group of a ready state. And each group of eight priority levels are used to manage rt_thread_ready_table array, each element occupies a 8bit size, each bit representing a same priority. In the source code a maximum priority from 0 to 255, the specific situation is every eight packets for a group represented by a bit, in turn divided into 32 groups were required to represent 32 bit packet, i.e. rt_thread_ready_priority_group variable. When a given task group priority corresponding to a ready state of the corresponding bit will be set to 1. 19, for example, the priority task in the ready, it is in the case of the packet according to a third set (16-23), so that the rt_thread_ready_priority_group the third bit is set to 1 (that is, the third bit bit2, because usually accustomed to bit 0 count starts). this scheduling algorithm in a subsequent process scheduler know this group has a task in the ready. In further detail, but also in the third rt_thread_ready_table element (subscript 2) in the fourth bit (bit3) is set (as in the third group 16, 17, 19 located in the fourth ). this accurately identifies unique priority number.

If there are tasks that more groups are at the same time to calculate how ready the highest priority it? If there are simultaneous in a ready task will be the corresponding bit in the packet set in rt_thread_ready_priority_group 1, while the corresponding group in the rt_thread_ready_table will set bit 1 of the previously mentioned are higher priority priority number, For example, priority 0 is the highest priority, the priority of 255 is the lowest priority. This can be inferred that the smaller the priority number will correspond on the lower rt_thread_ready_priority_group bit. Easy to see bit0 0 to 7 Priority levels corresponding rt_thread_ready_priority_group variable, empathy 8-15 correspond bit3 correspond to bit2,16 than 23 and the like. It is assumed that the task priority 5 and 19 in the ready state, which then bit0 and bit2 rt_thread_ready_priority_group variables will be set to 1, while rt_thread_ready_table [0] and the bit5 rt_thread_ready_table [2] will be set to 1. The bit3 as shown below: 

 

 

 

At this time, if the scheduler schedules should be to calculate the 5 priority task to perform. In fact, this points to a three-step calculation.

First take rt_thread_ready_priority_group Variables ffs function calculates the LSB bit 1 is the first of several bit. Obviously, this is the first example bit0 bit is 1, if this result we use the temporary variable called index up, then the index is equal to 1.

The first step in the second step using the result of calculation as an index rt_thread_ready_table index (the index is zero for the first, so index-1), i.e. rt_thread_ready_table [0]. Once again, do ffs calculate the least significant bit (rt_thread_ready_table [0]) is bit 1 is the first of several, apparently example bit5, if we re-use variables to store the offset value, the offset is equal to 6.

A third step according to the calculated results of the first two steps of the final index and offset of the highest priority (index-1) * 8 + (offset-1), where the multiplication can be used instead of bit operation, it is equivalent to (( . index-1) << 3) + (offset-1) RT-Thread its real source as follows:

register rt_ubase_t highest_ready_priority;

#if RT_THREAD_PRIORITY_MAX <= 32
    highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;

#else
    register rt_ubase_t number;

    number = __rt_ffs(rt_thread_ready_priority_group) -1;
    highest_ready_priority = (number << 3) +__rt_ffs(rt_thread_ready_table[number]) - 1;
#endif

 

__Rt_ffs code thereon the return value of the index variable to the above embodiment, the operation is done since the index minus 1 bit and start from 0. highest_ready_priority that is calculated from the highest priority.

About ffs function implementation details can be seen RT-Thread source in a variety of implementations, there are C functions to achieve, but also to achieve a variety of special instructions for the processor and compiler optimization of. But its function is to calculate a value of binary bits is the lowest bit 1 is the first of several. Indexed array made a 0 to 255 C in the RT-Thread function implementation, the value of the array which is 0 to 255, respectively, these values ​​corresponding to the bit of index 1 is the lowest bit.

Finally explain, as shown in the code above, when the priority number is defined as no more than 32, there is no rt_thread_ready_table, and has a smaller footprint. Can also be understood as only a priority of each, it can be directly used instead of directly rt_thread_ready_priority_group. Because only a maximum of 32 priority, rt_thread_ready_priority_group just 32bit, each bit representing a priority on exactly correspond.

Round-robin scheduling

In the description of round-robin scheduling ago, first explain what is the time slice. In operating systems, the concept of time slice is relative to the operating system TICK interrupted. TICK interrupt every triggering the equivalent of a time slice.

Round-robin scheduling will be the task of the current time slice minus one at each TICK interrupt other tasks and then check the remaining time slices situation. Once the current task time slice runs out, it will reset the first time slice of the current task. Then see if there want the same priority task, if the current task will be moved to the end of the queue. Then trigger the task priority scheduling, this time as long as the current priority is ready highest priority will eventually remove the head of the queue of the same priority to run. Other factors aside simply means that as long as the current task time slice runs out, it will be the current task to the end of the queue, the next task will automatically run in the head of the queue. So this seems to turn every task to run, just run the length of time each task just not the same, this runs the length of time that is specified by the time slice.

In summary, the round-robin scheduling reflects the premise that the establishment of a plurality of the same priority task. Because the round-robin scheduling occurs only between the same priority tasks. Otherwise, the system can be considered to exist only priority scheduling.

The following figure shows the three tasks of the same priority round-robin scheduling operation:

 

 

Guess you like

Origin www.cnblogs.com/rocotona/p/11096673.html