# Zephyr Operating System: Real-Time Scheduling
> [!ai]+ Perplexity
>
> Zephyr, a small real-time operating system (RTOS) for connected, resource-constrained, and embedded devices, offers a comprehensive set of thread scheduling choices to ensure efficient task execution and resource management[13].
>
> ## Priority-Based Scheduler
>
> Zephyr's scheduler operates on a priority-based system, determining which thread is allowed to execute at any given point in time. This thread is known as the current thread. The scheduler ready queue is implemented as an array of lists, one per priority, with a maximum of 32 priorities. This corresponds to the scheduler algorithm used in Zephyr versions prior to 1.12. It runs in O(1) time in almost all circumstances with a very low constant factor, but it requires a fairly large RAM budget to store those list heads[1].
>
> ## Rate-Monotonic Scheduling (RMS)
>
> Rate-Monotonic Scheduling (RMS) is a formal technique for calculating response times resulting from a set of tasks, given their respective execution times. During the analysis, tasks are assigned priorities based on their execution time, so that the quickest task runs with the highest priority. RMS can provide a formal guarantee that task starvation will not happen, or, seen from the other angle, tell you how much processing capacity your system needs to avoid starvation[2][5][7].
>
> ## Earliest Deadline First (EDF) Scheduling
>
> Zephyr also supports Earliest Deadline First (EDF) scheduling, a dynamic priority scheduling algorithm used in real-time systems. In EDF, each task is assigned a priority based on its deadline, with the earliest deadline given the highest priority. This scheduling mode allows threads to set their deadlines for execution, ensuring that tasks with the most urgent deadlines are executed first[3][6][9][12].
>
> ## Cooperative and Preemptive Scheduling
>
> Zephyr supports both cooperative and preemptive scheduling. In cooperative scheduling, a running thread retains control of the CPU until it is ready to release it, either by completing its task or by explicitly yielding control. In preemptive scheduling, a higher-priority thread can interrupt a lower-priority thread, taking control of the CPU. This ensures that the most critical tasks are executed promptly[1][6][9][12].
>
> ## Time Slicing
>
> Zephyr's scheduler also supports time slicing, a technique that allows multiple threads of the same priority level to share the CPU. The scheduler divides time into slices and allocates each slice to a different thread, ensuring that all threads have an opportunity to execute[1].
>
> These scheduling features make Zephyr a versatile RTOS that can meet the real-time requirements of a wide range of applications, from simple embedded systems to complex IoT devices.
>
> Citations:
> [1] https://docs.zephyrproject.org/latest/kernel/services/scheduling/index.html
> [2] https://zephyrproject.org/rtos-debugging-when-the-cpu-has-too-much-on-its-hands/
> [3] https://github.com/zephyrproject-rtos/zephyr/blob/main/kernel/Kconfig
> [4] https://www.youtube.com/watch?v=6PpjYa1kJ1U
> [5] https://github.com/zephyrproject-rtos/zephyr/discussions/44971
> [6] https://docs.zephyrproject.org/latest/introduction/index.html
> [7] https://sternumiot.com/iot-blog/zephyr-rtos-features-examples-benefits-and-challenges/
> [8] https://www.embeddedrelated.com/showthread/comp.arch.embedded/196295-1.php
> [9] https://docs.zephyrproject.org/2.7.5/introduction/index.html
> [10] https://lwn.net/Articles/676391/
> [11] https://docs.zephyrproject.org/latest/kernel/timeutil.html
> [12] https://wiki.seeedstudio.com/XIAO-ESP32S3-Zephyr-RTOS/
> [13] https://en.wikipedia.org/wiki/Zephyr_(operating_system)
> [14] https://cese.pages.ewi.tudelft.nl/real-time-systems/assignment_a.html
> [15] https://www.reddit.com/r/embedded/comments/yuse2a/rtos_and_their_scheduling_algorithms/?rdt=47982
> [16] https://www.scaler.com/topics/zephyr-operating-system/
> [17] https://micro.ros.org/docs/concepts/rtos/comparison/
> [18] https://www.intel.com/content/www/us/en/develop/articles/introduction-to-the-zephyr-real-time-operating-system-rtos-with-the-intel-quark.html
> [19] https://www.electronicdesign.com/technologies/embedded/software/article/21273429/electronic-design-zephyr-34-an-rtos-of-a-different-stripe