Introduction to Rtos (Real Time OS)

Rtos (Real time operating system) is a piece of software that switches between tasks, giving the impression that multiple programs are being executed at the same time on a single processing core.

In actual fact the processing single core can only execute one program at any one time, and what the RTOS is actually doing is rapidly switching between individual programming threads (or Tasks) to give the impression that multiple programs are executing simultaneously.

What is difference between OS and RTOS?

An operating system’s central role is to manage system resources to meet the demands of the applications that depend upon it. Operating systems have some essential elements such as a process scheduler, tasks, memory management, system: interface, filesystems multitasking, and synchronization, interrupt event handling, timers and clocks, inter-task communication, I/O, and memory management. where as,

A Real-time Operating System (RTOS) is an OS for devices and systems that need to react quickly to a trigger. In the case of a software fail-safe, for instance, an RTOS would pre-empt lower priority processes to take care of the higher-priority task.

The LiDAR and video imaging that autonomous vehicles use to drive the car down a highway without straying outside the lines or hitting anything is very compute-intensive. It makes sense that a complex system with life-safety responsibilities might have an RTOS so that a processor can make a quick decision.

On the other hand, an OS can be detrimental if it’s not really needed, and yet necessary if the system has a processor that must be interrupted so it can react to high priority tasks. Traditional MCUs are not going to run several threads simultaneously and are not going to have sophisticated interrupt capabilities. A while ago, the embedded world was mostly populated by MCUs due to the high price of performance computing and the fact that simple control loops were all that was needed.

The difference between an OS (Operating System) which is General prepose OS such as Windows/Linux and an RTOS (Real Time Operating System), is the response time to external events. OS’s typically provide a non-deterministic time response, where there are no guarantees as to when each task will complete, but they will try to stay responsive to the user.

An RTOS differs in that it typically provides a soft/hard real time response, providing a fast, highly deterministic reaction to external events. The difference between the two can be highlighted through examples – compare, for example, the editing of a text file on a PC to the operation of a precision motor control.

Real-Time Operating SystemGeneral Purpose Operating System
The RTOS always uses priority-based scheduling.Task scheduling in a OS isn’t necessarily based on which application or process is the most important. Threads and processes are often dispatched using a “fairness” policy.
The time response of the RTOS is deterministic.The time response of the general-purpose operating system is not deterministic.
A low-priority job in an RTOS would be pre-empted by a high-priority one if required, even executing a kernel call.A high-priority thread in a GPOS cannot preempt a kernel call.
The real-time operating system optimizes memory resources.The GPOS does not optimize the memory resources.
The RTOS is mainly used in the embedded system.GPOS is mainly used in PC, servers, tablets, and mobile phones.
The real-time operating system has a task deadline.The general-purpose operating system has no task deadline.
It doesn’t have large memory.
Rtos has small footprint in general since It has specific task to complete.
It has a large memory typically OS have big footprint since, It has general purpose os it has to cater all need.

RTOS kernel code is intended to be scalable, allowing developers to selectively select kernel objects.
GPOS code is not often modular in nature when it comes to development.
RTOS is designed and developed for a single-user environment.GPOS is designed for a multi-user environment.
Examples: FreeRTOS, Threadx,VxWorks etc.Examples: Linux, Windows, IOS, etc.

Now, when we have talked about differences between General purpose OS and RTOS. We need to understand one more concept.

We could categorize RTOS into two types:

  1. Soft RTOS “Soft real time os are those which are deterministic and time bound” but have margin of error” since we have little liberty in that sense. (e.g. rtos used in washing machines are soft rtos and in case of little margin of error it wont have much impact.) -FreeRTOS, Threadx etc.
  2. Hard RTOS “Hard real time os are those which are deterministic and time bound” but have no margin of error” since it could lead to catastrophic consequences.(e.g. rtos used Satellite launch are hard rtos since there is no margin of error since t could lead to catastrophic consequences in case of error).- Vxworks.

What makes RTOS different from the Simple microcontroller (BareMetal) program?

Difference between simple program running in microcontroller have no determinism and nor it time bound but with rtos we have control and can configure each task as per our use case.

The key factors in Real Time Operating Systems are minimum interrupt latency and minimum threads switching latency. The Real Time Operating System is valued more for how quickly and how predictably it responds to complete the tasks in a given period of time.

RTOS Pros

When we write good embedded software for Arduino we do not need FREERTOS but when its complexity and size increases FreeRTOS is always beneficial for the reasons listed below:

  • Abstract out timing information
  • Maintainability/Extensibility
  • Modularity
  • Cleaner interfaces
  • Easier testing (in some cases)
  • Code reuse
  • Improved efficiency
  • Idle time
  • Flexible interrupt handling
  • Mixed processing requirements
  • Easier control over peripherals

RTOS Cons

These are the advantages of using RTOS but there are also some disadvantages listed below:

  • Low Priority Tasks
  • Precision of code
  • Limited Tasks
  • Complex Algorithms
  • Device driver and interrupt signals
  • Thread Priority
  • Expensive
  • Not easy to program

Real-Time System Characteristics

5 CHARACTERISTICS OF AN RTOS
  • Determinism: Repeating an input will result in the same output.
  • High performance: RTOS systems are fast and responsive, often executing actions within a small fraction of the time needed by a general OS.
  • Safety and security: RTOSes are frequently used in critical systems when failures can have catastrophic consequences, such as robotics or flight controllers. To protect those around them, they must have higher security standards and more reliable safety features.
  • Priority-based scheduling: Priority scheduling means that actions assigned a high priority are executed first, and those with lower priority come after. This means that an RTOS will always execute the most important task.
  • Small footprint: Versus their hefty general OS counterparts, RTOSes weigh in at just a fraction of the size. For example, Windows 10, with post-install updates, takes up approximately 20 GB. VxWorks®, on the other hand, is approximately 20,000 times smaller, measured in the low single-digit megabytes.

How ​RTOS works?

Before starting with RTOS working, let’s see what is a Task. Task is a piece of code that is schedulable on the CPU to execute. So, if you want to perform some task, then it should be scheduled using kernel delay or using interrupts. This work is done by Scheduler present in the kernel. In a single-core processor, the scheduler helps tasks to execute in a particular time slice but it seems like different tasks are executing simultaneously. Every task runs according to the priority given to it.

Now, let’s see what happens in the RTOS kernel if we want to create a task for LED blinking with a one-second interval and put this task on the highest priority.

RTOS Working

Apart from the LED task, there will be one more task which is created by the kernel, it is known as an idle task. The idle task is created when no task is available for execution. This task always runs on the lowest priority i.e. 0 priority. If we analyze the timing graph given above, it can be seen that execution starts with an LED task and it runs for a specified time then for remaining time, the idle task runs until a tick interrupt occurs. Then kernel decides which task has to be executed according to the priority of the task and total elapsed time of the LED task. When 1 second is completed, the kernel chooses the led task again to execute because it has a higher priority than the idle task, we can also say that the LED task preempts the idle task. If there are more than two tasks with the same priority then they will run in round-robin fashion for a specified time.

Below the state diagram as it shows the switching of the non-running task into running state.

Arduino FreeRTOS LED Blinking State Diagram

Every newly created task goes in Ready state (part of not running state). If the created task (Task1) has the highest priority than other tasks, then it will move to running state. If this running task preempts by the other task, then it will go back to the ready state again. Else if task1 is blocked by using blocking API, then CPU will not engage with this task until the timeout defined by the user.

If Task1 is suspended in running state using Suspend APIs, then Task1 will go to Suspended state and it is not available to the scheduler again. If you resume Task1 in the suspended state then it will go back to the ready state as you can see in the block diagram.

This is the basic idea of how Tasks run and change their states. In this tutorial, we will implement two tasks in Arduino Uno using FreeRTOS API.

Frequently used terms in RTOS

1. Task: It is a piece of code that is schedulable on the CPU to execute.

2. Scheduler: It is responsible for selecting a task from the ready state list to the running state. Schedulers are often implemented so they keep all computer resources busy (as in load balancing).

3. Preemption: It is the act of temporarily interrupting an already executing task with the intention of removing it from the running state without its co-operation.

4. Context Switching: In priority-based preemption, the scheduler compares the priority of running tasks with a priority of ready task list on every systick interrupt. If there is any task in the list whose priority is higher than running task then context switch occurs. Basically, in this process contents of different tasks get saved in their respective stack memory.

5. Types of Scheduling policies:

  1. Preemptive Scheduling: In this type of scheduling, tasks run with equal time slice without considering the priorities.
  2. Priority-based Preemptive: High priority task will run first.
  3. Co-operative Scheduling: Context switching will happen only with the co-operation of running tasks. Task will run continuously until task yield is called.​

6. Kernel Objects: For signaling the task to perform some work, the synchronization process is used. To perform this process Kernel objects are used. Some Kernel objects are Events, Semaphores, Queues, Mutex, Mailboxes, etc. We will see how to use these objects in upcoming tutorials.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s