Consider the following FreeRTOS code snippet that sends and receives an integer via a queue:
QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int valueToSend = 42;
int valueReceived = 0;
xQueueSend(queue, &valueToSend, 0);
xQueueReceive(queue, &valueReceived, 0);
printf("%d", valueReceived);What will be printed?
QueueHandle_t queue = xQueueCreate(1, sizeof(int)); int valueToSend = 42; int valueReceived = 0; xQueueSend(queue, &valueToSend, 0); xQueueReceive(queue, &valueReceived, 0); printf("%d", valueReceived);
Remember that xQueueSend copies the data into the queue, and xQueueReceive copies it out.
The queue holds one integer. The value 42 is sent and then received immediately, so the received value is 42.
In FreeRTOS, if you call xQueueSend() on a queue that is already full and specify a block time of zero, what will happen?
Think about what zero block time means for waiting.
With zero block time, xQueueSend() does not wait and returns immediately with an error if the queue is full.
Examine this code snippet:
QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int receivedValue;
xQueueReceive(queue, &receivedValue, portMAX_DELAY);
printf("%d", receivedValue);The program blocks forever at xQueueReceive. Why?
QueueHandle_t queue = xQueueCreate(1, sizeof(int)); int receivedValue; xQueueReceive(queue, &receivedValue, portMAX_DELAY); printf("%d", receivedValue);
Think about what portMAX_DELAY means and the queue state.
The receive call waits indefinitely for data, but since no data was sent, it blocks forever.
Given the struct:
typedef struct {
int id;
float value;
} SensorData;Which code snippet correctly sends a SensorData variable to a queue?
typedef struct {
int id;
float value;
} SensorData;
SensorData data = {1, 23.5};
QueueHandle_t queue = xQueueCreate(5, sizeof(SensorData));Remember that xQueueSend expects a pointer to the data to copy.
Option B passes the address of the struct variable, which is correct. Others either pass the struct by value or wrong pointer.
Consider this queue creation:
QueueHandle_t queue = xQueueCreate(3, sizeof(char));
How many characters can this queue hold at once, and why?
QueueHandle_t queue = xQueueCreate(3, sizeof(char));Think about the parameters of xQueueCreate: length and item size.
The first parameter is the number of items the queue can hold, the second is the size of each item. So this queue holds 3 chars.
