当前位置:网站首页>Time slice polling scheduling of RT thread threads

Time slice polling scheduling of RT thread threads

2022-07-06 11:58:00 Weiyuan escort agency

Threads and priorities

Priority and time slice are two important parameters of threads , Describe the ability of threads to compete for processor resources and the ability to hold processor time .

RT-Thread Support 256 Priority . The smaller the numerical , The higher the priority .0 Is the highest priority , The lowest priority is reserved for idle threads . The user can go through rt_config.h Medium RT_THREAD_PRIORITY_MAX Macro to modify the maximum supported priority . in the light of STM32 Default setting maximum support 32 Priority .

In specific applications , There is no limit to the total number of threads , You can create multiple threads with the same priority . The total number of threads that can be created is only related to the memory of the specific hardware platform .

Thread timeslice

Time slices only work in ready threads of the same priority , The system adopts the scheduling method of time slice rotation for ready threads with the same priority . Time slice can constrain the single run time of thread , Its unit is a system beat (OS Tick).

Suppose there is 2 Ready threads with the same priority A and B,A The time slice of the thread is set to 10,B The time slice of the thread is set to 5, that , When there is no higher than A Thread priority ready thread , The system will be in A、B Switch back and forth between threads to execute , And every time A Threads execute 10 The length of a beat , Yes B perform 5 The length of a beat .

  Thread scheduling rules

Priority preemptive scheduling

The operating system always allows the highest priority ready tasks to run Limited : When the priority of a task is higher than that of the current task and it is in the ready state , System scheduling is bound to occur .

Through the priority preemption mechanism , It meets the real-time performance of the system to the greatest extent .

Time slice polling scheduling

When there are threads with the same priority in the operating system ( If the priority is the same, preemption will not occur ), The operating system will schedule threads in turn according to the time slice size set by threads , Time slice plays the role of restricting the single execution time of threads , Its unit is 1 A system beat (OS Tick).
Time slice polling scheduling mechanism , Ensure that threads with the same priority occupy the processor in turn .

Time slice polling scheduling example

timeslice_sample.c

1 #include <rtthread.h>
 2 
 3 #define THREAD_STACK_SIZE    1024
 4 #define THREAD_PRIORITY        20
 5 #define THREAD_TIMESLICE    10
 6 
 7 /*  Thread entry  */
 8 static void thread_entry(void* parameter)
 9 {
10     rt_uint32_t value;
11     rt_uint32_t count = 0;
12 
13     value = (rt_uint32_t)parameter;
14     while (1)
15     {
16         if(0 == (count % 5))
17         {           
18             rt_kprintf("thread %d is running ,thread %d count = %d
", value , value , count);      
19 
20             if(count > 200)
21                 return;            
22         }
23          count++;
24      }  
25 }
26 
27 int timeslice_sample(void)
28 {
29     rt_thread_t tid;
30     /*  Create thread 1 */
31     tid = rt_thread_create("thread1", 
32                             thread_entry, (void*)1, 
33                             THREAD_STACK_SIZE, 
34                             THREAD_PRIORITY, THREAD_TIMESLICE); 
35     if (tid != RT_NULL) 
36         rt_thread_startup(tid);
37 
38 
39     /*  Create thread 2 */
40     tid = rt_thread_create("thread2", 
41                             thread_entry, (void*)2,
42                             THREAD_STACK_SIZE, 
43                             THREAD_PRIORITY, THREAD_TIMESLICE-5);
44     if (tid != RT_NULL) 
45         rt_thread_startup(tid);
46     return 0;
47 }
48 
49 /*  Export to  msh  In the command list  */
50 MSH_CMD_EXPORT(timeslice_sample, timeslice sample);

The example creates 2 A dynamic thread , The last two parameters of the thread creation function are the priority of the thread and the time slice . In the example, the priority parameters of the two threads are the same , Time slice parameters are different .

Two threads use the same entry code .

In thread code , Save the parameters passed in by the thread in variables , And count . Then print thread parameters and count values .

Code is in the form of circular execution , But it is executed in sequence , Because a loop inside the loop jumps out .

RT-Thread Time slice and priority based scheduling

When time slice rotation and priority preemption coexist , If there is A B C Three priorities are used , among A higher than B higher than C.
If B There are multiple time slice based threads under the priority , Such as B1 B2. that B1 B2 It will be executed according to the order in the ready list , and C Will not be executed . If B Priority does not use time slice rotation , that B1 B2 It will be executed in the order of the ready list .

among flag1 by A Tasks executed by priority threads ,flag2 flag3 by B1 B2 Tasks performed by threads ,flag4 by C Tasks executed by priority threads

keil Simulation diagram

 

原网站

版权声明
本文为[Weiyuan escort agency]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913225637.html