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
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.
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.
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.
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
...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With