Task function signature in FreeRTOS - Time & Space Complexity
We want to understand how the time it takes to run a FreeRTOS task function changes as the task runs.
Specifically, we ask: How does the task function's execution time grow with its input or workload?
Analyze the time complexity of the following FreeRTOS task function signature and its loop.
void vTaskFunction(void *pvParameters) {
for(;;) {
// Task code here
}
}
This code defines a task function that runs forever in a loop, doing some work repeatedly.
Look for loops or repeated actions inside the task function.
- Primary operation: The infinite
for(;;)loop repeats the task's work. - How many times: It runs endlessly, repeating the task code over and over.
The task runs continuously, so the total execution time grows indefinitely as it keeps running.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Runs 10 times through the loop |
| 100 | Runs 100 times through the loop |
| 1000 | Runs 1000 times through the loop |
Pattern observation: The number of operations grows linearly with how many times the loop runs.
Time Complexity: O(n)
This means the time spent grows directly with the number of loop iterations the task performs.
[X] Wrong: "The task function runs once and then stops, so time is constant."
[OK] Correct: The task function has an infinite loop, so it keeps running repeatedly, making time grow with the number of iterations.
Understanding how task functions run repeatedly helps you explain how embedded systems handle ongoing work efficiently.
"What if the task function had a delay inside the loop? How would that affect the time complexity?"
Practice
What is the correct signature for a FreeRTOS task function?
Solution
Step 1: Recall FreeRTOS task function signature
FreeRTOS tasks must have the signature with return type void and a single void pointer parameter.Step 2: Match the signature with options
Only void TaskName(void *pvParameters) matchesvoid TaskName(void *pvParameters)exactly.Final Answer:
void TaskName(void *pvParameters) -> Option BQuick Check:
Task signature = void TaskName(void *pvParameters) [OK]
- Using int return type instead of void
- Omitting the void* parameter
- Using incorrect parameter types
Which of the following is a syntactically correct FreeRTOS task function declaration?
void MyTask(_____)
Solution
Step 1: Understand parameter type for FreeRTOS tasks
Task functions must accept a single parameter of typevoid *.Step 2: Check each option
Only void *pvParameters usesvoid *pvParameters, which is correct syntax.Final Answer:
void *pvParameters -> Option AQuick Check:
Parameter type = void *pvParameters [OK]
- Using int or char pointer instead of void pointer
- Using void without pointer
- Missing parameter entirely
What will happen if you run this FreeRTOS task function?
void ExampleTask(void *pvParameters) {
for(;;) {
// Task code here
}
}Solution
Step 1: Analyze the task function loop
The task contains afor(;;)infinite loop, so it runs repeatedly.Step 2: Understand FreeRTOS task behavior
Tasks should not return; they run inside infinite loops to keep running.Final Answer:
The task will run repeatedly in an infinite loop. -> Option DQuick Check:
Infinite loop in task = runs repeatedly [OK]
- Thinking task runs once and exits
- Expecting return value from task
- Confusing infinite loop with error
Identify the error in this FreeRTOS task function:
void FaultyTask(void *pvParameters) {
int count = 0;
while(count < 5) {
count++;
}
return;
}Solution
Step 1: Check task function return behavior
FreeRTOS tasks must not return; they should run inside infinite loops.Step 2: Analyze the code flow
This task loops 5 times then returns, which is incorrect for FreeRTOS tasks.Final Answer:
The function should not return; it must loop forever. -> Option AQuick Check:
Tasks must not return, use infinite loop [OK]
- Allowing task to return
- Changing parameter type incorrectly
- Misunderstanding loop conditions
You want to pass an integer value to a FreeRTOS task. Which is the correct way to define and use the task function?
void NumberTask(_____)
{
int received = *(int *)pvParameters;
for(;;) {
// Use received value
}
}Solution
Step 1: Recall task function parameter type
Task functions must accept avoid *parameter to allow any data type.Step 2: Understand data passing
The integer is passed via pointer cast fromvoid *, so parameter must bevoid *pvParameters.Final Answer:
void *pvParameters -> Option CQuick Check:
Use void* parameter to pass any data type [OK]
- Using int instead of void* parameter
- Using void without pointer
- Not casting void* to correct type inside task
Ready to prove it?
Pass the test and this pattern moves unknown
