Consider a FreeRTOS task that is created and immediately started. The task then calls vTaskDelay() for 100 ticks. What will be the task state right after vTaskDelay() is called?
TaskHandle_t xTask;
void vTaskFunction(void *pvParameters) {
// Task runs
vTaskDelay(100);
// Task resumes here
for(;;) {}
}
int main() {
xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, &xTask);
vTaskStartScheduler();
// After scheduler starts, what is the state of xTask immediately after vTaskDelay call?
}Think about what vTaskDelay() does to the task state.
When a task calls vTaskDelay(), it enters the Blocked state for the specified number of ticks. It is not Running or Ready during this delay period.
In FreeRTOS, which task state means the task is ready and waiting for CPU time?
Think about which state means the task can run but is waiting for the CPU.
The Ready state means the task is prepared to run and is waiting for the scheduler to assign CPU time.
Given a task that is suspended using vTaskSuspend() and later resumed with vTaskResume(), what is the task state immediately after resuming?
TaskHandle_t xTask;
void vTaskFunction(void *pvParameters) {
for(;;) {
// Task code
}
}
int main() {
xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, &xTask);
vTaskStartScheduler();
vTaskSuspend(xTask);
// Task is suspended here
vTaskResume(xTask);
// What is the state now?
}Resuming a suspended task makes it available to run again.
When a suspended task is resumed, it moves to the Ready state, waiting for the scheduler to run it.
Examine the code below. The task is created but never runs. What is the reason?
TaskHandle_t xTask;
void vTaskFunction(void *pvParameters) {
for(;;) {
// Task code
}
}
int main() {
xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, &xTask);
// Missing vTaskStartScheduler();
// What is the task state?
}Think about what starts the FreeRTOS scheduler.
Without calling vTaskStartScheduler(), the scheduler never runs, so tasks remain Ready but never execute.
If a task is in the Blocked state waiting for a delay to expire, and then vTaskSuspend() is called on it, what is the task state?
Suspending a task overrides other states.
Calling vTaskSuspend() forces the task into the Suspended state immediately, regardless of whether it was Blocked.