Challenge - 5 Problems
FreeRTOS Producer-Consumer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Consider task scheduling with equal priority tasks and the effect of vTaskDelay in the producer.
✗ Incorrect
Both tasks have the same priority. The producer sends an item, prints, then delays (yielding). The consumer receives and prints it, then blocks on the next receive. The producer wakes and repeats, resulting in interleaved output.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how FreeRTOS queues handle full conditions with blocking.
✗ Incorrect
FreeRTOS queues block the sending task when the queue is full if a block time is specified (like portMAX_DELAY). The producer will wait until the consumer removes an item before sending more.
🔧 Debug
advanced2: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);
}
}
}
Attempts:
2 left
💡 Hint
Check the block time parameters in xQueueSend and xQueueReceive calls.
✗ Incorrect
The consumer uses zero block time in xQueueReceive, so it busy-waits (polls) when the queue is empty instead of blocking efficiently. This wastes CPU cycles and is poor practice.
📝 Syntax
advanced1:30remaining
Syntax Error in FreeRTOS Producer-Consumer Code
Which option contains the correct syntax for creating a queue and starting the scheduler in FreeRTOS?
Attempts:
2 left
💡 Hint
Check for missing semicolons and correct sizeof usage.
✗ Incorrect
Option D correctly uses semicolons and sizeof(int) to create the queue and calls vTaskStartScheduler with parentheses.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Think about how FreeRTOS queues work and how to prioritize data processing.
✗ Incorrect
FreeRTOS queues do not sort items automatically. Using two queues allows the consumer to check the high-priority queue first, ensuring those items are processed before low-priority ones.