FreeRTOS — We are all Preemptive Scheduler!

Some Important Concept for Real-Time Operating System (RTOS) — IoT — Episode#03

J3
Jungletronics
6 min readJan 4, 2020

--

Hi, here you’ll find important RTOS’ dialects so that you will be more comfortable when we deal with a real-time operating system (FreeRTOS) algorithm later.

Let’s begin with one teasing question: Are we all Preemptive Scheduler?

Yes, unfortunately, yes! In this era of the internet and the omnipresence of cell phones, all of us do have no focus on what really matters!

Let me explain it better…

But first, let’s begin with the Operational System (OS) concept.

A modern operating system — like Windows, Linus, Apple macOS Catalina — ac OX — is computer software, firmware, and possibly hardware that interact at a low level with the computer system’s hardware components to manage the sharing of the computer’s resources among various software applications.

But we are all microcontrollers hobbyists…We have no Windows 10 at Arduinos…Yep, you’re right…but…

Let us meet the concept of a scheduler as tasks are more intuitive…

What is uC scheduling after all?

Well, in the microcontroller world there is something called a real-time operating system (RTOS).

Don’t you hear anything about it? Please consider reading this article first…

Anyway, by using RTOS in microcontrollers one machine can serve real-time applications. It will deliver a task at that specified time, not before…not after…at that exact pre-defined time! This is a Real-time app. The variable is Time! We are concerned about time, only about time.

This is an important requirement for a real-time system that our modern way of life might depend upon, as an aviation system control board, heath instruments, and the likes…

Task time table scheduling will be guarantee by RTOS.

Anyway, a scheduler is what?

RTOS scheduler is an algorithm the RTOS uses to schedule threads (tasks). The RTOS scheduler determines which thread to run on each CPU in the system at any given time. Run queue — The queue of threads ready to be scheduled. Threads in the run queue are either executing or ready to execute.

There are two main ways of working with tasks implemented by the scheduler: Preemptive and Non-Preemptive Scheduling.

Let me try to convey the idea by graphics.

Preemption— Basics types

In general, preemption means prior seizure-of. When the high priority task at that instance seizes the currently running task, it is known as preemptive scheduling.

As said, there are two types of scheduler related to it: Preemptive and Non-preemptive.

Non-preemptive

A Non-preemptive queue system happens when a high priority packet waits for a low priority packet to be transmitted (or executed).

Fig 1. This is indicated for a printer spool, right? What is a printer spool? btw a printer spool is a software program in Microsoft Windows that is responsible for managing all print jobs currently being sent to the computer printer or print server. The print spooler program allows a user to delete a print job being processed or otherwise manage the print jobs currently waiting to be printed

Note that even if the highest priority Task 2 is ready, the scheduler only runs it when Task 1 (lowest priority) finishes.

Algorithms based on non-preemptive scheduling are FIFO, Shortest Job First (SJF basically non-preemptive), and Priority (non-preemptive version), etc.

Preemptive

A Preemptive queue system exists when a high priority packet does not have to wait, ie even when a low priority packet is already willing or in the process of transmission, it will be interrupted and put on hold condition while the high priority packet is handled. (this is how our world works, right? think about when a cell rings in the middle of a chatting…)

Fig 2. This is what happens when you are in a chat with a colleague and the cellular ring :/ you are Task 1, the caller is Task 2 :/

Now when the highest priority Task 2 is ready the scheduler immediately runs it, even when Task 1 is still being processed.

Algorithms based on preemptive scheduling are: Round Robin(RR), Shortest Remaining Time First (SRTF), Priority (preemptive version), etc

Politics

Fine, now related to the Scheduling Behavior in Real-Time Operating Systems there are two most used politics: FIFO and Round Robin.

First:

FIFO (Fist-in First-out)

The next task to be performed is the highest priority that is in the longest wait ready state.

Fig 3. Non-preemptive scheduler (this happens in a bank queue, for example, seniors have priority over pregnant women over young people and so on). This is a non-preemptive system queue.

Then the second:

Round Robin

Fig 4. This happens only on The ShareWood forest :). This is a preemptive system queue.

Each task has the right to enjoy a minimal amount of available resource time, In this case: 50 ms.

Note that although Task 2 is below Task 1 in terms of priority, as it consumes the perfect part (50 ms), it is fully satisfied even before Task 1.

The beauty of this queue is emphasized by the minimalistic and natural design. The uniqueness of every handmade product lays mainly in the low quantity of produced items. This guarantees the balance with the environment and ecological character of the handicraft.

Another important concept: Context Switching!

When the task is resumed its saved context is restored by the RTOS kernel prior to its execution. The process of saving the context of a task being suspended and restoring the context of a task being resumed is called context switching.

Context switching is similar to interruptions. Data is saved to the respective stack of each task; when the task returns, its values are restored.

And now you are ready to go deep on the FreeRTOS systems on ESP-32.

I see you in the next IoT episode. Be tuned!

Bye o/

IoT Episodes Posts:

1° Episode — IoT — Meet IoT Boards: ESP8266 & ESP32 — Which are the best? ESPs or RPis or Arduinos Boards?

2° Episode — IoT — FreeRTOS Why To Use It? — IoT Projects? What is it?

3º Episode — IoT — FreeRTOS — We are all Preemptive Scheduler! — Some Important Concept for Real-Time Operating System (RTOS) — (this one:)

4° Episode — IoT — FreeRTOS — More About it! — The Essence of FreeRTOS

5° Episode — IoT — FreeRTOS A Real Real-Time TV Transmissions Fundamental Understanding of FreeRTOS Apps

References & Credits & Recommended Posts:

BTW

First Come First Serve (FCFS)

It is a non-preemptive, pre-emptive scheduling algorithm. Easy to understand and implement. Its implementation is based on the FIFO queue. Poor performance as the average wait time is high.

Round Robin is the preemptive process scheduling algorithm. Each process is provided a fixed time to execute, it is called a quantum. Once a process is executed for a given time period, it is preempted and another process executes for a given time period. Context switching is used to save states of preempted processes.

Why choose FreeRTOS?

“It’s probably safe to say at this point that FreeRTOS goes through more ‘peer-review’ than any other RTOS available on the planet. I have used it in several projects — one of which was a multiprocessor environment that used more than 64 processors and needed to run for months reliably. The RTOS core performed well. Take FreeRTOS for a spin.” — John Westmoreland

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!