Professional Documents
Culture Documents
Task Operations
Task Operations
Task Operations
In RTOS implementation of a design, the program is divided into different independent functions
what we call as a task. These functions are not called anywhere in the program, they are just
created. Each tasks runs continuously i.e. in infinite loop.
The kernel is responsible for the management of all the tasks. Check this tutorial to know
working of kernel and task scheduling.
In the FreeRTOS a task can be in either of four different states viz., Running, Ready, Blocked
and Suspended.
Running
The task which is executing currently is said to be in running state. It owns the CPU.
Ready
The task which is neither suspended nor blocked but still not executing will be ready
state. Its not in running state because either a high priority or equal priority task is
executing.
Blocked
A task will go in blocked state whenever its waiting for a event to happen. The event can
be completing a delay period or availability of a resource. The blocked tasks are not
available for scheduling.
Suspended
When vTaskSuspend() is called, the task goes in suspended state. To resume it,
xTaskResume() is called. The suspended tasks are also not available for scheduling.
It seems that the tasks in both Blocked and Suspended are in waiting state. But the
difference is, a suspended task can't come in Ready state unless xTaskResume() is called.
Time out period can be specified only for Blocked tasks but not for suspended tasks.
Deciding Priority of a task
One general rule to decide the task priority is RMS ( Rate Monotonic Scheduling ). According to
this rule, the most frequently executed task is given highest priority.
In FreeRTOS, higher is the priority number, higher is the task priority. FreeRTOS also always
you to have multiple tasks with same priority. In that case it executes the task with same priority
in time slice manner.
API Details
Here we will discuss some of the mostly frequently used APIs related to task.
{{#Widget:LibTable}}
portBASE_TYPE xTaskCreate (
pdTASK_CODE pvTaskCode,
const portCHAR * const pcName,
Defination unsigned portSHORT usStackDepth,
void *pvParameters,
unsigned portBASE_TYPE uxPriority,
xTaskHandle *pvCreatedTask);
1. pdTASK_CODE : Pointer to the task entry function
1. xTaskHandle* : Used to pass back a handle using which the task can be
referenced
Returns TRUE on successful creation of task and adding it to ready list FALSE
Return Value
otherwise.
Description It creates a task and adds it to ready list.
Usage xTaskHandle TaskHandle_1;
static void MyTask1(void* pvParameters);
{{#Widget:LibTable}}
Defination void vTaskDelay( portTickType xTicksToDelay )
Input
portTickType : Number of ticks for which task is to be delayed.
Arguments
Return Value None
It delays the calling task for specified number of ticks.
{{#Widget:LibTable}}
Defination void vTaskDelete( xTaskHandle pxTask )
xTaskHandle : handle to the task to be deleted. Pass NULL to suspend calling
Input Arguments
task
Return Value None
It removes the specified task from the RTOS real time kernels management.
Description But it does not frees the memory allocated by the task code.
{{#Widget:LibTable}}
Defination void vTaskSuspend( xTaskHandle pxTaskToSuspend );
Input xTaskHandle : handle to the task to be suspended. Pass NULL to suspend
Arguments calling task
Return Value None
It suspends the specified task
Description
Set INCLUDE_vTaskSuspend to use this API.
Usage vTaskSuspend(TaskHandle_1);
{{#Widget:LibTable}}
Defination void vTaskResume( xTaskHandle pxTaskToSuspend );
Input xTaskHandle : handle to the task to be resumed. Pass NULL to suspend the
Arguments calling task.
Return Value None
It resumes the specified task
Description
Set INCLUDE_vTaskSuspend to use this API.
Usage vTaskResume(TaskHandle_1);
{{#Widget:LibTable}}
Defination void vTaskSuspendAll(void);
Input
None
Arguments
Return Value None
It suspends all real time kernel activity while keeping interrupts (including the
kernel tick) enabled.
Description
After calling vTaskSuspendAll () the calling task will continue to execute until
xTaskResumeAll () is called.
Usage vTaskSuspendAll();
{{#Widget:LibTable}}
Defination portBASE_TYPE xTaskResumeAll( void );
Input Arguments None
Returns TRUE if resuming the scheduler caused a context switch, FALSE
Return Value
otherwise
Description Resumes real time kernel activity following a call to vTaskSuspendAll ()
vTaskSuspendAll();
Usage .
xTaskResumeAll();
Get Task Priority
{{#Widget:LibTable}}
unsigned portBASE_TYPE uxTaskPriorityGet ( xTaskHandle
Defination
pxTaskToSuspend );
Input xTaskHandle : handle to the task to be queried. Pass NULL to query the calling
Arguments task.
Return Value unsigned portBASE_TYPE : the priority of the specified task.
It is used to get the priority of a task.
Description
Set the constant INCLUDE_vTaskPriorityGet to 1.
unsigned int task_prio;
Usage
task_prio = xTaskPriorityGet (TaskHandle_1);
{{#Widget:LibTable}}
void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE
Defination
uxNewPriority )
1. xTaskHandle : Handle to the task of which the priority is being set. Pass
Input NULL to set the priority of calling task
Arguments 2. unsigned portBASE_TYPE : priority to be set
Precedence Constraint of Jobs: Jobs in a task are independent if they can be executed in any
order. If there is a specific order in which jobs in a task have to be executed then jobs are said to
have precedence constraints. For representing precedence constraints of jobs a partial order
relation < is used. This is called precedence relation. A job J i is a predecessor of job Jj if Ji < Jj
i.e. Jj cannot begin its execution until Ji completes. Ji is an immediate predecessor of Jj if Ji < Jj
and there is no other job J k such that Ji < Jk < Jj. Ji and Jj are independent if neither Ji < Jj nor Jj <
Ji is true.
An efficient way to represent precedence constraints is by using a directed graph G = (J, <)
where J is the set of jobs. This graph is known as the precedence graph. Jobs are represented by
vertices of the graph and precedence constraints are represented using directed edges. If there is a
directed edge from Ji to Jj then it means that Ji is the immediate predecessor of Jj. For example:
Consider a task T having 5 jobs J 1, J2, J3, J4, and J5 such that J2 and J5 cannot begin their
execution until J1 completes and there are no other constraints.
1. < (1) = { }
2. < (2) = {1}
3. < (3) = { }
4. < (4) = { }
5. < (5) = {1}
Consider another example where a precedence graph is given and you have to find precedence
constraints