What is Task Execution?
A task is a unit of execution or unit of work in a software application. Typically, task execution in an embedded processor is managed by the operating system (OS). When deployed to the embedded processor, a task corresponds to an OS thread. The SoC Blockset™ defines the execution life cycle and relation to OS threads as follows.
Task Execution Life Cycle
The life cycle of a task can be divided into five states:
Created – The system creates all the tasks when the application starts and immediately moves them to the waiting state.
Waiting – The task waits for the associated trigger signal, such as an OS timer or I/O device. After receiving the trigger signal, the task starts to run. If the task has the highest priority, it enters the running state. Otherwise, the task continues to wait until it becomes the highest priority, triggered task.
Running – The task executes its code. When the code completes execution, the task immediately moves to the waiting state. If a trigger for a higher-priority task occurs, the running task moves to the preempted state.
Preempted – The task is preempted and waiting to run. A task runs based on a combination of the task priority and the order the task entered the Preempted state. Assuming equal task priorities of all other tasks in Ready to Resume state, tasks run based on first-in-first-out (FIFO) ordering.
Terminated – Tasks terminate when the application ends.
This figure shows the state diagram of a task execution life cycle for an application using an OS. For simplicity, the terminated state is not shown, but a task can reach the terminated state from any of the other states.
Task and Thread
A task is a conceptual unit of work in an algorithm. In an application executing on a device, a task is a section of code that executes in a thread within an operating system (OS). The OS thread determines the state of execution of the task. Within the SoC Blockset, a task specifically refers to the portion of the Simulink® model contained within a rate or function-call subsystem. The trigger signal for that subsystem comes from a Task Manager block. When deployed to hardware, an OS thread uses the task properties. The thread executes the code generated from the subsystem. Conceptually, a Task in simulation is equivalent to a thread in generated code.