OptimusPrime
OptimusPrime

Reputation: 97

What is the difference between an embedded program with RTOS and without RTOS

Can anyone explain to me what is the difference between an embedded program with and without RTOS. As when i start learning embedded, I always write code without any OS, all code is separated in to sub function and main function, sub function is called inside main function and it still run correctly, why does it still run without OS? And if I add RTOS to my code, what would happen? All answers are appreciated, thank you so much

Upvotes: 3

Views: 2214

Answers (2)

HelpingHand
HelpingHand

Reputation: 1468

what is the difference between an embedded program with and without RTOS [...]

I always write code [...] separated in to sub function and main function, sub function is called inside main function and it still run correctly

You mentioned the perfect starting point for answers: As you partition your code into separated functions, modules or classes (if we think beyond languages like C and assembler) by using function syntax and entering separate translation units into your linker tool, you can use an RTOS to partition the CPU the software runs on like if you had multiple CPUs, using a different one for every thing your software shall achieve. Please replace "achieve thing" by "run task cycle" now.

Note that unlike the function/module separation, the task context separation is not supported inside your programming language if you are using C or C++, for example. Therefore, you will have to integrate this separation with more manual work.

why does it still run without OS?

Don't confuse the RTOS/OS kernel with what you get if you visit Microsoft or the GNU/Debian/Fedora/SuSE Foundation (or Apple or Google or IBM or ...) and ask for an OS - they'll give you the actual OS kernel with lots and lots of applications that may be quite necessary to make productive use out of your target system (PC/handy) at all.

When talking about RTOS, we always consider the RTOS kernel. When talking about OS, we mean the kernel (unless we think of the pars pro toto usage as with "OSes" like Windows, Linux etc., which won't happen very often on SO). The kernel of an (RT)OS is the component that organises when to run a task and when to suspend it.

And if I add RTOS to my code, what would happen?

At first, nothing: You could apply an RTOS configuration with one single task and stuff the current implementation of your main loop software into that singleton task. This should work without limitations (If you take the parallelism to structure programs into functions - a main function is already the first function...).

But now you can start with a decomposition (architecture) of your software, adding tasks one by one, with small interfaces to the others. The RTOS library will implement those interfaces by providing you with inter-process communication elements (events/queues/mailboxes).

In an ideal RTOS-based architecture, every task shall receive its work packets as items from a mailbox (or event, or queue etc.). Whenever there isn't another item (message, event, queue data etc.) for the task, the RTOS kernel will block the task and switch over to another task that is ready for execution. This way, only tasks that have work to do will actually consume CPU time.

If there are multiple tasks which are ready for execution, the choice which task will run next depends on the scheduling algorithm, which you have to select when configuring the RTOS library. Usually, the tasks in a typical embedded system are assigned different priorities so that a task will only occupy the CPU if there is no ready task at higher prioity. Hence, an RTOS offers you a perfect solution to implement a system that implements both urgent and non-urgent tasks, e. g., tasks with and without real-time requirements.

Upvotes: 1

Nomios
Nomios

Reputation: 181

You didn't give any context to the question, but let assume you are trying to program some kind of micro-controller with a development environment that allows you to run with free RTOS.

Running without RTOS is the simple case you already understand - Your program starts in the main function and runs whatever loop or set of actions you have programmed.

Running with RTOS would add a set of .c files that, for the most part, implement a scheduler. You would then need to register the functions you want to run periodically as tasks in the scheduler before it starts its main loop. So the implementation of the OS would become part of your project, and compile along with your program.

To summarize, If you have decided you need to run multiple tasks, and that a scheduler would benefit your system, you can add RTOS instead of implementing the logic behind your loop on your own.

Upvotes: 2

Related Questions