Consider a FreeRTOS task that manages access to a shared resource using a queue. What will be printed when the task receives a request?
#include "FreeRTOS.h" #include "task.h" #include "queue.h" #include <stdio.h> QueueHandle_t resourceQueue; void ResourceManagerTask(void *pvParameters) { char *request; while(1) { if(xQueueReceive(resourceQueue, &request, portMAX_DELAY) == pdPASS) { printf("Processing request: %s\n", request); vTaskDelay(pdMS_TO_TICKS(100)); } } } int main() { resourceQueue = xQueueCreate(5, sizeof(char*)); xTaskCreate(ResourceManagerTask, "ResMgr", 1000, NULL, 1, NULL); char *req = "Task1 Request"; xQueueSend(resourceQueue, &req, 0); vTaskStartScheduler(); return 0; }
Check how the queue is created and how the request is sent and received.
The queue is created properly before the task starts. The request string pointer is sent to the queue and received correctly, so the task prints the request string.
In a FreeRTOS resource manager task pattern, which synchronization method ensures exclusive access to a shared resource?
Think about priority inheritance and exclusive access.
A mutex is designed to provide exclusive access and supports priority inheritance, preventing priority inversion in FreeRTOS.
Analyze the code below. What error will occur when running this resource manager task?
QueueHandle_t resourceQueue;
void ResourceManagerTask(void *pvParameters) {
char request[20];
while(1) {
if(xQueueReceive(resourceQueue, &request, portMAX_DELAY) == pdPASS) {
printf("Request: %s\n", request);
}
}
}
int main() {
resourceQueue = xQueueCreate(5, sizeof(char*));
xTaskCreate(ResourceManagerTask, "ResMgr", 1000, NULL, 1, NULL);
char req[] = "Request1";
xQueueSend(resourceQueue, &req, 0);
vTaskStartScheduler();
return 0;
}Check the queue item size and what is sent/received.
The queue is created to hold pointers (char*), but the task receives into a char array. This mismatch causes the task to print garbage because the pointer is interpreted as data.
Identify the correct fix for the syntax error in the following snippet:
if xQueueReceive(resourceQueue, &request, portMAX_DELAY) == pdPASS {
processRequest(request);
}Remember C syntax for if statements.
In C, the condition must be inside parentheses and the block inside braces without 'then'.
Given the queue creation below, how many items can it hold?
resourceQueue = xQueueCreate(10, sizeof(int));
Check the parameters of xQueueCreate.
The first parameter is the number of items, the second is the size of each item. So the queue holds 10 items of int size.