Tasks within an operating system SW have state to determine task that are running, ready or waiting. There shall be only one task running in real time, while other task are ready if they are just waiting for the running task to finish or they are waiting for an external event to be ready to run. The scheduler is the only one with the ability to move task from ready to run state as it determines the priority, periodicity and deadlines of the set of tasks.
When a task is finishing, then the scheduler has a queue of ready task that are sorted by priority. The scheduler selects the higher priority and makes the context switch to change the task to run. Due real time reasons, this context switch shall be as faster as possible, so this context switch code is generally in assembly and Hw associated.
The task priority setting shall be as thoughtful as possible due that can cause different failures when the CPU overhead start reaching its limit (… for example). The task context (state of the task from the processor) contains the task priority, but also some other attributes as the current state of the task or the task identification for example. If a system had definitions of too many high priority tasks or high priority tasks that block the schedule (… for example), some tasks of lower priority might never be executed, producing some failures in the system. To solve the processor can be changed to a faster one or use a better priority and design in the tasks (make the highest priority to the shortest tasks – static scheduler).