No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:nrf52threads
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:aih:lab:nrf52threads [2023/11/23 13:50] – [Cooperative thread switching] ktokarz | en:edu:zmitac:s2:aih:lab:nrf52threads [2023/12/01 13:52] (current) – ktokarz | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| * cooperative | * cooperative | ||
| * preemptive | * preemptive | ||
| - | In the cooperative method, the thread is executed until it gives the control | + | Zephyr OS implements both methods of thread switching. |
| In the preemptive method, the thread is automatically suspended by the operating system when it decides to do so. Usually, the thread is given some period and is interrupted at the end of this period if there exists another thread to be executed. This method is implemented in general-purpose operating systems like Windows or Linux. It is much safer but more complicated and resource-consuming. In Zephyr preemptive threads have a positive priority number (thus they have a relatively lower priority). | In the preemptive method, the thread is automatically suspended by the operating system when it decides to do so. Usually, the thread is given some period and is interrupted at the end of this period if there exists another thread to be executed. This method is implemented in general-purpose operating systems like Windows or Linux. It is much safer but more complicated and resource-consuming. In Zephyr preemptive threads have a positive priority number (thus they have a relatively lower priority). | ||
| - | Zephyr OS implements both methods of thread switching. | + | |
| - | ===== Cooperative thread | + | ===== Thread |
| A cooperative thread gives control to the operating system in a few situations: | A cooperative thread gives control to the operating system in a few situations: | ||
| * if it decides to terminate by returning from its main function or by calling the k_thread_abort(). | * if it decides to terminate by returning from its main function or by calling the k_thread_abort(). | ||
| * if it decides to yield by calling the function k_yield(). | * if it decides to yield by calling the function k_yield(). | ||
| + | * if the thread is sleeping by calling some version of sleep function k_sleep(), k_msleep(), or k_usleep(). | ||
| + | * if the thread has been suspended by calling k_thread_suspend(). | ||
| + | |||
| + | While implementing the thread with the infinite loop it is recommended to call the k_yield() or some sleep function every pass of the loop. | ||
| + | |||
| + | A preemptive thread can be stopped by the operating system at any time if it finds the higher-priority thread ready to operate. | ||
| + | |||
| + | As the first example, we'll show the one available within the Nordic Developer Academy ((https:// | ||
| + | |||
| + | < | ||
| + | /* | ||
| + | * Copyright (c) 2017 Linaro Limited | ||
| + | * | ||
| + | * SPDX-License-Identifier: | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | /* STEP 2 - Define stack size and scheduling priority used by each thread */ | ||
| + | #define STACKSIZE 1024 | ||
| + | |||
| + | #define THREAD0_PRIORITY 7 | ||
| + | #define THREAD1_PRIORITY 7 | ||
| + | |||
| + | void thread0(void) | ||
| + | { | ||
| + | while (1) { | ||
| + | /* STEP 3 - Call printk() to display a simple string " | ||
| + | printk(" | ||
| + | /* STEP 6 - Make the thread yield */ | ||
| + | // | ||
| + | /* STEP 10 - Put the thread to sleep */ | ||
| + | k_msleep(5); | ||
| + | /* Remember to comment out the line from STEP 6 */ | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void thread1(void) | ||
| + | { | ||
| + | while (1) { | ||
| + | /* STEP 3 - Call printk() to display a simple string " | ||
| + | printk(" | ||
| + | /* STEP 8 - Make the thread yield */ | ||
| + | // | ||
| + | /* STEP 10 - Put the thread to sleep */ | ||
| + | k_msleep(5); | ||
| + | /* Remember to comment out the line from STEP 8 */ | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /* STEP 4 - Define and initialize the two threads */ | ||
| + | K_THREAD_DEFINE(thread0_id, | ||
| + | THREAD0_PRIORITY, | ||
| + | K_THREAD_DEFINE(thread1_id, | ||
| + | THREAD1_PRIORITY, | ||
| - | While implementing the thread with the infinite loop it is recommended to call the k_yield() function every pass of the loop. | + | </ |
| + | ===== The tasks for the laboratory ===== | ||
| + | To go through the lab please follow the steps: | ||
| + | - Copy-paste the code into some ready example in VSCode | ||
| + | - If you experience a problem with the include paths delete " | ||
| + | - Compile and execute the program, observe the behaviour of threads in the terminal | ||
| + | - Make some experiments as described below observing results in the terminal | ||
| + | * modify the priority of one thread | ||
| + | * modify sleep periods in k_msleep() functions | ||
| + | * change the priority of one and later of both threads to negative numbers making them cooperative | ||
| + | * change the method of passing control to OS - comment k_msleep() function call, uncomment k_yield() | ||
| + | * change version of k_msleep() to k_usleep(), k_sleep() - the latter one requires the k_timeout_t type structure as the parameter | ||
en/edu/zmitac/s2/aih/lab/nrf52threads.1700743803.txt.gz · Last modified: by ktokarz
