Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do RTOS tasks have to be executed in infinite loop?

Tags:

c

rtos

Hi I'm a newbie in RTOS and in almost every document I read, it indicates that the tasks must be in infinite loop but none states why. Can anyone help explain this? Thanks

like image 950
annena Avatar asked Mar 10 '15 07:03

annena


People also ask

What is infinite loop in embedded system?

An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

What are the three states of an RTOS task?

In the FreeRTOS a task can be in either of four different states viz., Running, Ready, Blocked and Suspended. Lets see each state one by one.

What is the role of task Scheduler in RTOS?

FreeRTOS allows us to set priorities for tasks, which allows the scheduler to preempt lower priority tasks with higher priority tasks. The scheduler is a piece of software inside the operating system in charge of figuring out which task should run at each tick.


2 Answers

They don't, but if a task function runs to completion, the task is terminated.

You could perhaps choose to instantiate a new run-to-completion task every time one is needed, but instantiating a task is time consuming and potentially non-deterministic, so not suited to hard real-time response. It is more efficient and responsive to have a task wait on some blocking object such as an event, semaphore or timer, so that it can respond deterministically every time it is required. On the other hand, if you have many different non-real-time tasks that only need to run occasionally, the run-to-completion pattern can save resources.

At least one task needs to run indefinitely however - if everything just stops your system will do nothing until the power is cycled or reset asserted (by a watchdog timer for example).

RTOS implementations vary, you will need to check how your RTOS handles terminating thread functions. You may need an explicit termination call to ensure resources are released by the kernel.

If you have a system that needs some form of controlled graceful shut-down, you need not code tasks as infinite loops, but have the loop exit conditionally so that the task can terminate on request, e.g;

while( (event_flags & TERMINATE) == 0 )
{
    event_flags = eventWait( WAIT_FOREVER ) ;

    // handle events
    ...
}
like image 62
Clifford Avatar answered Sep 19 '22 12:09

Clifford


I don't think it's entirely accurate to say that "an RTOS task must be an infinite loop". I think a more correct statement is that "for many RTOS, a task must not return". The reason for this is that the RTOS scheduler, which calls the task initially, is not designed to handle a return from the task. If a task did return then the RTOS scheduler may assert an error.

I can guess at a few reasons why many RTOS schedulers don't handle returns from tasks. First, an infinite loop is the most typical type of task that is implemented in embedded systems. A task that ends is much less common. Second, the RTOS scheduler may have to be more complex in order to handle returning tasks. Third, the RTOS designer may not have wanted to assume what should be done when a task returns and instead they want the task designer to call an appropriate task termination routine explicitly.

An infinite loop is not the only solution for many RTOS that don't allow tasks to return. The RTOS may provide a task termination routine that deletes the task from the task list so that it is never scheduled again. If a task calls the task termination routine then the task doesn't have to be an infinite loop and it also won't return to it's caller. (i.e., The RTOS task termination routine does not return so the task that calls it also does not return.) For example, FreeRTOS has vTaskDelete() and uC/OS-II has OSTaskDel() for deleting tasks that are not infinite loops.

An infinite loop is a common type of task in embedded systems because many embedded systems just do the same thing over and over again. Many embedded systems are unlike a PC in that they don't have a user interacting with them, starting and terminating various tasks or applications.

like image 31
kkrambo Avatar answered Sep 21 '22 12:09

kkrambo