Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Resource manager task pattern in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Resource Manager 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 resource manager task code?

Consider a FreeRTOS task that manages access to a shared resource using a queue. What will be printed when the task receives a request?

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include <stdio.h>

QueueHandle_t resourceQueue;

void ResourceManagerTask(void *pvParameters) {
    char *request;
    while(1) {
        if(xQueueReceive(resourceQueue, &request, portMAX_DELAY) == pdPASS) {
            printf("Processing request: %s\n", request);
            vTaskDelay(pdMS_TO_TICKS(100));
        }
    }
}

int main() {
    resourceQueue = xQueueCreate(5, sizeof(char*));
    xTaskCreate(ResourceManagerTask, "ResMgr", 1000, NULL, 1, NULL);
    char *req = "Task1 Request";
    xQueueSend(resourceQueue, &req, 0);
    vTaskStartScheduler();
    return 0;
}
AProcessing request: Task1 Request
BProcessing request: (null)
CCompilation error due to missing queue initialization
DRuntime error: Queue handle is NULL
Attempts:
2 left
💡 Hint

Check how the queue is created and how the request is sent and received.

🧠 Conceptual
intermediate
1:30remaining
Which synchronization primitive is best for a FreeRTOS resource manager task to protect a shared resource?

In a FreeRTOS resource manager task pattern, which synchronization method ensures exclusive access to a shared resource?

ABinary semaphore
BMutex
CCounting semaphore
DEvent group
Attempts:
2 left
💡 Hint

Think about priority inheritance and exclusive access.

🔧 Debug
advanced
2:30remaining
What error does this FreeRTOS resource manager task code produce?

Analyze the code below. What error will occur when running this resource manager task?

FreeRTOS
QueueHandle_t resourceQueue;

void ResourceManagerTask(void *pvParameters) {
    char request[20];
    while(1) {
        if(xQueueReceive(resourceQueue, &request, portMAX_DELAY) == pdPASS) {
            printf("Request: %s\n", request);
        }
    }
}

int main() {
    resourceQueue = xQueueCreate(5, sizeof(char*));
    xTaskCreate(ResourceManagerTask, "ResMgr", 1000, NULL, 1, NULL);
    char req[] = "Request1";
    xQueueSend(resourceQueue, &req, 0);
    vTaskStartScheduler();
    return 0;
}
AIncorrect output: prints garbage instead of request string
BNo error, prints 'Request1' correctly
CCompilation error: incompatible pointer types
DRuntime error: Stack overflow in ResourceManagerTask
Attempts:
2 left
💡 Hint

Check the queue item size and what is sent/received.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this FreeRTOS resource manager task snippet?

Identify the correct fix for the syntax error in the following snippet:

if xQueueReceive(resourceQueue, &request, portMAX_DELAY) == pdPASS {
    processRequest(request);
}
A
if (xQueueReceive(resourceQueue, &amp;request, portMAX_DELAY) == pdPASS) then {
    processRequest(request);
}
B
if xQueueReceive(resourceQueue, &amp;request, portMAX_DELAY) == pdPASS then {
    processRequest(request);
}
C
if xQueueReceive(resourceQueue, &amp;request, portMAX_DELAY) == pdPASS:
    processRequest(request);
D
if (xQueueReceive(resourceQueue, &amp;request, portMAX_DELAY) == pdPASS) {
    processRequest(request);
}
Attempts:
2 left
💡 Hint

Remember C syntax for if statements.

🚀 Application
expert
1:00remaining
How many items can the FreeRTOS resource manager queue hold in this configuration?

Given the queue creation below, how many items can it hold?

resourceQueue = xQueueCreate(10, sizeof(int));
ACannot hold any items because size is incorrect
B1 item, size 10 * sizeof(int)
C10 items, each of size int
DDepends on the FreeRTOS heap size only
Attempts:
2 left
💡 Hint

Check the parameters of xQueueCreate.

Practice

(1/5)
1. What is the main purpose of the Resource Manager Task Pattern in FreeRTOS?
easy
A. To centralize control of shared resources and prevent conflicts
B. To create multiple tasks accessing resources directly
C. To increase CPU usage by running more tasks simultaneously
D. To eliminate the need for task synchronization mechanisms

Solution

  1. Step 1: Understand resource sharing challenges

    When multiple tasks access shared resources directly, conflicts and data corruption can occur.
  2. Step 2: Role of Resource Manager Task

    The Resource Manager Task centralizes access, so tasks send requests to it instead of accessing resources directly, preventing conflicts.
  3. Final Answer:

    To centralize control of shared resources and prevent conflicts -> Option A
  4. Quick Check:

    Centralized control = Prevent conflicts [OK]
Hint: Resource manager centralizes resource access to avoid conflicts [OK]
Common Mistakes:
  • Thinking tasks access resources directly
  • Assuming it increases CPU usage
  • Believing it removes synchronization needs
2. Which of the following is the correct way to send a request to a Resource Manager Task in FreeRTOS?
easy
A. Using a queue to send a request message to the manager task
B. Calling the resource manager function without synchronization
C. Directly accessing the shared resource variable
D. Using global variables without any task communication

Solution

  1. Step 1: Identify communication method

    Resource Manager Tasks typically receive requests via queues to serialize access.
  2. Step 2: Evaluate options

    Only Using a queue to send a request message to the manager task uses a queue to send requests, which is the correct and safe method.
  3. Final Answer:

    Using a queue to send a request message to the manager task -> Option A
  4. Quick Check:

    Queue communication = Correct request method [OK]
Hint: Use queues to send requests to resource manager task [OK]
Common Mistakes:
  • Accessing shared variables directly
  • Ignoring synchronization
  • Using global variables without communication
3. Given the following FreeRTOS code snippet, what will be the output?
typedef struct {
  int command;
  int data;
} RequestMessage;

QueueHandle_t resourceQueue;

void ResourceManagerTask(void *params) {
  RequestMessage req;
  while (1) {
    if (xQueueReceive(resourceQueue, &req, portMAX_DELAY) == pdPASS) {
      if (req.command == 1) {
        printf("Processed data: %d\n", req.data * 2);
      }
    }
  }
}

void ClientTask(void *params) {
  RequestMessage msg = {1, 5};
  xQueueSend(resourceQueue, &msg, 0);
  vTaskDelete(NULL);
}
medium
A. Processed data: 5
B. Processed data: 10
C. No output, queue is empty
D. Runtime error due to invalid queue

Solution

  1. Step 1: Analyze ClientTask sending message

    ClientTask sends a message with command=1 and data=5 to the resourceQueue.
  2. Step 2: Analyze ResourceManagerTask processing

    ResourceManagerTask receives the message, checks command==1, then prints data*2 = 5*2 = 10.
  3. Final Answer:

    Processed data: 10 -> Option B
  4. Quick Check:

    Data 5 doubled = 10 output [OK]
Hint: Multiply data by 2 when command is 1 [OK]
Common Mistakes:
  • Confusing data value with output
  • Assuming no message received
  • Ignoring the multiplication by 2
4. Identify the bug in this Resource Manager Task code snippet:
void ResourceManagerTask(void *params) {
  RequestMessage req;
  while (1) {
    if (xQueueReceive(resourceQueue, &req, 0) == pdPASS) {
      if (req.command == 1) {
        // Process command
      }
    }
  }
}
What is the problem?
medium
A. xQueueReceive should not be used in Resource Manager Task
B. RequestMessage struct is not defined
C. ResourceManagerTask should not use a while loop
D. Using zero block time causes missed messages if queue is empty

Solution

  1. Step 1: Understand xQueueReceive block time

    Using 0 as block time means the call returns immediately if queue is empty, possibly missing messages.
  2. Step 2: Effect on Resource Manager Task

    The task may spin without waiting, causing high CPU usage and missed processing.
  3. Final Answer:

    Using zero block time causes missed messages if queue is empty -> Option D
  4. Quick Check:

    Zero block time = possible missed messages [OK]
Hint: Use portMAX_DELAY to wait for messages in resource manager [OK]
Common Mistakes:
  • Ignoring block time effect
  • Thinking struct is undefined
  • Believing while loop is wrong
5. You want to design a Resource Manager Task that handles multiple resource types (e.g., UART, SPI) with different commands. Which approach best applies the Resource Manager Task Pattern to handle this efficiently?
hard
A. Create separate manager tasks for each resource type and let clients send requests directly to each
B. Let clients access resources directly with mutexes protecting each resource
C. Use a single queue with a message struct containing resource type and command fields, then switch-case inside the manager task
D. Use global variables to store resource states and update them without task communication

Solution

  1. Step 1: Consider centralized control for multiple resources

    A single Resource Manager Task can handle multiple resource types by differentiating requests.
  2. Step 2: Use message struct with resource type and command

    This allows the manager task to use a switch-case to process requests efficiently and safely.
  3. Final Answer:

    Use a single queue with a message struct containing resource type and command fields, then switch-case inside the manager task -> Option C
  4. Quick Check:

    Single queue + message struct = efficient multi-resource management [OK]
Hint: Use message struct with resource type and switch-case in manager [OK]
Common Mistakes:
  • Creating multiple manager tasks unnecessarily
  • Allowing direct resource access with mutexes
  • Using global variables without communication
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