RT-thread 时间片轮转法

时间片轮转

主要用于分时系统中的进程调度。为了实现轮转调度,系统把所有就绪进程按先入先出的原则排成一个队列。新来的进程加到就绪队列末尾。每当执行进程调度时,进程调度程序总是选出就绪队列的队首进程,让它在CPU上运行一个时间片的时间。时间片是一个小的时间单位,通常为10~100ms数量级。当进程用完分给它的时间片后,系统的计时器发出时钟中断,调度程序便停止该进程的运行,把它放入就绪队列的末尾;然后,把CPU分给就绪队列的队首进程,同样也让它运行一个时间片,如此往复。例如:

首先创建2个优先级相同的线程,使他们的时间片不一样,验证是否按照时间片轮转调度线程。(注:shell线程的优先级也是20) 2个线程的入口代码完全相同,都是thread_entry,如果是新的时间片到来,那就打印相应线程信息。 这2个线程 分别在执行30个时间片后结束。

 
#include <rtthread.h>
 
#define THREAD_STACK_SIZE	1024
#define THREAD_PRIORITY	    20
#define THREAD_TIMESLICE    10
 
/* 线程入口 */
static void thread_entry(void* parameter)
{
    rt_uint32_t value;
    rt_uint32_t time,old_time,count = 0;
 
    value = (rt_uint32_t)parameter;
    while (1)
    {
        old_time = rt_tick_get();
        if(old_time == time)
        {
            /* 不是新的时间片,就什么也不干 */
        }
        else
        {
            /* 一个新的时间片到来,就打印一次,count用来计算现在是第几个时间片 */
            rt_kprintf("thread %d is running ,thread %d count = %d\n", value , value , count);
 
            count++;
            
            time = old_time;
            if(count > 29)
            {
               return;
            }
        }
     } 
}
 
int timeslice_sample()
{
    rt_thread_t tid;
    /* 创建线程1 */
    tid = rt_thread_create("thread1", 
                            thread_entry, (void*)1, 
                            THREAD_STACK_SIZE, 
                            THREAD_PRIORITY, THREAD_TIMESLICE); 
    if (tid != RT_NULL) 
        rt_thread_startup(tid);
 
 
    /* 创建线程2 */
    tid = rt_thread_create("thread2", 
                            thread_entry, (void*)2,
                            THREAD_STACK_SIZE, 
                            THREAD_PRIORITY, THREAD_TIMESLICE-5);
    if (tid != RT_NULL) 
        rt_thread_startup(tid);
    return 0;
}
 
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(timeslice_sample, timeslice sample);
 

仿真运行结果:

 
 \ | /
- RT -     Thread Operating System
 / | \     3.1.0 build Aug 27 2018
 2006 - 2018 Copyright by rt-thread team
msh >timeslice_sample
msh >thread 1 is running ,thread 1 count = 0
thread 1 is running ,thread 1 count = 1
thread 1 is running ,thread 1 count = 2
thread 1 is running ,thread 1 count = 3
thread 1 is running ,thread 1 count = 4
thread 1 is running ,thread 1 count = 5
thread 1 is running ,thread 1 count = 6
thread 1 is running ,thread 1 count = 7
thread 1 is running ,thread 1 count = 8
thread 1 is running ,thread 1 count = 9
thread 2 is running ,thread 2 count = 0
thread 2 is running ,thread 2 count = 1
thread 2 is running ,thread 2 count = 2
thread 2 is running ,thread 2 count = 3
thread 2 is running ,thread 2 count = 4
thread 1 is running ,thread 1 count = 10
thread 1 is running ,thread 1 count = 11
thread 1 is running ,thread 1 count = 12
thread 1 is running ,thread 1 count = 13
thread 1 is running ,thread 1 count = 14
thread 1 is running ,thread 1 count = 15
thread 1 is running ,thread 1 count = 16
thread 1 is running ,thread 1 count = 17
thread 1 is running ,thread 1 count = 18
thread 1 is running ,thread 1 count = 19
thread 2 is running ,thread 2 count = 5
thread 2 is running ,thread 2 count = 6
thread 2 is running ,thread 2 count = 7
thread 2 is running ,thread 2 count = 8
thread 2 is running ,thread 2 count = 9
thread 1 is running ,thread 1 count = 20
thread 1 is running ,thread 1 count = 21
thread 1 is running ,thread 1 count = 22
thread 1 is running ,thread 1 count = 23
thread 1 is running ,thread 1 count = 24
thread 1 is running ,thread 1 count = 25
thread 1 is running ,thread 1 count = 26
thread 1 is running ,thread 1 count = 27
thread 1 is running ,thread 1 count = 28
thread 1 is running ,thread 1 count = 29
thread 2 is running ,thread 2 count = 10
thread 2 is running ,thread 2 count = 11
thread 2 is running ,thread 2 count = 12
thread 2 is running ,thread 2 count = 13
thread 2 is running ,thread 2 count = 14
thread 2 is running ,thread 2 count = 15
thread 2 is running ,thread 2 count = 16
thread 2 is running ,thread 2 count = 17
thread 2 is running ,thread 2 count = 18
thread 2 is running ,thread 2 count = 19
thread 2 is running ,thread 2 count = 20
thread 2 is running ,thread 2 count = 21
thread 2 is running ,thread 2 count = 22
thread 2 is running ,thread 2 count = 23
thread 2 is running ,thread 2 count = 24
thread 2 is running ,thread 2 count = 25
thread 2 is running ,thread 2 count = 26
thread 2 is running ,thread 2 count = 27
thread 2 is running ,thread 2 count = 28
thread 2 is running ,thread 2 count = 29
 

线程1执行10个时间片 线程2之执行5个时间片 ... 线程1先执行完30个时间片后结束,剩下的只有线程2在执行,到30时也结束   

时间片就是线程执行的时间计数单位。

参考:https://blog.csdn.net/yang1111111112/article/details/82114874

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
RT-Thread是一个开源的嵌入式实时操作系统,支持多种不同的处理器架构。nRF52840是一款由Nordic Semiconductor开发的低功耗蓝牙微控制器芯片。 RT-Thread nRF52840是指RT-Thread在nRF52840芯片上的移植和运行。nRF52840具有强大的处理能力和低功耗特性,适用于物联网和无线通信应用。RT-Thread在nRF52840上的移植意味着RT-Thread的实时操作系统可以在该芯片上运行,并能够充分发挥其性能和功能。 通过将RT-Thread移植到nRF52840芯片上,可以实现以下功能: 1. 实时操作系统支持:RT-Thread是一个实时操作系统,具有任务调度、中断处理和内存管理等功能,可以实现快速响应和实时的系统行为。 2. 多任务支持:RT-Thread支持多任务并发执行,可以同时运行多个任务,并且可以根据优先级和时间片轮转来进行任务调度。 3. 低功耗支持:nRF52840芯片具有低功耗特性,结合RT-Thread的低功耗管理功能,可以实现优化的能源管理和延长设备的电池寿命。 4. 物联网支持:nRF52840芯片支持蓝牙低功耗(BLE)和IEEE 802.15.4等无线通信协议,与RT-Thread的网络协议栈配合使用,可以实现智能家居、传感器网络和物联网设备等应用。 5. 外设驱动支持:nRF52840芯片具有丰富的外设接口,如UART、SPI、I2C和GPIO等,RT-Thread提供了相应的驱动程序和中间件,可以方便地使用这些外设接口。 总之,RT-Thread在nRF52840芯片上的移植使得开发者可以利用RT-Thread的丰富功能来开发基于该芯片的嵌入式应用程序,实现实时、低功耗和物联网支持的应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气滴点C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值