Hi @mhaza
In a non-RTOS system the program instructions are executed sequentially.
In a RTOS the program's flow is determined by a scheduler. The scheduler is some clever code that checks to see which tasks are in the "ready" state and whether their priority is higher than the task currently in "running" state.
If so, it swiches context, or in other works saves the currently "running" task's position (aka context) to memory, relegating it to the "ready" state. It then loads the context of the highest priority "ready" task from memory, elevating it to the status of "running" state, allowing the task to run from its last know position.
If not, and the current task remains the highest priority, it is allowed to continue executing.
The scheduler itself can be called in a number of different ways:
Firstly, it can be called by a hardware timer interrupt, this can be a standard or watchdog timer (depending on the RTOS implementation). This is known as a "time slice" and allows the scheduler a window of opportunity to periodically "switch context" in order to prioritise the highest priority tasks. The scheduler is essentially called from the timer's interrupt service routine (ISR).
Secondly, it can be called whenever a task currently in the "running" state enters a "blocked" state, for example due to a delay, or to wait for synchronisation with another task, for example a semaphore or some other form of task-to-task based communication. These delay and synchronisation RTOS functions call on the scheduler behind the scenes.
Finally, it can be called whenever a hardware interrupt occurs. Hardware interrupts again provide an opportunity to invoke the scheduler in order to re-prioritise tasks. This is implemented by employing special RTOS functions within the interrupt handler function's ISR itself.
In summary, the RTOS is essentially a software construct that operates at higher level than the underlying hardware. However, hardware interrupts allow the scheduler to call on high priorty, service tasks that override the lower priority time-sliced ones. Thereby creating an interrupt driven, prioritised, multi-tasking system.