Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Queue blocking behavior (timeout) in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS queue send with timeout?

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?

FreeRTOS
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
ApdPASS (1) immediately because the queue has space for one more item
BerrQUEUE_FULL (0) because the queue is full and timeout does not unblock send
CpdFAIL (0) because the queue is full and the send times out without success
DpdPASS (1) because the queue will accept the item after timeout
Attempts:
2 left
💡 Hint

Think about what happens when the queue is full and the send function waits for a timeout.

❓ Predict Output
intermediate
2:00remaining
What happens when receiving from an empty queue with timeout?

Given a queue created with length 2, what will xQueueReceive return if called on an empty queue with a timeout of 50 ticks?

FreeRTOS
QueueHandle_t queue = xQueueCreate(2, sizeof(int));
int receivedValue;
BaseType_t result = xQueueReceive(queue, &receivedValue, 50);
// Return or print result
ApdFAIL (0) after 50 ticks because no item was received
BpdPASS (1) after 50 ticks with a default value
CpdPASS (1) immediately because the queue is empty
DpdFAIL (0) immediately because the queue is empty
Attempts:
2 left
💡 Hint

Consider what xQueueReceive does when the queue is empty and a timeout is specified.

🧠 Conceptual
advanced
2:00remaining
Which statement about FreeRTOS queue blocking with timeout is true?

Choose the correct statement about how FreeRTOS queues behave when sending or receiving with a timeout.

AA send operation with timeout will always succeed if the timeout is non-zero.
BA receive operation with timeout returns immediately if the queue is empty, ignoring the timeout.
CA receive operation with timeout blocks indefinitely regardless of the timeout value.
DA send operation with timeout blocks until space is available or timeout expires.
Attempts:
2 left
💡 Hint

Think about how blocking with timeout works for queue send and receive.

❓ Predict Output
advanced
2:00remaining
What is the value of variable after this queue receive with zero timeout?

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?

FreeRTOS
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
Areceived = 0, result = pdFAIL (0)
Breceived = 10, result = pdPASS (1)
Creceived = 10, result = pdFAIL (0)
Dreceived = 0, result = pdPASS (1)
Attempts:
2 left
💡 Hint

Think about what happens when the queue has data and receive is called with zero wait.

❓ Predict Output
expert
3:00remaining
What is the output count of items in queue after these operations with timeouts?

Consider this sequence of FreeRTOS queue operations with timeouts. How many items remain in the queue at the end?

FreeRTOS
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
A2 items remain in the queue
B1 item remains in the queue
C3 items remain in the queue
D0 items remain in the queue
Attempts:
2 left
💡 Hint

Trace each send and receive carefully, considering blocking and timeouts.

Practice

(1/5)
1. What does the xTicksToWait parameter control in FreeRTOS queue functions like xQueueReceive() or xQueueSend()?
easy
A. The number of items currently in the queue.
B. The size of the queue in bytes.
C. The maximum time a task will wait for the queue operation before timing out.
D. The priority of the task accessing the queue.

Solution

  1. Step 1: Understand the role of xTicksToWait

    This parameter specifies how long a task should wait if the queue is full (for send) or empty (for receive).
  2. Step 2: Relate to queue blocking behavior

    If the queue operation cannot proceed immediately, the task blocks for up to xTicksToWait ticks before timing out.
  3. Final Answer:

    The maximum time a task will wait for the queue operation before timing out. -> Option C
  4. Quick Check:

    Timeout duration = xTicksToWait [OK]
Hint: Remember: xTicksToWait sets wait time before giving up [OK]
Common Mistakes:
  • Confusing xTicksToWait with queue size
  • Thinking it sets task priority
  • Assuming it counts queue items
2. Which of the following is the correct way to call xQueueReceive() with a timeout of 10 ticks?
easy
A. xQueueReceive(queueHandle, &data);
B. xQueueReceive(queueHandle, data, 10);
C. xQueueReceive(queueHandle, &data, "10");
D. xQueueReceive(queueHandle, &data, 10);

Solution

  1. Step 1: Check function parameters

    xQueueReceive() requires the queue handle, pointer to data buffer, and timeout in ticks as a number.
  2. Step 2: Validate correct syntax

    xQueueReceive(queueHandle, &data, 10); correctly passes the address of data and numeric timeout 10 ticks.
  3. Final Answer:

    xQueueReceive(queueHandle, &data, 10); -> Option D
  4. Quick Check:

    Correct parameters and timeout type = xQueueReceive(queueHandle, &data, 10); [OK]
Hint: Use &data and numeric timeout for xQueueReceive [OK]
Common Mistakes:
  • Passing data instead of &data
  • Using string "10" instead of number 10
  • Omitting timeout parameter
3. Consider this code snippet:
if(xQueueReceive(queue, &item, 5) == pdPASS) {
    process(item);
} else {
    handleTimeout();
}
What happens if the queue is empty and no item arrives within 5 ticks?
medium
A. The function returns after 5 ticks with a failure code, and handleTimeout() is called.
B. The task blocks indefinitely waiting for an item.
C. The function returns immediately with pdPASS.
D. The queue is deleted automatically.

Solution

  1. Step 1: Analyze xQueueReceive behavior with timeout

    If the queue is empty, the task waits up to 5 ticks for an item to arrive.
  2. Step 2: Understand return values

    If no item arrives in 5 ticks, xQueueReceive() returns a failure code (not pdPASS), so handleTimeout() runs.
  3. Final Answer:

    The function returns after 5 ticks with a failure code, and handleTimeout() is called. -> Option A
  4. Quick Check:

    Timeout triggers failure return = The function returns after 5 ticks with a failure code, and handleTimeout() is called. [OK]
Hint: Timeout means failure return, so else branch runs [OK]
Common Mistakes:
  • Assuming indefinite block without timeout
  • Thinking pdPASS is returned on timeout
  • Believing queue deletes itself
4. Which of the following correctly describes this code snippet:
BaseType_t result = xQueueSend(queue, &data, 0);
if(result == pdFAIL) {
    // handle failure
}
medium
A. Using 0 as timeout means the task will wait forever.
B. Using 0 as timeout means the function returns immediately if the queue is full.
C. The timeout parameter should be a pointer, not a number.
D. The return value should be compared to pdPASS, not pdFAIL.

Solution

  1. Step 1: Understand timeout=0 behavior in xQueueSend

    A timeout of 0 means the function does not block; it returns immediately if the queue is full.
  2. Step 2: Check the error handling logic

    If the queue is full, xQueueSend() returns pdFAIL immediately, so the failure handling code runs correctly.
  3. Final Answer:

    Using 0 as timeout means the function returns immediately if the queue is full. -> Option B
  4. Quick Check:

    Timeout 0 means no wait, immediate return = Using 0 as timeout means the function returns immediately if the queue is full. [OK]
Hint: Timeout 0 means no wait, immediate fail if full [OK]
Common Mistakes:
  • Thinking 0 means wait forever
  • Confusing timeout type with pointer
  • Comparing return to wrong constant
5. You want a task to send data to a queue but only wait up to 20 ticks if the queue is full. If the queue remains full after 20 ticks, the task should perform an alternative action. Which code snippet correctly implements this behavior?
hard
A. if(xQueueSend(queue, &data, 20) == pdPASS) { // sent successfully } else { // alternative action }
B. if(xQueueSend(queue, &data, 0) == pdFAIL) { // alternative action } else { // sent successfully }
C. if(xQueueSend(queue, &data, portMAX_DELAY) == pdPASS) { // sent successfully } else { // alternative action }
D. if(xQueueSend(queue, &data, 20) == pdFAIL) { // sent successfully } else { // alternative action }

Solution

  1. Step 1: Understand desired behavior

    The task should wait up to 20 ticks to send data, then do alternative action if it fails.
  2. Step 2: Analyze each option

    if(xQueueSend(queue, &data, 20) == pdPASS) { // sent successfully } else { // alternative action } waits 20 ticks and checks for pdPASS to confirm success; else runs alternative action. if(xQueueSend(queue, &data, 0) == pdFAIL) { // alternative action } else { // sent successfully } waits 0 ticks (no wait). if(xQueueSend(queue, &data, portMAX_DELAY) == pdPASS) { // sent successfully } else { // alternative action } waits forever. if(xQueueSend(queue, &data, 20) == pdFAIL) { // sent successfully } else { // alternative action } reverses success/failure logic.
  3. Final Answer:

    if(xQueueSend(queue, &data, 20) == pdPASS) { // sent successfully } else { // alternative action } -> Option A
  4. Quick Check:

    Wait 20 ticks, success check = if(xQueueSend(queue, &data, 20) == pdPASS) { // sent successfully } else { // alternative action } [OK]
Hint: Wait 20 ticks, check for pdPASS to confirm send success [OK]
Common Mistakes:
  • Using 0 ticks instead of 20
  • Waiting forever with portMAX_DELAY
  • Mixing up pdPASS and pdFAIL checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard