provide a consistent foundation for developing application code.
· Kernels are the core module of every RTOS and typically contain kernel objects, services, and scheduler.
· Kernels can deploy different algorithms for task scheduling. The most common two algorithms are preemptive priority-based scheduling and round-robin scheduling.
· RTOSes for real-time embedded systems should be reliable, predictable, high performance, compact, and scalable.
Chapter 5: Tasks
5.1 Introduction
Simple software applications are typically designed to run sequentially, one instruction at a time, in a pre- determined chain of instructions. However, this scheme is inappropriate for real-time embedded applications, which generally handle multiple inputs and outputs within tight time constraints. Real-time embedded software applications must be designed for concurrency.
This chapter discusses the following topics:
· task definition,
· task states and scheduling,
· typical task operations,
· typical task structure, and
· task coordination and concurrency.
5.2 Defining a Task
A
A task is
Figure 5.1: A task, its associated parameters, and supporting data structures.
When the kernel first starts, it creates its own set of
· initialization or startup task - initializes the system and creates and starts system tasks,
· idle task - uses up processor idle cycles when no other activity is present,
· logging task - logs system messages,
· exception-handling task - handles exceptions, and
· debug agent task - allows debugging with a host debugger. Note that other system tasks might be created during initialization, depending on what other components are included with the kernel.
The idle task, which is created at kernel startup, is one system task that bears mention and should not be ignored. The idle task is set to the lowest priority, typically executes in an endless loop, and runs when either no other task can run or when no other tasks exist, for the sole purpose of using idle processor cycles. The idle task is necessary because the processor executes the instruction to which the program counter register points while it is running. Unless the processor can be suspended, the program counter must still point to valid instructions even when no tasks exist in the system or when no tasks can run. Therefore, the idle task ensures the processor program counter is always valid when no other tasks are running.
In some cases, however, the kernel might allow a user-configured routine to run instead of the idle task in order to implement special requirements for a particular application. One example of a special requirement is power conservation. When no other tasks can run, the kernel can switch control to the user-supplied routine instead of to the idle task. In this case, the user-supplied routine acts like the idle task but instead initiates power conservation code, such as system suspension, after a period of idle time.
After the kernel has initialized and created all of the required tasks, the kernel jumps to a predefined entry point (such as a predefined function) that serves, in effect, as the beginning of the application. From the entry point, the developer can initialize and create other application tasks, as well as other kernel objects, which the application design might require.
As the developer creates new tasks, the developer must assign each a task name, priority, stack size, and a task routine. The kernel does the rest by assigning each task a unique ID and creating an associated TCB and stack space in memory for it.
5.3 Task States and Scheduling
Whether it's a system task or an application task, at any time each task exists in one of a small number of states, including ready, running, or blocked. As the real-time embedded system runs, each task moves from one state to another, according to the logic of a simple finite state machine (FSM). Figure 5.2 illustrates a typical FSM for task execution states, with brief descriptions of state transitions.
Figure 5.2: A typical finite state machine for task execution states.
Although kernels can define task-state groupings differently, generally three main states are used in most typical preemptive-scheduling kernels, including:
· ready state - the task is ready to run but cannot because a higher priority task is executing.
· blocked state - the task has requested a resource that is not available, has requested to wait until some event occurs, or has delayed itself for some duration.