Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Multiple producers single consumer in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Producer-Consumer Master
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 example with multiple producers?

Consider two producer tasks sending integers to a single consumer task via a FreeRTOS queue. What will the consumer print?

FreeRTOS
QueueHandle_t queue;

void producer1(void *params) {
    int val = 1;
    while(1) {
        xQueueSend(queue, &val, portMAX_DELAY);
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

void producer2(void *params) {
    int val = 2;
    while(1) {
        xQueueSend(queue, &val, portMAX_DELAY);
        vTaskDelay(pdMS_TO_TICKS(150));
    }
}

void consumer(void *params) {
    int received;
    for (int i = 0; i < 5; i++) {
        xQueueReceive(queue, &received, portMAX_DELAY);
        printf("Received: %d\n", received);
    }
    vTaskDelete(NULL);
}

int main() {
    queue = xQueueCreate(10, sizeof(int));
    xTaskCreate(producer1, "Producer1", 1000, NULL, 1, NULL);
    xTaskCreate(producer2, "Producer2", 1000, NULL, 1, NULL);
    xTaskCreate(consumer, "Consumer", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
AReceived: 1\nReceived: 1\nReceived: 2\nReceived: 2\nReceived: 1
BReceived: 1\nReceived: 2\nReceived: 1\nReceived: 2\nReceived: 1
CReceived: 2\nReceived: 2\nReceived: 1\nReceived: 1\nReceived: 2
DReceived: 1\nReceived: 2\nReceived: 2\nReceived: 1\nReceived: 2
Attempts:
2 left
💡 Hint

Consider the delay times and task priorities to understand the order of messages received.

🧠 Conceptual
intermediate
1:30remaining
Which synchronization primitive best suits multiple producers and a single consumer in FreeRTOS?

In FreeRTOS, when multiple producer tasks send data to a single consumer task, which synchronization method ensures safe data transfer without data loss?

AMutex to protect a shared buffer
BCounting semaphore to signal data availability
CBinary semaphore for mutual exclusion
DQueue to hold data items
Attempts:
2 left
💡 Hint

Think about a structure that can hold multiple data items and manage access automatically.

🔧 Debug
advanced
2:00remaining
Why does this FreeRTOS multiple producer single consumer code cause data corruption?

Examine the code snippet below. Multiple producers write to a shared buffer without synchronization. What is the main cause of data corruption?

FreeRTOS
char sharedBuffer[10];

void producer(void *params) {
    const char *msg = (const char *)params;
    while(1) {
        strcpy(sharedBuffer, msg); // No synchronization
        vTaskDelay(pdMS_TO_TICKS(50));
    }
}

void consumer(void *params) {
    while(1) {
        printf("Buffer: %s\n", sharedBuffer);
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}
ANo mutual exclusion causes race conditions on sharedBuffer
BThe sharedBuffer is too small for the messages
CThe consumer task has lower priority than producers
DThe strcpy function is not thread-safe
Attempts:
2 left
💡 Hint

Think about what happens when multiple tasks write to the same memory without coordination.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a FreeRTOS queue for multiple producers and a single consumer?

Choose the correct code snippet to create a queue that holds 5 integers for multiple producers and a single consumer.

AQueueHandle_t q = xQueueCreate(5, sizeof(int));
BQueueHandle_t q = xQueueCreate(sizeof(int), 5);
CQueueHandle_t q = xQueueCreate(5);
DQueueHandle_t q = xQueueCreate(5, sizeof(int*));
Attempts:
2 left
💡 Hint

Check the order and meaning of parameters in xQueueCreate.

🚀 Application
expert
2:30remaining
How to ensure no data loss when multiple producers send data faster than the consumer can process?

In a FreeRTOS system with multiple producers and a single consumer, producers sometimes send data faster than the consumer can handle. Which approach best prevents data loss?

ADrop new data when the queue is full to avoid blocking producers
BUse a binary semaphore to signal the consumer immediately
CIncrease the queue length and block producers when queue is full
DIncrease consumer task priority and reduce producer delays
Attempts:
2 left
💡 Hint

Think about how to buffer data and control producer flow to avoid losing messages.

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