RT-thread 时间片轮转法

时间片轮转

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

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

 
#include 
 
#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

你可能感兴趣的:(32)