0
0
FreeRTOSprogramming~20 mins

Producer-consumer pattern in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 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
Output of Producer-Consumer Queue Example
What will be the output of this FreeRTOS producer-consumer example code snippet?
FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include <stdio.h>

QueueHandle_t queue;

void producer(void *params) {
    int count = 1;
    while (count <= 3) {
        xQueueSend(queue, &count, portMAX_DELAY);
        printf("Produced: %d\n", count);
        count++;
        vTaskDelay(pdMS_TO_TICKS(10));
    }
    vTaskDelete(NULL);
}

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

int main() {
    queue = xQueueCreate(3, sizeof(int));
    xTaskCreate(producer, "Producer", 1000, NULL, 1, NULL);
    xTaskCreate(consumer, "Consumer", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ANo output because tasks never run due to missing scheduler start
B
Consumed: 1
Consumed: 2
Consumed: 3
Produced: 1
Produced: 2
Produced: 3
C
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
D
Produced: 1
Produced: 2
Produced: 3
Consumed: 1
Consumed: 2
Consumed: 3
Attempts:
2 left
💡 Hint
Consider task scheduling with equal priority tasks and the effect of vTaskDelay in the producer.
🧠 Conceptual
intermediate
1:30remaining
Queue Length in Producer-Consumer Pattern
In a FreeRTOS producer-consumer pattern, if the queue length is set to 5 and the producer tries to send 7 items without the consumer running, what happens?
AThe producer discards the 6th and 7th items silently
BThe producer blocks on the 6th item until the consumer receives an item
CThe producer overwrites the oldest item in the queue when full
DThe producer crashes due to queue overflow
Attempts:
2 left
💡 Hint
Think about how FreeRTOS queues handle full conditions with blocking.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Producer-Consumer Synchronization
What is the main problem in this FreeRTOS producer-consumer code snippet? void producer(void *params) { int count = 0; while(1) { xQueueSend(queue, &count, 0); count++; vTaskDelay(pdMS_TO_TICKS(100)); } } void consumer(void *params) { int data; while(1) { if(xQueueReceive(queue, &data, 0)) { printf("Consumed %d\n", data); } } }
AConsumer uses zero block time causing it to miss items if queue is empty
BProducer uses zero block time causing it to block indefinitely
CQueue is not created before tasks start
DProducer increments count after sending causing off-by-one error
Attempts:
2 left
💡 Hint
Check the block time parameters in xQueueSend and xQueueReceive calls.
📝 Syntax
advanced
1:30remaining
Syntax Error in FreeRTOS Producer-Consumer Code
Which option contains the correct syntax for creating a queue and starting the scheduler in FreeRTOS?
A
queue = xQueueCreate(5, sizeof(int));
vTaskStartScheduler
B
queue = xQueueCreate(5, sizeof(int))
vTaskStartScheduler()
C
queue = xQueueCreate(5, int);
vTaskStartScheduler();
D
queue = xQueueCreate(5, sizeof(int));
vTaskStartScheduler();
Attempts:
2 left
💡 Hint
Check for missing semicolons and correct sizeof usage.
🚀 Application
expert
3:00remaining
Designing a Priority Producer-Consumer System
You want to design a FreeRTOS producer-consumer system where the consumer always processes high-priority items before low-priority ones. Which approach is best?
AUse two separate queues for high and low priority items; consumer checks high-priority queue first
BUse a queue with priority field and sort items inside the queue automatically
CUse a single queue and rely on task priority to process items in order
DUse a mutex to lock the queue when high-priority items arrive
Attempts:
2 left
💡 Hint
Think about how FreeRTOS queues work and how to prioritize data processing.