Bird
Raised Fist0
FreeRTOSprogramming~30 mins

Multiple producers single consumer in FreeRTOS - Mini Project: Build & Apply

Choose your learning style10 modes available
Multiple Producers Single Consumer in FreeRTOS
📖 Scenario: Imagine a factory where several machines (producers) create parts and place them on a conveyor belt. One worker (consumer) takes parts from the belt to assemble a product. We want to simulate this in FreeRTOS where multiple producer tasks send data to a single consumer task safely.
🎯 Goal: Build a FreeRTOS program with multiple producer tasks sending messages to a single consumer task using a queue. The consumer will print the received messages.
📋 What You'll Learn
Create a queue to hold messages
Create two producer tasks that send different messages
Create one consumer task that receives and prints messages
Use FreeRTOS API functions correctly for task creation and queue handling
💡 Why This Matters
🌍 Real World
This pattern is common in embedded systems where multiple sensors or inputs produce data that a single processor or handler must consume safely.
💼 Career
Understanding task synchronization and communication in FreeRTOS is essential for embedded software engineers working on real-time systems like IoT devices, automotive controllers, and industrial automation.
Progress0 / 4 steps
1
Create the message queue
Create a queue called messageQueue that can hold 5 items of type char*.
FreeRTOS
Hint

Use xQueueCreate with 5 as the queue length and sizeof(char*) as the item size.

2
Create producer tasks
Create two producer tasks called Producer1 and Producer2. Each task should send a unique message string to messageQueue every 1000 milliseconds using xQueueSend.
FreeRTOS
Hint

Define two functions for producers. Use xQueueSend to send messages and vTaskDelay for timing. Create tasks with xTaskCreate.

3
Create the consumer task
Create a consumer task called Consumer that receives messages from messageQueue using xQueueReceive and prints them using printf. The task should run indefinitely.
FreeRTOS
Hint

Use xQueueReceive to get messages and printf to show them. Create the consumer task with higher priority.

4
Print messages in the consumer task
Add a printf statement inside the Consumer task to print the received message with the format: Received: <message>. Run the FreeRTOS scheduler in main.
FreeRTOS
Hint

Use printf("Received: %s\n", receivedMsg); inside the consumer task. Start the scheduler in main.

Practice

(1/5)
1.

In FreeRTOS, when multiple producers send data to a single consumer using a queue, what ensures that data is received in the correct order?

easy
A. The consumer sorts data after receiving it.
B. Each producer uses a separate queue to avoid conflicts.
C. Producers send data only when the consumer is ready.
D. The queue maintains the order of data sent by producers.

Solution

  1. Step 1: Understand FreeRTOS queue behavior

    FreeRTOS queues store data in the order it is sent, preserving sequence.
  2. Step 2: Analyze producer-consumer interaction

    Multiple producers send data to one queue; the queue keeps data ordered for the consumer.
  3. Final Answer:

    The queue maintains the order of data sent by producers. -> Option D
  4. Quick Check:

    Queue order = correct data sequence [OK]
Hint: Remember: queues keep data order automatically [OK]
Common Mistakes:
  • Thinking each producer needs its own queue
  • Assuming consumer sorts data
  • Believing producers wait for consumer readiness
2.

Which of the following is the correct way to send data to a FreeRTOS queue from a producer task?

QueueHandle_t xQueue;
int data = 10;
// Which function call is correct?
easy
A. xQueueCreate(data, sizeof(xQueue));
B. xQueueReceive(xQueue, &data, portMAX_DELAY);
C. xQueueSend(xQueue, &data, portMAX_DELAY);
D. xQueueDelete(xQueue);

Solution

  1. Step 1: Identify the function to send data

    xQueueSend is used to send data to a queue in FreeRTOS.
  2. Step 2: Confirm parameters

    xQueueSend takes the queue handle, pointer to data, and wait time (portMAX_DELAY waits indefinitely).
  3. Final Answer:

    xQueueSend(xQueue, &data, portMAX_DELAY); -> Option C
  4. Quick Check:

    Send data = xQueueSend [OK]
Hint: Use xQueueSend to send, xQueueReceive to receive [OK]
Common Mistakes:
  • Using xQueueReceive to send data
  • Confusing queue creation with sending
  • Deleting queue instead of sending
3.

Consider this FreeRTOS code snippet where two producer tasks send integers to a queue and one consumer task receives them:

QueueHandle_t xQueue = xQueueCreate(5, sizeof(int));

// Producer 1 sends 1
xQueueSend(xQueue, &(int){1}, 0);
// Producer 2 sends 2
xQueueSend(xQueue, &(int){2}, 0);

int receivedValue;
xQueueReceive(xQueue, &receivedValue, 0);
printf("%d", receivedValue);

What will be printed?

medium
A. 1
B. 2
C. 0
D. Compilation error

Solution

  1. Step 1: Analyze the order of sending

    Producer 1 sends 1 first, then Producer 2 sends 2 to the queue.
  2. Step 2: Understand queue receive behavior

    xQueueReceive reads the first item in the queue, which is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    First sent = first received = 1 [OK]
Hint: Queue returns data in send order, first in first out [OK]
Common Mistakes:
  • Assuming last sent is received first
  • Thinking receive returns 0 by default
  • Confusing send and receive functions
4.

In a FreeRTOS system with multiple producers and one consumer, the consumer task sometimes blocks indefinitely waiting on xQueueReceive. What is the most likely cause?

medium
A. Producers never send data to the queue.
B. The queue size is too large.
C. The consumer uses xQueueSend instead of xQueueReceive.
D. The queue was deleted before receiving.

Solution

  1. Step 1: Understand blocking on xQueueReceive

    If the consumer blocks indefinitely, it means no data is available in the queue.
  2. Step 2: Identify why no data is available

    Most likely, producers are not sending data, so the queue remains empty.
  3. Final Answer:

    Producers never send data to the queue. -> Option A
  4. Quick Check:

    No data sent = consumer blocks [OK]
Hint: If consumer blocks, check if producers send data [OK]
Common Mistakes:
  • Blaming queue size for blocking
  • Mixing send and receive functions
  • Ignoring queue deletion effects
5.

You have three producer tasks sending sensor readings to a single consumer task via a FreeRTOS queue. The queue size is 5. If all producers send data rapidly, what is the best way to avoid losing data?

hard
A. Use separate queues for each producer without increasing size.
B. Increase the queue size or use xQueueSendToBack with blocking wait.
C. Make the consumer task sleep longer to batch process data.
D. Delete the queue and recreate it frequently.

Solution

  1. Step 1: Understand queue overflow risk

    If producers send data faster than consumer processes, queue fills and data can be lost.
  2. Step 2: Choose solution to prevent data loss

    Increasing queue size or blocking send calls (like xQueueSendToBack with wait) lets producers wait instead of losing data.
  3. Final Answer:

    Increase the queue size or use xQueueSendToBack with blocking wait. -> Option B
  4. Quick Check:

    Prevent overflow = bigger queue or blocking send [OK]
Hint: Avoid data loss by bigger queue or blocking send [OK]
Common Mistakes:
  • Using separate queues without consumer changes
  • Making consumer sleep longer causes more overflow
  • Deleting queue causes data loss
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