Round-Robin Scheduling
When two or more tasks have the same priority, µC/OS-III allows one task to run for a predetermined amount of time (called a Time Quanta) before selecting another task. This process is called Round-Robin Scheduling or Time Slicing. If a task does not need to use its full time quanta it can voluntarily give up the CPU so that the next task can execute. This is called Yielding. µC/OS-III allows the user to enable or disable round robin scheduling at run time.
The figure below shows a timing diagram with tasks running at the same priority. There are three tasks that are ready-to-run at priority “X”. For sake of illustration, the time quanta occurs every 4th clock tick. This is shown as a darker tick mark.
(1) Task #3 is executing. During that time, a tick interrupt occurs but the time quanta have not expired yet for Task #3.
(2) On the 4th tick interrupt, the time quanta for Task #3 expire.
(3) µC/OS-III resumes Task #1 since it was the next task in the list of tasks at priority “X” that was ready-to-run.
(4) Task #1 executes until its time quanta expires (i.e., after four ticks).
(5)
(6)
(7) Here Task #3 executes but decides to give up its time quanta by calling the µC/OS-III function OSSchedRoundRobinYield()
, which causes the next task in the list of tasks ready at priority “X” to execute. An interesting thing occurred when µC/OS-III scheduled Task #1. It reset the time quanta for that task to four ticks so that the next time quanta will expire four ticks from this point.
(8) Task #1 executes for its full time quanta.
µC/OS-III allows the user to change the default time quanta at run time through the OSSchedRoundRobinCfg()
function (see Appendix A, “µC/OS-III API Reference”). This function also allows round robin scheduling to be enabled/disabled, and the ability to change the default time quanta.
µC/OS-III also enables the user to specify the time quanta on a per-task basis. One task could have a time quanta of 1 tick, another 12, another 3, and yet another 7, etc. The time quanta of a task is specified when the task is created. The time quanta of a task may also be changed at run time through the function OSTaskTimeQuantaSet()
.