Consider two tasks with different priorities. What will be printed when both tasks run?
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void Task1(void *pvParameters) { printf("Task1 start\n"); vTaskDelay(pdMS_TO_TICKS(100)); printf("Task1 end\n"); vTaskDelete(NULL); } void Task2(void *pvParameters) { printf("Task2 start\n"); vTaskDelay(pdMS_TO_TICKS(50)); printf("Task2 end\n"); vTaskDelete(NULL); } int main() { xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL); xTaskCreate(Task2, "Task2", 1000, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Remember that higher priority tasks preempt lower priority ones, and vTaskDelay causes the task to block for the specified time.
Task1 has higher priority (2) than Task2 (1). Task1 runs first and prints "Task1 start". Then it delays for 100ms, allowing Task2 to run and print its messages. Task2 delays for 50ms, so it finishes before Task1 resumes and prints "Task1 end".
In FreeRTOS, what is the typical cause of a system freeze when tasks never resume?
Think about tasks waiting on each other indefinitely.
Deadlock happens when two or more tasks wait for resources held by each other, causing all to block forever. This freezes the system.
Examine the task code below. What causes the stack overflow?
void TaskExample(void *pvParameters) {
char buffer[1024];
while(1) {
// Do some work
vTaskDelay(pdMS_TO_TICKS(10));
}
}Consider how much memory the stack has and what local variables consume.
The large local array 'buffer' (1024 bytes) consumes a lot of stack memory. If the task stack size is not large enough, this causes stack overflow.
What error occurs when running this code snippet?
QueueHandle_t queue = xQueueCreate(5, sizeof(int)); int value = 10; xQueueSend(queue, &value, 0); xQueueSend(queue, NULL, 0);
Check the parameters passed to xQueueSend carefully.
xQueueSend expects a valid pointer to data. Passing NULL causes undefined behavior and likely runtime error.
Given a FreeRTOS queue created with length 3, what is the number of items in the queue after these operations?
QueueHandle_t q = xQueueCreate(3, sizeof(int)); int a=1,b=2,c=3,d=4; xQueueSend(q, &a, 0); xQueueSend(q, &b, 0); xQueueSend(q, &c, 0); xQueueSend(q, &d, 0);
Remember the fixed size of FreeRTOS queues and behavior when full.
The queue has fixed length 3. The first three sends succeed. The fourth send fails because the queue is full, so only 3 items remain.