Real Time Operating System – Basics

Before I started two years back on my first project, I have to admit that I did not know much about the working of a (real-time) operating system. All the stuff like – counting semaphores, task scheduling, context switches, event control blocks, interrupts and ISR routines, task and time management and what else and what not, all seemed a wee bit too much to grasp.

For me, the only thing I could think about when someone said Operating System before was a Windows OS or a Linux OS…and boy was ignorance bliss! As I started to write code for the first embedded system, I realized how naive I have been to think that I could always stick to Windows or a Linux machine and write small software programs and stay afloat.

As it dawned on me that this would not be the case in my new company, I began searching frantically to learn what an RTOS (aka, Real Time Operating System) could do and how it worked. It did take me a while and a lot of effort to finally get to know why we actually needed one and what all one could do with one. But I do not regret the time spent learning all that I could find on the topic. Even now I am still not an expert but at least not as naive and ignorant as before.

So I thought maybe, I should just note it all down somewhere maybe for future reference or just for anyone who wanted a quick feel or glimpse at what a real time OS was needed for or could do.

Chapter 1. RTOS – Fundamentals

To start, An RTOS is a multitasking operating system intended for real-time applications which mostly include those which run on embedded systems. The list of embedded systems that we find in our everyday life keeps growing exponentially every year. Nowadays you find embedded systems everywhere, be it household appliances- like microwaves, dishwashers, office appliances – like fax machines or copiers, computer peripherals – like modems, printers, scanners, or hi-tech stuff like robots, spacecrafts, automobiles, research equipments, weapons systems and what not.

The RTOS that I got to work with was called uCOS-II from Micrium. The Basic concepts that one has to know about before one actually starts off with an RTOS are –

Resources A resource simple means any entity, for example an I/O device like a keyboard, display, printer or a program defined array, variable or structure.
Tasks A Task or a thread as some may call it, is just a simple piece of program that thinks that it has all the resources and cpu all to itself. A task can typically have any of these states – Running, Dormant , Ready, Waiting. A task is said to be running when it has control of the CPU and it can execute its code. A task is said to be ready when it can execute its code, but its priority is lesser than the current task which is running. A task is said to be waiting when it is waiting for the occurrence of an event like an Input/Output operation to complete or an interrupt to occur or any resource to become available. A task is said to be dormant when it still resides in the memory and has not been made available to the kernel.
Multi Tasking Multitasking is the process by which several tasks or threads can run on the same CPU. In fact, better said when a single CPU switches its attention between various tasks in a sequential way enabling someone to think that all the tasks are being run at the same time.
Context Switches or Task Switches This is the process by which a kernel shifts its attention to a different task from the one it is currently executing. In order to enable context switches, the kernel must save all the running tasks CPU registers safely on to the stack and then load all the saved registers of the next task which is waiting to be run.
Kernel The Kernel is the central component of the Operating system and it manages all the tasks. Its main job is to enable the context switches or task switches. Its responsibilities also includes managing the various system resources.
Schedulers
First in First Out
Round Robin
Shortest Remaining Time
Priority Based
The Scheduler is the part of the Kernel who is responsible for determining which on the current tasks have to be run next.

There actually exists two types of priority based kernels – the preemptive ones and the non-preemptive ones.

The non-preemptive scheduling is also called co-operative multitasking; the tasks actually cooperate with one another to share the CPU and give the illusion that all of them are executing simultaneously. In real each task works for sometime and then gives the CPU control to the other tasks.

The preemptive one runs always the highest priority task which is ready to run. When a higher priority task becomes ready to run, the current task is pre-emptied ( or suspended) and the CPU control is given away to the new higher priority task. Once the higher priority task has finished execution the CPU control is transferred back to the earlier task which was suspended.