void Yield( void )
If there are a number of tasks of equal priority which are all ready to run, the kernel does round-robin scheduling of these tasks. This is a process by which each task is allowed to run for a fixed amount of time before the CPU is taken away from it, and given to another task of the same priority. The amount of time a task is allowed to run before being preempted is called the task's "quantum".
The purpose of the Yield()
function is to let a task voluntarily give up the remaining time of its quantum. Since the time quantum is only an issue when the kernel does round-robin scheduling, it means that Yield()
actually only does something when there are multiple ready tasks at the same priority. However, since the yielding task does not know exactly which task, if any, is going to run next, Yield()
should not be used for implicit communication amongst tasks. The way to cooperate amongst tasks is using signals, messages, and semaphores.
In short, if there are higher-priority tasks in the system, the current task will only run if the higher-priority tasks are all in the wait queue. If there are lower-priority tasks, these will only run if the current task is in the wait queue. And if there are other tasks of the same priority, the kernel automatically cycles through all the tasks, spending a quantum of time on each task, unless a task calls Yield()
, which will cut short its quantum.
If there are no other ready tasks of the same priority as the task that calls Yield()
, then that task will keep running as if nothing happened.
Yield()
is only useful in very specific circumstances. If you plan on using it, think twice. In most cases it is not needed, and using it will result in a decrease in performance.
WaitSignal
()