Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Preemptive scheduling behavior in FreeRTOS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the FreeRTOS scheduler.

FreeRTOS
vTaskStartScheduler[1];
Drag options to blanks, or click blank then click option'
A()
B{}
C[]
D;
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the parentheses and writing just the function name.
Adding curly braces instead of parentheses.
2fill in blank
medium

Complete the code to create a task with priority 2.

FreeRTOS
xTaskCreate(TaskFunction, "Task1", 1000, NULL, [1], NULL);
Drag options to blanks, or click blank then click option'
A2
B1
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using priority 0 which is the lowest priority.
Confusing the stack size with priority.
3fill in blank
hard

Fix the error in the task delay function to yield control correctly.

FreeRTOS
vTaskDelay([1]);
Drag options to blanks, or click blank then click option'
AportTICK_PERIOD_MS
B1000
C1000 * portTICK_PERIOD_MS
D1000 / portTICK_PERIOD_MS
Attempts:
3 left
💡 Hint
Common Mistakes
Passing milliseconds directly without conversion.
Multiplying instead of dividing by portTICK_PERIOD_MS.
4fill in blank
hard

Fill both blanks to create a preemptive scheduler configuration.

FreeRTOS
configUSE_PREEMPTION = [1];
configMAX_PRIORITIES = [2];
Drag options to blanks, or click blank then click option'
A1
B0
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Setting configUSE_PREEMPTION to 0 disables preemption.
Choosing too few or too many priorities.
5fill in blank
hard

Fill all three blanks to correctly implement a task function that yields control.

FreeRTOS
void TaskFunction(void *pvParameters) {
    for(;;) {
        // Task code here
        [1]();
        [2](10 / [3]);
    }
}
Drag options to blanks, or click blank then click option'
AvTaskDelay
BvTaskDelayUntil
CportTICK_PERIOD_MS
DtaskYIELD
Attempts:
3 left
💡 Hint
Common Mistakes
Using vTaskDelayUntil without proper tick count variable.
Forgetting to convert milliseconds to ticks.

Practice

(1/5)
1. What happens in FreeRTOS when a higher priority task becomes ready while a lower priority task is running under preemptive scheduling?
easy
A. The scheduler ignores the higher priority task until the lower priority task yields.
B. The lower priority task continues running until it finishes.
C. Both tasks run at the same time.
D. The higher priority task immediately preempts the lower priority task and starts running.

Solution

  1. Step 1: Understand preemptive scheduling behavior

    In preemptive scheduling, the system always runs the highest priority task that is ready.
  2. Step 2: Apply to the scenario

    When a higher priority task becomes ready, it interrupts the lower priority task immediately.
  3. Final Answer:

    The higher priority task immediately preempts the lower priority task and starts running. -> Option D
  4. Quick Check:

    Preemptive scheduling = immediate switch to higher priority task [OK]
Hint: Higher priority tasks always interrupt lower ones immediately [OK]
Common Mistakes:
  • Thinking tasks run simultaneously on a single core
  • Believing lower priority tasks finish before switching
  • Confusing preemptive with cooperative scheduling
2. Which of the following is the correct way to enable preemptive scheduling in FreeRTOS configuration?
easy
A. #define configUSE_PREEMPTION 1
B. #define configENABLE_PREEMPTION true
C. #define configPREEMPTIVE_SCHEDULING 1
D. #define configUSE_PREEMPTION 0

Solution

  1. Step 1: Recall FreeRTOS config option for preemption

    The correct macro to enable preemption is configUSE_PREEMPTION.
  2. Step 2: Identify correct value to enable preemption

    Setting configUSE_PREEMPTION to 1 enables preemptive scheduling.
  3. Final Answer:

    #define configUSE_PREEMPTION 1 -> Option A
  4. Quick Check:

    configUSE_PREEMPTION = 1 enables preemption [OK]
Hint: Enable preemption by setting configUSE_PREEMPTION to 1 [OK]
Common Mistakes:
  • Using 0 instead of 1 to enable preemption
  • Using wrong macro names
  • Using boolean true instead of 1
3. Consider two tasks in FreeRTOS: TaskA with priority 2 and TaskB with priority 3. TaskA is running when TaskB becomes ready. What will be the output if both tasks print their names when running?
medium
A. TaskA prints first, then TaskB prints after TaskA finishes.
B. TaskB prints immediately, preempting TaskA.
C. Both tasks print simultaneously.
D. No output because tasks block each other.

Solution

  1. Step 1: Identify task priorities and running state

    TaskA has priority 2 and is running; TaskB has higher priority 3 and becomes ready.
  2. Step 2: Apply preemptive scheduling rules

    TaskB will preempt TaskA immediately and run first, printing its name.
  3. Final Answer:

    TaskB prints immediately, preempting TaskA. -> Option B
  4. Quick Check:

    Higher priority task runs first = TaskB prints first [OK]
Hint: Higher priority task prints first by preempting lower priority [OK]
Common Mistakes:
  • Assuming lower priority task finishes before switching
  • Thinking tasks run at the same time
  • Ignoring task priorities
4. Given this FreeRTOS snippet, what is the error that prevents preemptive scheduling from working?
#define configUSE_PREEMPTION 0

void Task1(void *pvParameters) {
  while(1) {
    // Task code
  }
}

void Task2(void *pvParameters) {
  while(1) {
    // Task code
  }
}
medium
A. configUSE_PREEMPTION is set to 0, disabling preemption.
B. Tasks are missing priority assignments.
C. Task functions must return int, not void.
D. Tasks need to call vTaskDelay to allow switching.

Solution

  1. Step 1: Check configUSE_PREEMPTION value

    The macro is set to 0, which disables preemptive scheduling.
  2. Step 2: Understand effect on scheduling

    With preemption disabled, tasks will not be interrupted automatically by higher priority tasks.
  3. Final Answer:

    configUSE_PREEMPTION is set to 0, disabling preemption. -> Option A
  4. Quick Check:

    configUSE_PREEMPTION = 0 disables preemption [OK]
Hint: Preemption disabled if configUSE_PREEMPTION = 0 [OK]
Common Mistakes:
  • Thinking tasks must return int
  • Assuming missing priorities cause preemption failure
  • Believing vTaskDelay is required for preemption
5. You have three tasks: TaskX (priority 2), TaskY (priority 3), and TaskZ (priority 3). TaskX is running, then both TaskY and TaskZ become ready at the same time. Which task will FreeRTOS run first under preemptive scheduling, and why?
hard
A. TaskY runs first because it was created before TaskZ.
B. TaskZ runs first because it has the same priority as TaskY but higher than TaskX.
C. Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities.
D. TaskX continues running until it yields.

Solution

  1. Step 1: Identify task priorities and ready state

    TaskX priority 2 is running; TaskY and TaskZ both have priority 3 and become ready simultaneously.
  2. Step 2: Understand FreeRTOS behavior with equal priority tasks

    FreeRTOS uses time slicing (round-robin) between tasks of equal highest priority.
  3. Step 3: Determine which task runs first

    Either TaskY or TaskZ can run first; the scheduler switches between them fairly.
  4. Final Answer:

    Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities. -> Option C
  5. Quick Check:

    Equal priority tasks share CPU time via time slicing [OK]
Hint: Equal priority tasks share CPU time by time slicing [OK]
Common Mistakes:
  • Assuming creation order decides first run
  • Thinking lower priority task continues running
  • Believing one equal priority task always runs first
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