π Introduction #
One of the defining features of a real-time operating system (RTOS) like VxWorks is its deterministic scheduling.
In VxWorks, tasks are scheduled primarily by priority using a preemptive, priority-based scheduler.
This means:
- Higher-priority tasks always run before lower-priority tasks.
- If multiple tasks have the same priority, they run in round-robin fashion.
Understanding this is crucial for building predictable and reliable embedded systems.
π§© How Task Scheduling Works in VxWorks #
- Priorities Range: 0 (highest) β 255 (lowest).
- Preemption: A higher-priority task can interrupt a running lower-priority task.
- Round-Robin: Tasks with the same priority share CPU time if time-slicing is enabled.
- Blocking: Tasks that wait on semaphores, message queues, or delays yield CPU.
π» Example: Task Priorities in Action #
Letβs see how different priorities affect execution.
Code Example #
#include <vxWorks.h>
#include <taskLib.h>
#include <stdio.h>
void highPriorityTask()
{
while (1)
{
printf("High priority task running\n");
taskDelay(50); // delay ~0.5s
}
}
void mediumPriorityTask()
{
while (1)
{
printf("Medium priority task running\n");
taskDelay(100); // delay ~1s
}
}
void lowPriorityTask()
{
while (1)
{
printf("Low priority task running\n");
taskDelay(150); // delay ~1.5s
}
}
void usrAppInit(void)
{
taskSpawn("tHigh", 50, 0, 4000, (FUNCPTR)highPriorityTask,
0,0,0,0,0,0,0,0,0,0);
taskSpawn("tMed", 100, 0, 4000, (FUNCPTR)mediumPriorityTask,
0,0,0,0,0,0,0,0,0,0);
taskSpawn("tLow", 150, 0, 4000, (FUNCPTR)lowPriorityTask,
0,0,0,0,0,0,0,0,0,0);
}
π Explanation of the Code #
-
Three tasks created:
tHighat priority 50 (higher priority).tMedat priority 100.tLowat priority 150 (lower priority).
-
Execution Order:
- The high-priority task always gets CPU time first.
- Medium and low tasks run only when higher-priority tasks are waiting or delayed.
-
Output:
High priority task running Medium priority task running Low priority task running High priority task running ...Youβll notice that the high-priority task dominates execution, while others get CPU time when it delays.
βοΈ Round-Robin Scheduling (Same Priority) #
If multiple tasks share the same priority, they run in a round-robin manner if time-slicing is enabled.
// Enable round-robin time slicing (system-wide setting)
kernelTimeSlice(20); // 20 ticks per slice
This ensures fair sharing among tasks with the same priority.
π Key Takeaways #
- VxWorks uses preemptive, priority-based scheduling.
- Higher-priority tasks always run before lower-priority ones.
- Tasks at the same priority can use round-robin scheduling with time-slicing.
- Proper priority assignment is critical to ensure real-time guarantees.
β Wrap-Up #
In this tutorial, you learned:
- How task priorities affect scheduling in VxWorks.
- How preemption and round-robin scheduling work.
- How to control execution order with
kernelTimeSlice().
In the next blog, weβll explore Priority Inversion and How VxWorks Solves It β a crucial concept in real-time systems.