Bird
Raised Fist0
FreeRTOSprogramming~5 mins

Why queues are needed for inter-task communication in FreeRTOS

Choose your learning style10 modes available
Introduction

Queues help tasks talk to each other safely and in order. They make sure data moves between tasks without getting lost or mixed up.

When one task produces data and another task needs to use it later.
When tasks run at different speeds but need to share information.
When you want to avoid tasks interfering with each other's data.
When you want to keep the order of messages or data sent between tasks.
When tasks need to wait for data from other tasks before continuing.
Syntax
FreeRTOS
/* Queue handle declaration */
QueueHandle_t xQueue;

/* Create a queue to hold 10 integers */
xQueue = xQueueCreate(10, sizeof(int));

/* Send data to the queue */
int dataToSend = 100;
xQueueSend(xQueue, &dataToSend, portMAX_DELAY);

/* Receive data from the queue */
int receivedData;
xQueueReceive(xQueue, &receivedData, portMAX_DELAY);

Queues hold copies of data, so tasks do not share pointers directly.

Use portMAX_DELAY to wait indefinitely for send or receive.

Examples
Shows what happens if you try to receive from an empty queue without waiting.
FreeRTOS
/* Example: Empty queue */
QueueHandle_t xQueue = xQueueCreate(5, sizeof(int));
int receivedValue;

// Trying to receive from empty queue will block or fail
if(xQueueReceive(xQueue, &receivedValue, 0) == pdPASS) {
    // Got data
} else {
    // No data available
}
Shows sending and receiving a single item in the queue.
FreeRTOS
/* Example: Queue with one element */
QueueHandle_t xQueue = xQueueCreate(5, sizeof(int));
int dataToSend = 42;
xQueueSend(xQueue, &dataToSend, 0);

int receivedValue;
xQueueReceive(xQueue, &receivedValue, 0); // Receives 42
Shows what happens if you try to send data when the queue is full.
FreeRTOS
/* Example: Queue full scenario */
QueueHandle_t xQueue = xQueueCreate(2, sizeof(int));
int data1 = 1, data2 = 2, data3 = 3;
xQueueSend(xQueue, &data1, 0);
xQueueSend(xQueue, &data2, 0);
// Queue is full now
BaseType_t result = xQueueSend(xQueue, &data3, 0); // Fails because queue is full
Sample Program

This program creates a queue and two tasks. The sender task puts a number into the queue. The receiver task waits and gets the number from the queue. It prints messages before and after to show the data flow.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include <stdio.h>

QueueHandle_t xQueue;

void vSenderTask(void *pvParameters) {
    int dataToSend = 10;
    printf("Sender: Sending %d to queue\n", dataToSend);
    xQueueSend(xQueue, &dataToSend, portMAX_DELAY);
    vTaskDelete(NULL);
}

void vReceiverTask(void *pvParameters) {
    int receivedData = 0;
    if(xQueueReceive(xQueue, &receivedData, portMAX_DELAY) == pdPASS) {
        printf("Receiver: Received %d from queue\n", receivedData);
    } else {
        printf("Receiver: Failed to receive data\n");
    }
    vTaskDelete(NULL);
}

int main(void) {
    xQueue = xQueueCreate(5, sizeof(int));
    if(xQueue == NULL) {
        printf("Failed to create queue\n");
        return 1;
    }

    xTaskCreate(vSenderTask, "Sender", 1000, NULL, 1, NULL);
    xTaskCreate(vReceiverTask, "Receiver", 1000, NULL, 1, NULL);

    vTaskStartScheduler();

    // Should never reach here
    return 0;
}
OutputSuccess
Important Notes

Time complexity of sending or receiving from a queue is generally O(1).

Queues use extra memory to store copies of data, so consider space when choosing queue size.

Common mistake: Trying to share pointers instead of data copies can cause errors.

Use queues when you want safe, ordered communication between tasks. Use other methods like direct task notifications for simpler signals.

Summary

Queues let tasks send and receive data safely without conflicts.

They keep data in order and handle different task speeds.

Queues are essential for clear and reliable communication between tasks in FreeRTOS.

Practice

(1/5)
1. Why are queues needed for inter-task communication in FreeRTOS?
easy
A. To create multiple copies of the same task
B. To speed up the execution of a single task
C. To store data permanently on disk
D. To safely send and receive data between tasks without conflicts

Solution

  1. Step 1: Understand the role of queues in task communication

    Queues allow tasks to exchange data safely without overwriting or losing information.
  2. Step 2: Identify the main purpose of queues in FreeRTOS

    They prevent conflicts by managing data flow between tasks running at different speeds.
  3. Final Answer:

    To safely send and receive data between tasks without conflicts -> Option D
  4. Quick Check:

    Queues ensure safe data exchange = A [OK]
Hint: Queues prevent data conflicts between tasks [OK]
Common Mistakes:
  • Thinking queues speed up single tasks
  • Confusing queues with permanent storage
  • Believing queues duplicate tasks
2. Which of the following is the correct way to create a queue in FreeRTOS?
easy
A. xQueueCreate(10, sizeof(int));
B. xQueueStart(10, sizeof(int));
C. createQueue(10, sizeof(int));
D. QueueCreate(10, sizeof(int));

Solution

  1. Step 1: Recall the FreeRTOS queue creation function

    The correct function to create a queue is xQueueCreate.
  2. Step 2: Verify the syntax of the function call

    The function takes two parameters: the number of items and the size of each item.
  3. Final Answer:

    xQueueCreate(10, sizeof(int)); -> Option A
  4. Quick Check:

    Queue creation uses xQueueCreate() = A [OK]
Hint: Remember FreeRTOS queue creation is xQueueCreate() [OK]
Common Mistakes:
  • Using incorrect function names like xQueueStart
  • Mixing up function name capitalization
  • Omitting parameters or using wrong ones
3. Given the following code snippet, what will be the output?
QueueHandle_t queue = xQueueCreate(2, sizeof(int));
int value = 5;
xQueueSend(queue, &value, 0);
value = 10;
xQueueSend(queue, &value, 0);
int received;
xQueueReceive(queue, &received, 0);
printf("%d", received);
medium
A. 10
B. 5
C. 0
D. Compilation error

Solution

  1. Step 1: Understand queue send order

    The first value sent is 5, then 10. Queues keep order FIFO (first in, first out).
  2. Step 2: Check which value is received first

    The first received value is 5, the first sent value.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Queue FIFO order means first sent is first received = C [OK]
Hint: Queues deliver data in FIFO order [OK]
Common Mistakes:
  • Assuming last sent value is received first
  • Confusing queue with stack behavior
  • Expecting zero instead of actual data
4. What is wrong with this code snippet for sending data to a queue?
QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int value = 42;
xQueueSend(queue, &value, 0);
xQueueSend(queue, &value, 0);
medium
A. Queue must be created with zero size
B. The data type int is not allowed in queues
C. The queue size is too small for two sends without receiving
D. xQueueSend requires a pointer to a pointer

Solution

  1. Step 1: Analyze queue size and send operations

    The queue can hold only 1 item, but two sends are done without receiving.
  2. Step 2: Identify the problem caused by queue overflow

    The second send will fail or block because the queue is full.
  3. Final Answer:

    The queue size is too small for two sends without receiving -> Option C
  4. Quick Check:

    Queue size limits sends = D [OK]
Hint: Queue size limits how many items can be sent before receive [OK]
Common Mistakes:
  • Thinking int is invalid data type for queues
  • Misunderstanding xQueueSend parameters
  • Believing queue size can be zero
5. You have two tasks: Task A produces sensor data at irregular intervals, and Task B processes this data. Why is using a queue the best method for communication between these tasks?
hard
A. Because queues buffer data safely and handle different task speeds
B. Because queues force both tasks to run at the same speed
C. Because queues automatically delete old data when full
D. Because queues allow tasks to share variables directly without copying

Solution

  1. Step 1: Understand task speed differences

    Task A produces data irregularly, Task B processes it at its own pace.
  2. Step 2: Identify queue benefits for this scenario

    Queues buffer data safely and keep order, allowing tasks to work independently without data loss.
  3. Step 3: Eliminate incorrect options

    Queues do not force speed matching, do not delete data automatically, and do not share variables directly.
  4. Final Answer:

    Because queues buffer data safely and handle different task speeds -> Option A
  5. Quick Check:

    Queues buffer and sync tasks = B [OK]
Hint: Queues handle different task speeds by buffering data [OK]
Common Mistakes:
  • Thinking queues force tasks to run at same speed
  • Believing queues delete old data automatically
  • Confusing queues with shared variable access
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