Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Time-slicing for equal priority tasks in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Time-Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of time-slicing with two equal priority tasks

Consider two FreeRTOS tasks with the same priority. Each task toggles an LED and then calls vTaskDelay(1). What is the expected behavior of the LED toggling?

FreeRTOS
void Task1(void *pvParameters) {
    for (;;) {
        ToggleLED1();
        vTaskDelay(1);
    }
}

void Task2(void *pvParameters) {
    for (;;) {
        ToggleLED2();
        vTaskDelay(1);
    }
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    for(;;);
}
ABoth LEDs toggle simultaneously at the same time.
BOnly LED1 toggles because Task1 always runs first and blocks Task2.
CBoth LEDs toggle alternately with roughly equal frequency due to time-slicing.
DNeither LED toggles because tasks block each other causing deadlock.
Attempts:
2 left
💡 Hint

Think about how FreeRTOS scheduler switches between tasks of equal priority when they call vTaskDelay().

🧠 Conceptual
intermediate
2:00remaining
Effect of preemption on equal priority tasks

In FreeRTOS, if two tasks have the same priority and preemption is enabled, what happens when one task becomes ready while the other is running?

AThe currently running task is immediately preempted and the new task runs.
BThe currently running task continues until it blocks or yields, then the new task runs.
CBoth tasks run simultaneously on separate cores.
DThe scheduler ignores the new task until the running task finishes.
Attempts:
2 left
💡 Hint

Consider how FreeRTOS handles tasks of equal priority with preemption enabled.

🔧 Debug
advanced
2:00remaining
Identify the cause of starvation in equal priority tasks

Two tasks with equal priority are created. Task1 runs an infinite loop without any blocking calls. Task2 never runs. What is the cause?

FreeRTOS
void Task1(void *pvParameters) {
    for (;;) {
        // No blocking or yielding
    }
}

void Task2(void *pvParameters) {
    for (;;) {
        ToggleLED();
        vTaskDelay(10);
    }
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 3, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    for(;;);
}
ATask1 starves Task2 because it never blocks or yields, preventing time-slicing.
BTask2 starves Task1 because it has a delay causing Task1 to run more.
CBoth tasks run equally because they have the same priority.
DThe scheduler crashes due to infinite loops.
Attempts:
2 left
💡 Hint

Think about how FreeRTOS schedules tasks that never block or yield.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in time-slicing task creation

Which option contains a syntax error in creating two equal priority tasks for time-slicing?

A
TaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
B
xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
C
;)LLUN ,2 ,LLUN ,0001 ,"2ksaT" ,2ksaT(etaerCksaTx
;)LLUN ,2 ,LLUN ,0001 ,"1ksaT" ,1ksaT(etaerCksaTx
D
xTaskCreate(Task1, "Task1", 1000, NULL, 2);
xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
Attempts:
2 left
💡 Hint

Check the number of parameters required by xTaskCreate.

🚀 Application
expert
2:00remaining
Calculate CPU time slice for equal priority tasks

In FreeRTOS, two tasks have equal priority and the tick rate is set to 1000 Hz. If each task runs for one tick before switching, how much CPU time (in milliseconds) does each task get per time slice?

A1 millisecond per time slice
B100 milliseconds per time slice
C10 milliseconds per time slice
D0.5 milliseconds per time slice
Attempts:
2 left
💡 Hint

Consider the tick rate and how it relates to time slice duration.

Practice

(1/5)
1. What does time-slicing mean in FreeRTOS when tasks have equal priority?
easy
A. Higher priority tasks are paused to let lower priority tasks run.
B. The CPU time is shared fairly among tasks with the same priority.
C. Tasks with equal priority run one after another without switching.
D. Only the first created task runs until it finishes.

Solution

  1. Step 1: Understand time-slicing concept

    Time-slicing means the CPU divides its time between tasks that have the same priority.
  2. Step 2: Apply to FreeRTOS behavior

    FreeRTOS automatically switches between equal priority tasks to share CPU time fairly.
  3. Final Answer:

    The CPU time is shared fairly among tasks with the same priority. -> Option B
  4. Quick Check:

    Time-slicing = fair CPU sharing [OK]
Hint: Equal priority means tasks share CPU time fairly [OK]
Common Mistakes:
  • Thinking only one task runs until it finishes
  • Believing tasks run sequentially without switching
  • Assuming lower priority tasks pause higher priority ones
2. Which of the following is the correct way to set two tasks to have equal priority in FreeRTOS?
easy
A. xTaskCreate(task1, "Task1", 1000, NULL, 2, NULL); xTaskCreate(task2, "Task2", 1000, NULL, 2, NULL);
B. xTaskCreate(task1, "Task1", 1000, NULL, 1, NULL); xTaskCreate(task2, "Task2", 1000, NULL, 3, NULL);
C. xTaskCreate(task1, "Task1", 1000, NULL, 0, NULL); xTaskCreate(task2, "Task2", 1000, NULL, 1, NULL);
D. xTaskCreate(task1, "Task1", 1000, NULL, 5, NULL); xTaskCreate(task2, "Task2", 1000, NULL, 4, NULL);

Solution

  1. Step 1: Check task priority parameters

    In FreeRTOS, the fifth parameter in xTaskCreate is the task priority.
  2. Step 2: Identify equal priorities

    Both tasks have priority 2 in xTaskCreate(task1, "Task1", 1000, NULL, 2, NULL); xTaskCreate(task2, "Task2", 1000, NULL, 2, NULL);, making them equal priority tasks.
  3. Final Answer:

    Both tasks have priority 2, so they have equal priority. -> Option A
  4. Quick Check:

    Same priority number = equal priority [OK]
Hint: Same priority number means equal priority [OK]
Common Mistakes:
  • Assigning different priority numbers to tasks
  • Confusing task stack size with priority
  • Using priority 0 and 1 as equal
3. Given two tasks with equal priority in FreeRTOS, what is the expected behavior when both are ready to run?
medium
A. Both tasks run simultaneously on the same CPU core.
B. One task runs to completion before the other starts.
C. The second task never runs because the first task blocks it.
D. The scheduler switches between tasks periodically to share CPU time.

Solution

  1. Step 1: Understand FreeRTOS scheduling for equal priority tasks

    FreeRTOS uses time-slicing to switch between tasks of equal priority.
  2. Step 2: Predict scheduler behavior

    The scheduler switches tasks periodically, giving each task CPU time in turn.
  3. Final Answer:

    The scheduler switches between tasks periodically to share CPU time. -> Option D
  4. Quick Check:

    Equal priority tasks share CPU via switching [OK]
Hint: Equal priority tasks get CPU time by switching [OK]
Common Mistakes:
  • Thinking tasks run one after another without switching
  • Assuming tasks run simultaneously on one core
  • Believing one task blocks the other forever
4. You have two tasks with equal priority, but one task never seems to run. What is the most likely cause?
medium
A. The task that never runs has a higher priority number.
B. FreeRTOS does not support time-slicing for equal priority tasks.
C. The task that never runs is blocked or waiting indefinitely.
D. The scheduler is disabled, so no task switching occurs.

Solution

  1. Step 1: Check task states in FreeRTOS

    Even with equal priority, a task that is blocked or waiting will not run.
  2. Step 2: Identify why a task never runs

    If a task never runs, it is likely blocked or waiting indefinitely, not ready to run.
  3. Final Answer:

    The task that never runs is blocked or waiting indefinitely. -> Option C
  4. Quick Check:

    Blocked task = no CPU time [OK]
Hint: Blocked tasks don't get CPU time even if priority is equal [OK]
Common Mistakes:
  • Assuming higher priority task blocks lower priority always
  • Believing FreeRTOS lacks time-slicing
  • Ignoring task states like blocked or suspended
5. You have three tasks with equal priority in FreeRTOS. Task A runs for 10ms, Task B for 20ms, and Task C for 30ms before yielding. How does time-slicing affect their CPU time distribution?
hard
A. Each task gets equal CPU time slices regardless of run duration before yielding.
B. Tasks that run longer before yielding get more CPU time overall.
C. Task C will starve Tasks A and B because it runs longest.
D. FreeRTOS runs tasks in creation order ignoring time-slicing.

Solution

  1. Step 1: Understand time-slicing with equal priority tasks

    FreeRTOS scheduler switches tasks periodically to share CPU time fairly among equal priority tasks.
  2. Step 2: Analyze effect of different run durations before yielding

    Even if tasks run different times before yielding, the scheduler enforces time slices to balance CPU time.
  3. Final Answer:

    Each task gets equal CPU time slices regardless of run duration before yielding. -> Option A
  4. Quick Check:

    Time-slicing ensures fair CPU time despite run length [OK]
Hint: Time-slicing balances CPU time regardless of task run length [OK]
Common Mistakes:
  • Assuming longer running tasks get more CPU time
  • Thinking tasks can starve others with longer runs
  • Believing task creation order controls CPU time
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