• Home
  • Q&A
  • Integrated Circuits (ICs)
  • What Are the Principles of Priority-Based Task Scheduling?

    * Question

    What Are the Principles of Priority-Based Task Scheduling?

    * Answer

    Priority-based task scheduling is a method used by operating systems and real-time operating systems (RTOSs) to determine which ready task should use the processor. Each task is assigned a priority, and the scheduler normally selects the highest-priority task that is ready to run.

    Highest-Priority Ready Task Runs First

    Only tasks in the ready state compete for processor time. A high-priority task that is delayed, suspended, or waiting for an event does not prevent a lower-priority ready task from running.

    When the high-priority task becomes ready again, it may immediately take control of the processor in a preemptive system.

    Higher-Priority Tasks Can Preempt Lower-Priority Tasks

    In preemptive scheduling, a running task is interrupted when a higher-priority task becomes ready. The operating system saves the current task’s context, runs the higher-priority task, and later resumes the interrupted task.

    This behavior allows time-critical operations to respond quickly to interrupts, messages, timers, and other events.

    Equal-Priority Tasks Share Processor Time

    If multiple ready tasks have the same priority, the scheduler may use:

    • Round-robin scheduling:Each task receives a configurable time slice.
    • First-in, first-out scheduling:The task waiting longest runs first.
    • Cooperative scheduling:The current task continues until it blocks, finishes, or voluntarily yields.

    The exact behavior depends on the operating system and its configuration. For example, Zephyr normally selects the highest-priority ready thread and uses waiting time to choose among threads with the same priority. It can also apply time slicing to equal-priority preemptive threads.

    Blocked Tasks Do Not Consume CPU Time

    A task should enter a waiting or blocked state when it is waiting for a semaphore, message, I/O operation, timer, or another event. The scheduler can then run other ready tasks instead of wasting processor time in a polling loop.

    When the required event occurs, the task returns to the ready state and competes according to its priority.

    Priorities Should Reflect Timing Requirements

    Task priorities should be based on system requirements rather than general importance. Tasks with short response-time requirements or strict deadlines normally receive higher priorities.

    A practical priority order might be:

    1. Safety-critical or hard real-time control
    2. Time-sensitive data acquisition and communication
    3. Normal control and application processing
    4. Logging, display updates, and background maintenance
    5. The idle task

    High-priority tasks should perform only essential work and then block or yield. A high-priority task that continuously remains ready can prevent lower-priority tasks from running, causing task starvation.

    Shared Resources Require Special Protection

    Priority inversion can occur when a high-priority task waits for a resource held by a lower-priority task while medium-priority tasks continue running. RTOS mutexes may use priority inheritance, temporarily raising the resource owner’s priority so it can release the resource sooner.

    Critical sections should remain short, and the appropriate mutex, semaphore, queue, or event mechanism should be selected for each shared resource.

    Effective priority scheduling therefore depends on more than assigning priority numbers. Designers must also consider task states, preemption, execution time, deadlines, resource sharing, interrupt behavior, and the possibility of starvation or priority inversion.