Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Priority inheritance behavior in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Priority Inheritance 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 priority inheritance example?

Consider a FreeRTOS system with three tasks: TaskHigh (priority 3), TaskMedium (priority 2), and TaskLow (priority 1). TaskLow holds a mutex that TaskHigh needs. TaskMedium runs continuously. What happens to TaskHigh's effective priority when it tries to take the mutex?

FreeRTOS
/* Pseudocode illustrating priority inheritance */
void TaskLow(void *params) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    // Simulate long processing
    vTaskDelay(1000);
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}

void TaskHigh(void *params) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    // Critical section
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}

void TaskMedium(void *params) {
    while(1) {
        // Runs continuously
    }
}

int main() {
    mutex = xSemaphoreCreateMutex();
    xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL);
    xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL);
    xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
ATaskHigh blocks until TaskLow releases the mutex; TaskLow temporarily inherits priority 3, preempting TaskMedium.
BTaskHigh preempts TaskMedium immediately; TaskLow continues at priority 1 until it releases the mutex.
CTaskMedium preempts TaskLow and TaskHigh because it has priority 2; TaskHigh waits indefinitely.
DTaskLow and TaskHigh run simultaneously; TaskMedium is blocked by the mutex.
Attempts:
2 left
💡 Hint

Think about how priority inheritance prevents priority inversion by temporarily boosting the priority of the mutex holder.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes priority inheritance in FreeRTOS?

Choose the statement that correctly explains how priority inheritance works in FreeRTOS.

APriority inheritance permanently raises the priority of the mutex holder until system reset.
BA task waiting for a mutex inherits the priority of the task holding the mutex to speed up execution.
CPriority inheritance disables all interrupts to avoid priority inversion during mutex access.
DA task holding a mutex inherits the highest priority of any task waiting for that mutex to prevent priority inversion.
Attempts:
2 left
💡 Hint

Consider which task's priority changes and why during mutex contention.

🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS system experience priority inversion despite using a mutex?

Examine the code snippet below. Despite using a mutex, a high priority task is blocked for a long time by a low priority task while a medium priority task runs. Identify the cause.

FreeRTOS
xSemaphoreHandle mutex;

void TaskLow(void *params) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    vTaskDelay(5000); // Simulate long work
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}

void TaskHigh(void *params) {
    vTaskDelay(100); // Ensure TaskLow takes mutex first
    xSemaphoreTake(mutex, portMAX_DELAY);
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}

void TaskMedium(void *params) {
    while(1) {
        // Busy loop
    }
}

int main() {
    mutex = xSemaphoreCreateMutex();
    xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL);
    xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL);
    xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
AThe mutex was created without priority inheritance enabled, so TaskLow does not inherit TaskHigh's priority.
BTaskMedium has a higher priority than TaskHigh, causing TaskHigh to be blocked.
CTaskHigh does not wait for the mutex properly and runs before TaskLow releases it.
DThe mutex is not given back by TaskLow, causing TaskHigh to block indefinitely.
Attempts:
2 left
💡 Hint

Check how the mutex was created and if priority inheritance is enabled by default.

📝 Syntax
advanced
1:30remaining
Identify the syntax error preventing priority inheritance in this FreeRTOS mutex usage

Which option contains the syntax error that breaks priority inheritance behavior in this FreeRTOS mutex example?

FreeRTOS
SemaphoreHandle_t mutex;

void TaskA(void *params) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    // Critical section
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}

int main() {
    mutex = xSemaphoreCreateMutex();
    xTaskCreate(TaskA, "TaskA", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
AvTaskDelete(NULL) should be called before xSemaphoreGive(mutex)
BIncorrect type declaration: SemaphoreHandle_t should be SemaphoreHandle
CMissing semicolon after xSemaphoreTake(mutex, portMAX_DELAY)
DxTaskCreate requires a function pointer cast to (TaskFunction_t)
Attempts:
2 left
💡 Hint

Look carefully at the line with xSemaphoreTake and check punctuation.

🚀 Application
expert
2:00remaining
How many tasks can simultaneously inherit priority from a mutex holder in FreeRTOS?

In a FreeRTOS system, if multiple higher priority tasks are waiting on a mutex held by a lower priority task, how many of these tasks' priorities does the mutex holder inherit?

AThe mutex holder inherits all waiting tasks' priorities individually and switches between them.
BThe mutex holder inherits the highest priority among all waiting tasks, not multiple priorities simultaneously.
CThe mutex holder inherits the priority of the first task that requested the mutex only.
DThe mutex holder inherits the sum of all waiting tasks' priorities combined.
Attempts:
2 left
💡 Hint

Consider how priority inheritance resolves priority inversion with multiple waiting tasks.

Practice

(1/5)
1. What is the main purpose of priority inheritance in FreeRTOS?
easy
A. To increase the priority of all tasks equally
B. To prevent low priority tasks from blocking high priority tasks for too long
C. To allow tasks to run in a random order
D. To disable task scheduling temporarily

Solution

  1. Step 1: Understand priority inversion problem

    Priority inversion happens when a low priority task holds a resource needed by a high priority task, causing delays.
  2. Step 2: Role of priority inheritance

    Priority inheritance temporarily raises the low priority task's priority to the high priority task's level to reduce blocking time.
  3. Final Answer:

    To prevent low priority tasks from blocking high priority tasks for too long -> Option B
  4. Quick Check:

    Priority inheritance prevents blocking = A [OK]
Hint: Priority inheritance helps high priority tasks run faster [OK]
Common Mistakes:
  • Thinking it raises all tasks' priorities
  • Confusing it with disabling scheduling
  • Believing it randomizes task order
2. Which FreeRTOS synchronization primitive automatically supports priority inheritance?
easy
A. Semaphore (binary)
B. Event group
C. Mutex
D. Queue

Solution

  1. Step 1: Identify synchronization primitives

    FreeRTOS offers semaphores, mutexes, queues, and event groups for task synchronization.
  2. Step 2: Check which supports priority inheritance

    Mutexes in FreeRTOS implement priority inheritance automatically to avoid priority inversion.
  3. Final Answer:

    Mutex -> Option C
  4. Quick Check:

    Mutex = priority inheritance support [OK]
Hint: Mutexes handle priority inheritance automatically [OK]
Common Mistakes:
  • Choosing binary semaphore which lacks priority inheritance
  • Confusing queues or event groups with mutexes
  • Assuming all synchronization primitives support priority inheritance
3. Consider three tasks: High, Medium, and Low priority. Low holds a mutex needed by High. Medium is ready to run. What happens when priority inheritance is enabled?
medium
A. Medium task runs before Low and High
B. Low task is blocked until Medium finishes
C. High task runs immediately, ignoring mutex
D. Low task inherits High priority, runs before Medium

Solution

  1. Step 1: Analyze priority inversion scenario

    Low priority task holds mutex needed by High priority task, but Medium priority task is ready.
  2. Step 2: Effect of priority inheritance

    Low task temporarily inherits High priority, so it runs before Medium to release mutex faster.
  3. Final Answer:

    Low task inherits High priority, runs before Medium -> Option D
  4. Quick Check:

    Priority inheritance boosts Low task priority = B [OK]
Hint: Low task runs at High priority to unblock High task [OK]
Common Mistakes:
  • Assuming Medium runs before Low despite inheritance
  • Thinking High runs without mutex
  • Believing Low is blocked by Medium
4. You wrote code using a binary semaphore to protect a shared resource, but high priority tasks are blocked too long. What is the likely issue?
medium
A. Binary semaphore does not support priority inheritance
B. Mutex was not created properly
C. High priority tasks are not using the semaphore
D. Task priorities are all equal

Solution

  1. Step 1: Identify synchronization primitive used

    Binary semaphore is used to protect shared resource.
  2. Step 2: Check priority inheritance support

    Binary semaphores do not support priority inheritance, causing priority inversion issues.
  3. Final Answer:

    Binary semaphore does not support priority inheritance -> Option A
  4. Quick Check:

    Binary semaphore lacks priority inheritance = A [OK]
Hint: Use mutex, not binary semaphore, for priority inheritance [OK]
Common Mistakes:
  • Assuming binary semaphore supports priority inheritance
  • Blaming task priorities without checking synchronization
  • Ignoring mutex creation correctness
5. In a system with tasks A (priority 3), B (priority 2), and C (priority 1), task C holds a mutex needed by A. Task B is CPU intensive. How does FreeRTOS priority inheritance help avoid priority inversion here?
hard
A. Task C temporarily inherits priority 3, preempting B to release mutex faster
B. Task B inherits priority 3 to run before C and A
C. Task A runs without waiting for mutex
D. Task C is blocked until B finishes

Solution

  1. Step 1: Understand task priorities and mutex ownership

    Task C (low priority) holds mutex needed by Task A (high priority), while Task B (medium priority) is CPU intensive.
  2. Step 2: Apply priority inheritance behavior

    Task C inherits Task A's high priority (3) temporarily, allowing it to preempt Task B and release the mutex quickly.
  3. Final Answer:

    Task C temporarily inherits priority 3, preempting B to release mutex faster -> Option A
  4. Quick Check:

    Priority inheritance boosts C to A's priority = C [OK]
Hint: Low priority task inherits high priority to beat medium task [OK]
Common Mistakes:
  • Thinking medium priority task inherits high priority
  • Assuming high priority task runs without mutex
  • Believing low priority task is blocked by medium task
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