Operation | Description |
---|---|
Suspend | Suspends a task |
Resume | Resumes a task |
Delay | Delays a task |
Restart | Restarts a task |
Get Priority | Gets the current task’s priority |
Set Priority | Dynamically sets a task’s priority |
Preemption lock | Locks out higher priority tasks from preempting the current task |
Preemption unlock | Unlocks a preemption lock |
Using manual scheduling, developers can suspend and resume tasks from within an application. Doing so might be important for debugging purposes or, as discussed earlier, for suspending a high-priority task so that lower priority tasks can execute.
A developer might want to delay (block) a task, for example, to allow manual scheduling or to wait for an external condition that does not have an associated interrupt. Delaying a task causes it to relinquish the CPU and allow another task to execute. After the delay expires, the task is returned to the task-ready list after all other ready tasks at its priority level. A delayed task waiting for an external condition can wake up after a set time to check whether a specified condition or event has occurred, which is called
A developer might also want to restart a task, which is not the same as resuming a suspended task. Restarting a task begins the task as if it had not been previously executing. The internal state the task possessed at the time it was suspended (for example, the CPU registers used and the resources acquired) is lost when a task is restarted. By contrast, resuming a task begins the task in the same internal state it possessed when it was suspended.
Restarting a task is useful during debugging or when reinitializing a task after a catastrophic error. During debugging, a developer can restart a task to step through its code again from start to finish. In the case of catastrophic error, the developer can restart a task and ensure that the system continues to operate without having to be completely reinitialized.
Getting and setting a task’s priority during execution lets developers control task scheduling manually. This process is helpful during a
Finally, the kernel might support
5.4.3 Obtaining Task Information
Kernels provide routines that allow developers to access task information within their applications, as shown in Table 5.3. This information is useful for debugging and monitoring.
Table 5.3: Task-information operations.
Operation | Description |
---|---|
Get ID | Get the current task’s ID |
Get TCB | Get the current task’s TCB |
One use is to obtain a particular task’s ID, which is used to get more information about the task by getting its TCB. Obtaining a TCB, however, only takes a snapshot of the task context. If a task is not dormant (e.g., suspended), its context might be dynamic, and the snapshot information might change by the time it is used. Hence, use this functionality wisely, so that decisions aren’t made in the application based on querying a constantly changing task context.
5.5 Typical Task Structure
When writing code for tasks, tasks are structured in one of two ways:
· run to completion, or