Skip to main content

Task Priorities and Scheduling in VxWorks

·420 words·2 mins
VxWorks RTOS Task Priorities Scheduling Real-Time Systems Embedded Systems Programming Tutorial
Table of Contents
VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 17: This Article

πŸš€ 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
#

  1. Priorities Range: 0 (highest) β†’ 255 (lowest).
  2. Preemption: A higher-priority task can interrupt a running lower-priority task.
  3. Round-Robin: Tasks with the same priority share CPU time if time-slicing is enabled.
  4. 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
#

  1. Three tasks created:

    • tHigh at priority 50 (higher priority).
    • tMed at priority 100.
    • tLow at priority 150 (lower priority).
  2. 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.
  3. 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.

VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 17: This Article

Related

Understanding Tasks and Scheduling in VxWorks
·423 words·2 mins
VxWorks RTOS Tasks Scheduling Embedded Systems Programming Tutorial
Watchdog Timers in VxWorks: Monitoring Task Health
·457 words·3 mins
VxWorks RTOS Watchdog Timers Task Monitoring Embedded Systems Programming Tutorial
Signals in VxWorks: Asynchronous Notifications Between Tasks
·401 words·2 mins
VxWorks RTOS Signals IPC Asynchronous Embedded Systems Programming Tutorial