Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Drag & Drop Files, Choose Files to Upload 您最多可以上传 2 个文件。
  • 简体中文
    Eng
    简体中文
  • 练习 2 – 时间片轮转

    时间片轮转

    如果您不想担心为同优先级线程之间的让权创建完美的逻辑,您可以使用时间片轮转。

    在本练习中,我们将深入研究时间片轮转,以及它如何影响同优先级和不同优先级线程之间的交互。

    练习步骤

    1. 在本课程的 GitHub 仓库中,打开此练习的基础代码,位于 l7/l7_e2。

    2. 正如您在 main.c 中看到的,线程的执行函数既不休眠也不让权。

    void thread0(void)
    {
    	while (1) {
                printk("Hello, I am thread0\n");
                k_busy_wait(1000000);
    	}
    }
    
    void thread1(void)
    {
    	while (1) {
                printk("Hello, I am thread1\n");
                k_busy_wait(1000000);
    	}
    }
    C

    我们现在知道,第一个开始运行的线程将无限期地阻塞另一个线程。我们添加 k_busy_wait() 的原因是防止串口终端被日志淹没。此函数调用会导致当前线程在指定的时间(以微秒为单位)内执行空循环。此函数仅用于调试目的,不建议用于生产代码。

    在开发套件上编译并烧录应用程序,您应该会观察到 thread0 导致 thread1 饥饿,这是因为 prj.conf 中禁用了时间片轮转。

    *** Booting nRF Connect SDK ***
    Hello, I am thread0
    Hello, I am thread0
    Hello, I am thread0
    Hello, I am thread0
    Terminal

    3. 为了避免 thread0 导致处于相同优先级水平的 thread1 饥饿,让我们在项目中启用时间片轮转,方法是在 prj.conf 文件中将 CONFIG_TIMESLICING 由 n 改为 y,并添加下面几行配置,如下所示。

    CONFIG_TIMESLICING=y
    CONFIG_TIMESLICE_SIZE=10
    CONFIG_TIMESLICE_PRIORITY=0
    Kconfig
    • TIMESLICING 启用时间片轮转功能。
    • TIMESLICE_SIZE 当前正在运行的线程最长执行时长(单位:毫秒),超时后调度器会强制抢占,让其他同优先级线程获得运行机会。
    • TIMESLICE_PRIORITY 是时间片轮转的优先级阈值,这意味着优先级高于此阈值的线程不受时间片轮转的影响。我们将此值设置为 0。这意味着只有当一个级别中存在两个或更多线程时,优先级(0 到 15)才会受到时间片轮转的影响。请务必记住,时间片轮转仅影响具有相同优先级水平的线程。

    注意

    从 nRF Connect SDK v3.1.1 开始,默认启用时间片轮转,默认 TIMESLICE_SIZE 等于 20 毫秒。

    4. 构建应用程序并将其烧录到您的开发套件中。

    使用串口终端,您现在应该看到以下输出:

    *** Booting nRF Connect SDK ***
    Hello, I am thread0
    Hello, I am thread1
    Hello, I am thread0
    Hello, I am thread1
    Hello, I am thread0
    Hello, I am thread1
    Hello, I am thread0
    Hello, I am thread1
    Hello, I am thread0
    Hello, I am thread1
    Hello, I am thread1
    
    Hello, I am thread0
    Hello, I am thread1
    Hello, I am thread0
    Hello, I am thread1
    Hello, I amHello, I am  thread0
    thread1
    Terminal

    输出结果表明:调度器会在配置的时长(本例为 10 毫秒)到达后,强制抢占当前正在运行的线程,无论线程正在执行什么操作。我们能观察到,即便线程还没有完整打印完 printk() 信息,调度器也会对它实施抢占。 注意:被切换回来的线程,会从上次被抢占的位置继续打印。

    执行时序大致如下。

    抢占式时间片轮转时序图

    thread0 运行满 10 毫秒后,调度器对其执行强制抢占,切换到 thread1 运行 10 毫秒;随后 thread1 被抢占,再次把 CPU 时间交给 thread0。

    需要注意的是,时间片配置仅影响同优先级线程。那么,当启用时间片轮转且在时间片到期后没有同优先级线程可以运行时,会发生什么呢?

    我们可以通过提高其中一个线程的优先级(即降低优先级数值)来找出答案。

    5. 将 THREAD0_PRIORITY 的值更改为 6,如下所示,使 thread0 的优先级高于 thread1。

    #define THREAD0_PRIORITY 6
    #define THREAD1_PRIORITY 7
    C

    6. 构建应用程序并将其烧录到您的开发套件中。使用串口终端,您现在应该从输出中看到 thread0 不间断地运行,导致 thread1 永远饥饿。

    虽然打印输出不一定能体现这个过程,但调度器依然会每 10 毫秒抢占一次 thread0,检查就绪队列里是否存在其他同优先级或更高优先级的就绪线程。本例中另一个线程 thread1 优先级更低,因此调度器会让 thread0 继续占用下一个时间片。由于 thread1 的优先级始终更低,thread0 会持续这样一直运行。

    此练习的解决方案可以在 GitHub 仓库 l7/l7_e2_sol 中找到。

    Switch language?

    Progress is tracked separately for each language. Switching will continue from your progress in that language or start fresh if you haven't begun.

    Your current progress is saved, and you can switch back anytime.

    Register an account
    Already have an account? Log in
    (All fields are required unless specified optional)

    • 8 or more characters
    • Upper and lower case letters
    • At least one number or special character

    Forgot your password?
    Enter the email associated with your account, and we will send you a link to reset your password.