The diagram below demonstrates how the tasks defined on the previous page would be scheduled by a real time operating system. The RTOS has itself created a task – the idle task – which will execute only when there are no other tasks able to do so. The RTOS idle task is always in a state where it is able to execute.
Referring to the diagram above:
At the start neither of our two tasks are able to run – vControlTask is waiting for the correct time to start a new control cycle and vKeyHandlerTask is waiting for a key to be pressed. Processor time is given to the RTOS idle task.
At time t1, a key press occurs. vKeyHandlerTask is now able to execute – it has a higher priority than the RTOS idle task so is given processor time.
At time t2 vKeyHandlerTask has completed processing the key and updating the LCD.
It cannot continue until another key has been pressed so suspends itself and the RTOS idle task is again resumed.
At time t3 a timer event indicates that it is time to perform the next control cycle. vControlTask can now execute and as the highest priority task is scheduled processor time immediately.
Between time t3 and t4, while vControlTask is still executing, a key press occurs. vKeyHandlerTask is now able to execute, but as it has a lower priority than vControlTask it is not scheduled any processor time.
At t4 vControlTask completes processing the control cycle and cannot restart until the next timer event – it suspends itself. vKeyHandlerTask is now the task with the highest priority that is able to run so is scheduled processor time in order to process the previous key press.
At t5 the key press has been processed, and vKeyHandlerTask suspends itself to wait for the next key event. Again neither of our tasks are able to execute and the RTOS idle task is
scheduled processor time.
Between t5 and t6 a timer event is processed, but no further key presses occur.
The next key press occurs at time t6, but before vKeyHandlerTask has completed processing the key a timer event occurs. Now both tasks are able to execute. As vControlTask has the higher priority vKeyHandlerTask is suspended before it has completed processing the key, and vControlTask is scheduled processor time.
At t8 vControlTask completes processing the control cycle and suspends itself to wait for the next. vKeyHandlerTask is again
the highest priority task that is able to run so is scheduled processor time so the key press processing can be completed.