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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With