当前位置:网站首页>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
边栏推荐
- Come and walk into the JVM
- Common regular expression collation
- 2020网鼎杯_朱雀组_Web_nmap
- [CDH] modify the default port 7180 of cloudera manager in cdh/cdp environment
- 【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
- Raspberry pie tap switch button to use
- Selective sorting and bubble sorting [C language]
- MongoDB
- [Bluebridge cup 2021 preliminary] weight weighing
- 数据库面试常问的一些概念
猜你喜欢
随机推荐
Machine learning -- linear regression (sklearn)
Pytorch实现简单线性回归Demo
机器学习--线性回归(sklearn)
Correspondence between STM32 model and contex M
【Flink】CDH/CDP Flink on Yarn 日志配置
Detailed explanation of express framework
Oppo vooc fast charging circuit and protocol
Basic use of pytest
[NPUCTF2020]ReadlezPHP
Come and walk into the JVM
RT-Thread API参考手册
RT-Thread 线程的时间片轮询调度
Reno7 60W super flash charging architecture
4. Install and deploy spark (spark on Yan mode)
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
RT-Thread的main线程“卡死”的一种可能原因及解决方案
MongoDB
分布式事务的实现方案
MongoDB
STM32 如何定位导致发生 hard fault 的代码段