In FreeRTOS, tasks are fundamental. Which statement best explains why tasks are called the building blocks?
Think about how FreeRTOS manages different jobs at the same time.
Tasks in FreeRTOS are like small programs that run independently. They let the system do many things at once by sharing the CPU. This is why they are called building blocks.
Consider the following FreeRTOS code snippet. What will be the output on the console?
void vTaskCode( void * pvParameters ) {
for( ;; ) {
printf("Task running\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
int main(void) {
xTaskCreate(vTaskCode, "Task1", 1000, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}Remember that vTaskStartScheduler starts the tasks created.
The task prints "Task running" every second because vTaskDelay pauses the task for 1000 milliseconds, then it repeats forever.
Look at the code below. What error will occur when running this code?
void vTaskCode( void * pvParameters ) {
for( ;; ) {
// Task code here
}
}
int main(void) {
BaseType_t result = xTaskCreate(vTaskCode, "Task1", 1000, NULL, 1, NULL);
if (result != pdPASS) {
printf("Task creation failed\n");
}
// Missing vTaskStartScheduler call
return 0;
}Think about what starts the FreeRTOS scheduler.
Without calling vTaskStartScheduler, the created tasks do not start running. The program ends immediately.
Choose the code snippet that correctly creates a FreeRTOS task named "Task1" with priority 2.
Check the order and types of parameters for xTaskCreate.
The correct order is: function pointer, task name string, stack size, parameters, priority (number), task handle pointer.
Given the code below, how many tasks will be running after vTaskStartScheduler() is called?
void vTask1(void *pvParameters) { for(;;) {} }
void vTask2(void *pvParameters) { for(;;) {} }
int main(void) {
xTaskCreate(vTask1, "Task1", 1000, NULL, 1, NULL);
xTaskCreate(vTask2, "Task2", 1000, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}Don't forget the idle task created automatically by the scheduler.
Two user tasks are created, and FreeRTOS automatically creates an idle task when the scheduler starts, for a total of 3 tasks running.