当前位置:网站首页>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
边栏推荐
- MongoDB
- Dependency in dependencymanagement cannot be downloaded and red is reported
- Detailed explanation of express framework
- Machine learning -- linear regression (sklearn)
- MySQL START SLAVE Syntax
- [CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
- I2C bus timing explanation
- Connexion sans mot de passe du noeud distribué
- Linux yum安装MySQL
- Contiki源码+原理+功能+编程+移植+驱动+网络(转)
猜你喜欢
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
Mysql database interview questions
分布式節點免密登錄
ToggleButton实现一个开关灯的效果
Come and walk into the JVM
Reno7 60W super flash charging architecture
Implementation scheme of distributed transaction
Linux Yum install MySQL
MongoDB
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
随机推荐
【kerberos】深入理解kerberos票据生命周期
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
机器学习--决策树(sklearn)
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
List and set
2020网鼎杯_朱雀组_Web_nmap
Wangeditor rich text component - copy available
wangeditor富文本组件-复制可用
分布式节点免密登录
互聯網協議詳解
FreeRTOS 任务函数里面的死循环
【Flink】CDH/CDP Flink on Yarn 日志配置
高通&MTK&麒麟 手機平臺USB3.0方案對比
小天才电话手表 Z3工作原理
Reno7 60W超级闪充充电架构
Pytorch-温度预测
Composition des mots (sous - total)
Mysql的索引实现之B树和B+树
I2C bus timing explanation
分布式事务的实现方案