Page 379 - ARM Based Microcontroller Projects Using MBED
P. 379
15.2 TASK SCHEDULING 365
NextTask ¼ 1;
while(1)
{
switch(NextTask)
{
case 1:
Task1 code
NextTask ¼ 2;
break;
case 2:
Task2 code
NextTask ¼ 3;
break;
case 3:
Task3 code
NextTask ¼ 4;
break;
case 4:
Task4 code
NextTask ¼ 1;
break;
}
}
15.2.2 Preemptive Scheduling
In a preemptive scheduling once the CPU is given to a task it can be taken away, for example
when ahigherpriority task wants the CPU.Preemptivescheduling isused in real-time systems
where the tasks are usually configured with different priorities and time critical tasks are given
higher priorities. A higher priority task can stop a lower priority one and grab and use the CPU
until it releases it. In preemptive scheduling the task contexts are saved so that the tasks can
resume their operations from the point they left when they are given back the CPU.
Preemptive scheduling is normally implemented in two different ways: using Round Robin
(RR) scheduling, or using interrupt-based (IB) scheduling.
In RR scheduling all the tasks are given equal amount of CPU times and tasks do not have
any priorities. When the CPU is to be given to another task, the context of the current task is
saved and the next task is started. The task context is restored when the same task gets control
of the CPU. RR scheduling has the advantage that all the tasks get equal amount of the CPU
time. It is however not suitable in real-time systems since a time critical task cannot get hold of
the CPU when it needs to. Also, a long task can be stopped before it completes its operations.
Fig. 15.2 shows an example RR type scheduling with three tasks.
In IB scheduling tasks may be given different priorities and the task with the highest pri-
ority gets hold of the CPU. Tasks with same priorities are executed with RR type scheduling
where they are all given equal amount of CPU time. IB scheduling is best suited to real-time
systems where time critical tasks are given higher priorities. The disadvantages of IB