0
0
FreeRTOSprogramming~20 mins

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

Choose your learning style9 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.