Scheduling in FreeRTOS - Pre-Emptive Vs Co-Operative
Scheduling in FreeRTOS - Pre-Emptive Vs Co-Operative
- In this mode, no task can be pre-empted by another task with the same or higher priority unless it
chooses to yield.
Key API:
- In addition, if tasks share the same priority, they will be switched using “Round-Robin
Scheduling”, where each task runs for a fixed amount of time (a time slice), ensuring fair usage of
CPU time.
- If tasks have equal priority, FreeRTOS uses Round-Robin Scheduling to switch between them,
ensuring each task gets a fair share of CPU time.
- Example: If two tasks, A and B, have the same priority, Task A runs for one time slice, then Task B
runs for the next, and this pattern continues.
- In this model, task switching is cooperative rather than enforced by the scheduler.
// Task1 function
for(;;) {
// Task1 logic
printf("Task1 is running\n");
// Task2 function
for(;;) {
// Task2 logic
printf("Task2 is running\n");
int main(void) {
vTaskStartScheduler();
for(;;);
#define configUSE_PREEMPTION 0
// Task1 function
for(;;) {
// Task1 logic
printf("Task1 is running\n");
// Task2 function
for(;;) {
// Task2 logic
printf("Task2 is running\n");
int main(void) {
vTaskStartScheduler();
for(;;);
}
Key APIs for FreeRTOS Scheduling:
- vTaskDelay(): Puts the task into a blocked state for a certain period.