Why queues are needed for inter-task communication
📖 Scenario: Imagine you have two tasks in a FreeRTOS system: one task reads sensor data, and another task processes that data. They need a way to share information safely and in order.
🎯 Goal: Build a simple FreeRTOS program that uses a queue to send data from one task to another, showing why queues are needed for safe communication between tasks.
📋 What You'll Learn
Create a queue to hold integer data
Create a sender task that sends numbers to the queue
Create a receiver task that receives numbers from the queue
Print the received numbers to show communication
💡 Why This Matters
🌍 Real World
In embedded systems, tasks often need to share sensor readings or commands safely. Queues help keep data organized and prevent errors.
💼 Career
Understanding queues is essential for embedded software developers working with real-time operating systems like FreeRTOS to build reliable multitasking applications.
Progress0 / 4 steps
1
Create a queue to hold integer data
Write code to create a FreeRTOS queue called dataQueue that can hold 5 integers.
FreeRTOS
Hint
Use xQueueCreate with the number of items and size of each item.
2
Create a sender task that sends numbers to the queue
Write a FreeRTOS task function called SenderTask that sends the integer 100 to dataQueue using xQueueSend. Use portMAX_DELAY to wait if the queue is full.
FreeRTOS
Hint
Use xQueueSend with the queue handle, address of the value, and wait time.
3
Create a receiver task that receives numbers from the queue
Write a FreeRTOS task function called ReceiverTask that receives an integer from dataQueue using xQueueReceive and stores it in a variable called receivedValue. Use portMAX_DELAY to wait if the queue is empty.
FreeRTOS
Hint
Use xQueueReceive with the queue handle, address of the variable, and wait time.
4
Print the received numbers to show communication
Add a printf statement inside ReceiverTask to print the text "Received: " followed by the receivedValue.
FreeRTOS
Hint
Use printf("Received: %d\n", receivedValue); to show the value.
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
Step 1: Understand the role of queues in task communication
Queues allow tasks to exchange data safely without overwriting or losing information.
Step 2: Identify the main purpose of queues in FreeRTOS
They prevent conflicts by managing data flow between tasks running at different speeds.
Final Answer:
To safely send and receive data between tasks without conflicts -> Option D
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
Step 1: Recall the FreeRTOS queue creation function
The correct function to create a queue is xQueueCreate.
Step 2: Verify the syntax of the function call
The function takes two parameters: the number of items and the size of each item.
Final Answer:
xQueueCreate(10, sizeof(int)); -> Option A
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
Step 1: Understand queue send order
The first value sent is 5, then 10. Queues keep order FIFO (first in, first out).
Step 2: Check which value is received first
The first received value is 5, the first sent value.
Final Answer:
5 -> Option B
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
Step 1: Analyze queue size and send operations
The queue can hold only 1 item, but two sends are done without receiving.
Step 2: Identify the problem caused by queue overflow
The second send will fail or block because the queue is full.
Final Answer:
The queue size is too small for two sends without receiving -> Option C
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
Step 1: Understand task speed differences
Task A produces data irregularly, Task B processes it at its own pace.
Step 2: Identify queue benefits for this scenario
Queues buffer data safely and keep order, allowing tasks to work independently without data loss.
Step 3: Eliminate incorrect options
Queues do not force speed matching, do not delete data automatically, and do not share variables directly.
Final Answer:
Because queues buffer data safely and handle different task speeds -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.