Reputation: 165
I'm trying to comprehend the workflow of a simple 2 task model on freeRTOS. Adding psuedo code for clarity,
Task_A
void Task_A( void *pvParameters )
{
const char *pcTaskName = "Task_A is running\r\n";
for( ;; )
{
vPrintString( pcTaskName );
/* Delay for a period. */
vTaskDelay( 250 / portTICK_RATE_MS );
}
}
Task_B
void Task_B( void *pvParameters )
{
const char *pcTaskName = "Task_B is running\r\n";
volatile unsigned long ul;
for( ;; )
{
vPrintString( pcTaskName );
/* Delay for a period. */
vTaskDelay( 250 / portTICK_RATE_MS );
}
}
main
int main( void )
{
xTaskCreate( Task_A, "Task 1", 1000, NULL, 1, NULL );
xTaskCreate( Task_B, "Task 2", 1000, NULL, 1, NULL );
/* Start the scheduler so the tasks start executing. */
vTaskStartScheduler();
for( ;; );
}
Assuming both Tasks, say Task_A and Task_B are created inside the main function, a call to the scheduler is given (Succeeding all task creation). How would the call to scheduler be executed if scheduler is not invoked before the creation of tasks ? Or putting simply, while the execution starts from main, what causes the control to come out of Task_A and Task_B so that later on the scheduler is invoked ? Please correct me if my understanding is flawed.
Upvotes: 3
Views: 359
Reputation: 129
Tasks do not begin to execute when they are created but you can dispose of its TaskHandle_t pointer just after their creation no matter if the scheduler is running or not. For this, a "*TaskHandle_t * const variable" has to be passed as 6th parameter of the xTaskCreate function.
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
const char * const pcName,
const uint16_t usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pxCreatedTask )
pxCreatedTask Used to pass back a handle by which the created task can be referenced.
Upvotes: 0
Reputation: 1116
First main starts executing.It gives a call to xTaskCreate which will only create Task 1(in ready state) and return to main which again gives a call to xTaskCreate which only create a Task 2(in ready state) and returns.After execution of vTaskStartScheduler(), scheduler will schedule both the tasks based on the priority (selected Scheduling Algorithm).The highest priority task will first go from ready state to running state and starts executing the task function(TaskA or TaskB) passed as parameter while calling the xTaskCreate.
Upvotes: 1
Reputation: 7057
Tasks do not begin to execute when they are created. Creating a task simply puts in place the data structures and information that the scheduler needs to know about the task. The tasks do not begin to execute until the scheduler runs one of them.
In your example main
is executing. It calls the task creation routine which builds and initializes the task data structure. It does not run the task but instead returns to main
. Then main
calls the task creation routine again and it returns to main
again. Finally main
calls the scheduler and the scheduler chooses the highest priority task that is ready to run and begins executing that task. The scheduler does not return to main
.
Upvotes: 4