Consider the following FreeRTOS code snippet where a task tries to send an item to a full queue with a timeout of 100 ticks. What will be the return value of xQueueSend?
QueueHandle_t queue = xQueueCreate(1, sizeof(int)); int item = 42; // Fill the queue xQueueSend(queue, &item, 0); // Try to send another item with 100 tick timeout BaseType_t result = xQueueSend(queue, &item, 100); // Return or print result
Think about what happens when the queue is full and the send function waits for a timeout.
The queue has length 1 and is already full. xQueueSend will block up to 100 ticks waiting for space. Since no task removes items, the send times out and returns pdFAIL (0).
Given a queue created with length 2, what will xQueueReceive return if called on an empty queue with a timeout of 50 ticks?
QueueHandle_t queue = xQueueCreate(2, sizeof(int)); int receivedValue; BaseType_t result = xQueueReceive(queue, &receivedValue, 50); // Return or print result
Consider what xQueueReceive does when the queue is empty and a timeout is specified.
The queue is empty, so xQueueReceive blocks up to 50 ticks waiting for an item. Since no item arrives, it returns pdFAIL (0) after the timeout.
Choose the correct statement about how FreeRTOS queues behave when sending or receiving with a timeout.
Think about how blocking with timeout works for queue send and receive.
When sending to a full queue, FreeRTOS blocks the task until space is available or the timeout expires. It does not guarantee success just because timeout is non-zero.
Given a queue with one item (value 10), what will be the value of received and the return of xQueueReceive if called with zero timeout?
QueueHandle_t queue = xQueueCreate(1, sizeof(int)); int value = 10; xQueueSend(queue, &value, 0); int received = 0; BaseType_t result = xQueueReceive(queue, &received, 0); // Return or print received and result
Think about what happens when the queue has data and receive is called with zero wait.
The queue has one item, so xQueueReceive returns immediately with the item and pdPASS.
Consider this sequence of FreeRTOS queue operations with timeouts. How many items remain in the queue at the end?
QueueHandle_t queue = xQueueCreate(2, sizeof(int)); int a = 1, b = 2, c = 3; xQueueSend(queue, &a, 0); // send 1 xQueueSend(queue, &b, 0); // send 2 // Try to send 3 with 10 tick timeout xQueueSend(queue, &c, 10); int received; // Receive one item with 5 tick timeout xQueueReceive(queue, &received, 5); // Try to send 3 again with 0 timeout xQueueSend(queue, &c, 0); // Count items in queue UBaseType_t count = uxQueueMessagesWaiting(queue); // Return or print count
Trace each send and receive carefully, considering blocking and timeouts.
Initially, queue has 2 items (1 and 2). The send of 3 blocks for 10 ticks but queue is full, so it times out and fails. Then one item is received (1 removed). Then send 3 with zero timeout succeeds because space is available. So queue has 2 items (2 and 3).
