Bird
Raised Fist0
FreeRTOSprogramming~10 mins

uxTaskPriorityGet() for reading priority in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - uxTaskPriorityGet() for reading priority
Call uxTaskPriorityGet(taskHandle)
↓
Check if taskHandle is NULL?
No→Access task's priority
↓
Return task priority
↓
Return current task's priority
↓
Output priority value
The function checks if a task handle is given. If NULL, it returns the priority of the current task. Otherwise, it returns the priority of the specified task.
Execution Sample
FreeRTOS
TaskHandle_t myTask = xTaskGetCurrentTaskHandle();
UBaseType_t prio = uxTaskPriorityGet(myTask);
printf("Priority: %u\n", prio);
This code gets the current task handle, reads its priority using uxTaskPriorityGet(), and prints it.
Execution Table
StepInput taskHandleCheck if NULLPriority AccessedReturned PriorityOutput
1myTask (valid handle)NoSpecified task's priority read5Priority: 5
2NULLYesCurrent task's priority read5Priority: 5
3invalid handleNoUndefined behavior (not shown)--
💡 Function returns the priority of the given task or current task if NULL.
Variable Tracker
VariableStartAfter uxTaskPriorityGet callFinal
myTaskValid task handleValid task handleValid task handle
prioUndefined55
Key Moments - 2 Insights
What happens if we pass NULL as the task handle?
If NULL is passed, uxTaskPriorityGet() returns the priority of the current running task, as shown in execution_table row 2.
Can uxTaskPriorityGet() be used with an invalid task handle?
No, passing an invalid handle leads to undefined behavior, which is not safe and not shown in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what priority is returned when passing a valid task handle?
A5
B0
CUndefined
DNULL
💡 Hint
See execution_table row 1 under 'Returned Priority' column.
At which step does the function return the current task's priority because the input is NULL?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Check execution_table row 2 where 'Input taskHandle' is NULL.
If the variable 'prio' is 5 after the call, what was the likely input task handle?
ANULL
BValid task handle
CInvalid handle
DUninitialized
💡 Hint
Refer to variable_tracker and execution_table rows 1 and 2.
Concept Snapshot
uxTaskPriorityGet(taskHandle) returns the priority of the given task.
If taskHandle is NULL, it returns the current task's priority.
Priority is of type UBaseType_t (usually an unsigned int).
Passing invalid handles causes undefined behavior.
Use this to check task priority safely.
Full Transcript
The uxTaskPriorityGet() function reads the priority of a FreeRTOS task. When you call it with a task handle, it checks if the handle is NULL. If it is NULL, it returns the priority of the current running task. Otherwise, it returns the priority of the specified task. The priority is returned as an unsigned integer. Passing an invalid task handle is unsafe and can cause errors. This function helps you know the priority level of tasks in your system.

Practice

(1/5)
1.

What does the FreeRTOS function uxTaskPriorityGet() do?

easy
A. It returns the priority of a specified task.
B. It sets the priority of a specified task.
C. It deletes a specified task.
D. It creates a new task.

Solution

  1. Step 1: Understand the function purpose

    uxTaskPriorityGet() is designed to read the priority of a task, not to modify or manage tasks.
  2. Step 2: Match function behavior to options

    Only "It returns the priority of a specified task." correctly states it returns the priority of a specified task.
  3. Final Answer:

    It returns the priority of a specified task. -> Option A
  4. Quick Check:

    uxTaskPriorityGet() reads priority = It returns the priority of a specified task. [OK]
Hint: Remember: 'Get' means read, not change [OK]
Common Mistakes:
  • Confusing 'get' with 'set' functions
  • Thinking it creates or deletes tasks
  • Assuming it returns task handle
2.

Which of the following is the correct way to get the priority of the current task using uxTaskPriorityGet()?

TaskHandle_t xTaskHandle;
UBaseType_t uxPriority = ?;
easy
A. uxPriority = uxTaskPriorityGet(xTaskHandle);
B. uxPriority = uxTaskPriorityGet(NULL);
C. uxPriority = uxTaskPriorityGet(&xTaskHandle);
D. uxPriority = uxTaskPriorityGet(*xTaskHandle);

Solution

  1. Step 1: Recall how to get current task priority

    Passing NULL to uxTaskPriorityGet() returns the priority of the current task.
  2. Step 2: Check each option

    uxPriority = uxTaskPriorityGet(xTaskHandle); uses uninitialized xTaskHandle (undefined behavior). uxPriority = uxTaskPriorityGet(&xTaskHandle); passes address (wrong type). uxPriority = uxTaskPriorityGet(*xTaskHandle); dereferences uninitialized handle (crash). Only uxPriority = uxTaskPriorityGet(NULL); is correct.
  3. Final Answer:

    uxPriority = uxTaskPriorityGet(NULL); -> Option B
  4. Quick Check:

    Use NULL for current task priority [OK]
Hint: Pass NULL to get current task priority [OK]
Common Mistakes:
  • Passing address of handle instead of handle
  • Dereferencing handle incorrectly
  • Using uninitialized task handle
3.

Given the following code snippet, what will be printed?

TaskHandle_t xHandle = xTaskGetHandle("Task1");
UBaseType_t uxPriority = uxTaskPriorityGet(xHandle);
printf("Priority: %u\n", uxPriority);

Assume "Task1" exists and has priority 3.

medium
A. Priority: 0
B. Compilation error
C. Priority: 3
D. Priority: 1

Solution

  1. Step 1: Understand task handle retrieval

    xTaskGetHandle("Task1") returns the handle of the task named "Task1" which exists with priority 3.
  2. Step 2: Use uxTaskPriorityGet() to read priority

    Calling uxTaskPriorityGet(xHandle) returns the priority of that task, which is 3.
  3. Final Answer:

    Priority: 3 -> Option C
  4. Quick Check:

    uxTaskPriorityGet returns task priority = 3 [OK]
Hint: Get handle then get priority returns actual task priority [OK]
Common Mistakes:
  • Assuming default priority 0
  • Confusing handle with task name
  • Expecting syntax error
4.

Identify the error in this code snippet:

TaskHandle_t xHandle;
UBaseType_t uxPriority = uxTaskPriorityGet(xHandle);
printf("Priority: %u\n", uxPriority);
medium
A. xHandle is uninitialized before use.
B. uxTaskPriorityGet() cannot be called with a TaskHandle_t.
C. printf format specifier is incorrect.
D. uxPriority should be a pointer.

Solution

  1. Step 1: Check variable initialization

    xHandle is declared but not assigned any value before use.
  2. Step 2: Understand consequences of uninitialized handle

    Passing an uninitialized handle to uxTaskPriorityGet() leads to undefined behavior or incorrect priority reading.
  3. Final Answer:

    xHandle is uninitialized before use. -> Option A
  4. Quick Check:

    Always initialize task handles before use [OK]
Hint: Initialize task handle before getting priority [OK]
Common Mistakes:
  • Ignoring uninitialized variables
  • Misunderstanding function parameter types
  • Assuming pointer needed for uxPriority
5.

You want to check if the current task has higher priority than another task named "WorkerTask". Which code snippet correctly compares their priorities?

TaskHandle_t xWorkerHandle = xTaskGetHandle("WorkerTask");
UBaseType_t uxCurrentPriority = uxTaskPriorityGet(NULL);
UBaseType_t uxWorkerPriority = uxTaskPriorityGet(xWorkerHandle);
// What next?
hard
A. if (uxCurrentPriority != uxWorkerPriority) { /* current task higher */ }
B. if (uxCurrentPriority < uxWorkerPriority) { /* current task higher */ }
C. if (uxCurrentPriority == uxWorkerPriority) { /* current task higher */ }
D. if (uxCurrentPriority > uxWorkerPriority) { /* current task higher */ }

Solution

  1. Step 1: Understand priority comparison logic

    Higher numeric value means higher priority in FreeRTOS.
  2. Step 2: Compare current and worker task priorities

    Use > operator to check if current task priority is higher than worker's.
  3. Final Answer:

    if (uxCurrentPriority > uxWorkerPriority) { /* current task higher */ } -> Option D
  4. Quick Check:

    Higher number = higher priority [OK]
Hint: Use '>' to check if current task priority is higher [OK]
Common Mistakes:
  • Using '<' instead of '>' for priority comparison
  • Confusing equality with higher priority
  • Not retrieving correct task handles
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