described earlier.
However, if the unblocked task is the highest priority task, the task moves directly to the running state (without going through the ready state) and preempts the currently running task. The preempted task is then moved to the ready state and put into the appropriate priority-based location in the task-ready list.
5.4 Typical Task Operations
In addition to providing a task object, kernels also provide
A kernel, however, also provides an API that allows developers to manipulate tasks. Some of the more common operations that developers can perform with a task object from within the application include:
· creating and deleting tasks,
· controlling task scheduling, and
· obtaining task information.
Developers should learn how to perform each of these operations for the kernel selected for the project. Each operation is briefly discussed next.
5.4.1 Task Creation and Deletion
The most fundamental operations that developers must learn are creating and deleting tasks, as shown in Table 5.1.
Table 5.1: Operations for task creation and deletion.
Operation | Description |
---|---|
Create | Creates a task |
Delete | Deletes a task |
Developers typically create a task using one or two operations, depending on the kernel’s API. Some kernels allow developers first to create a task and then start it. In this case, the task is first created and put into a suspended state; then, the task is moved to the ready state when it is started (made ready to run).
Creating tasks in this manner might be useful for debugging or when special initialization needs to occur between the times that a task is created and started. However, in most cases, it is sufficient to create and start a task using one kernel call.
The suspended state is similar to the blocked state, in that the suspended task is neither running nor ready to run. However, a task does not move into or out of the suspended state via the same operations that move a task to or from the blocked state. The exact nature of the suspended state varies between RTOSes. For the present purpose, it is sufficient to know that the task is not yet ready to run.
Starting a task does not make it run immediately; it puts the task on the task-ready list.
Many kernels also provide
· when a task is first created,
· when a task is suspended for any reason and a context switch occurs, and
· when a task is deleted.
Hooks are useful when executing special initialization code upon task creation, implementing status tracking or monitoring upon task context switches, or executing clean-up code upon task deletion.
Carefully consider how tasks are to be deleted in the embedded application. Many kernel implementations allow any task to delete any other task. During the deletion process, a kernel terminates the task and frees memory by deleting the task’s TCB and stack.
However, when tasks execute, they can acquire memory or access resources using other kernel objects. If the task is deleted incorrectly, the task might not get to release these resources. For example, assume that a task acquires a semaphore token to get exclusive access to a shared data structure. While the task is operating on this data structure, the task gets deleted. If not handled appropriately, this abrupt deletion of the operating task can result in:
· a corrupt data structure, due to an incomplete write operation,
· an unreleased semaphore, which will not be available for other tasks that might need to acquire it, and
· an inaccessible data structure, due to the unreleased semaphore.
As a result, premature deletion of a task can result in memory or resource leaks.
A
This book discusses these concepts in more detail later. At this point, however, note that any tasks to be deleted must have enough time to clean up and release resources or memory before being deleted.
5.4.2 Task Scheduling
From the time a task is created to the time it is deleted, the task can move through various states resulting from program execution and kernel scheduling. Although much of this state changing is automatic, many kernels provide a set of API calls that allow developers to control when a task moves to a different state, as shown in Table 5.2. This capability is called
Table 5.2: Operations for task scheduling.