Page 378 - ARM Based Microcontroller Projects Using MBED
P. 378

364                             15. Mbed RTOS PROJECTS

                 15.2.1 Nonpreemptive Scheduling
                   Nonpreemptive scheduling is the simplest form of task scheduling in a multitasking sys-
                 tem. Here, once a task is given the CPU, the CPU cannot be taken away from that task. It is up
                 to the task to give away the CPU and this usually happens when the task completes its op-
                 erations, or when the task is waiting for some external resources and thus cannot continue.
                 Nonpreemptive scheduling is also called Cooperative Scheduling. Some characteristics of coop-
                 erative scheduling are:
                 • In cooperative scheduling short tasks have to wait for long tasks to complete before they
                   can grab the CPU. In extreme cases short tasks can never be executed if the long tasks do
                   not release the CPU.
                 • In cooperative scheduling there is no task priority and tasks that require immediate
                   attention have to wait until other tasks release the CPU. This may not be desirable in many
                   real-time applications where immediate attention of the CPU may be required, for
                   example, when an important and highly risky external event occurs (e.g., very high
                   temperature, alarm, etc.).
                 • It is important to make sure that a task does not block, for example, waiting for an external
                   event to occur. If a task blocks then none of the other tasks can run in the system. A blocking
                   task should release the CPU and wait for its turn to come again to check whether or not the
                   blocking condition still exists.
                   Because of its nature, cooperative scheduling-based multitasking is not used in real-time
                 systems where immediate attention of the CPU may be required. Fig. 15.1 shows an example
                 cooperative scheduling of three tasks. Task1 takes the longest time and when it releases the
                 CPU then Task2 starts. Task3 starts after Task2 completes its processing. CPU is given back to
                 Task1 after Task3 releases it. Depending on the algorithm used, the context of a task may or
                 may not be saved. Saving the context of a task enables the task to return and continue from the
                 point where it released the CPU.
                   Perhaps the simplest way to implement cooperative scheduling in a program is to use a
                 state diagram approach where the tasks can be selected using a switch statement as in the
                 following skeleton code. In this example there are four tasks which are selected sequentially
                 one after the other. Note that in this example the task context is not saved and tasks start from
                 the beginning of their codes:


















                 FIG. 15.1  Example cooperative scheduling with three tasks.
   373   374   375   376   377   378   379   380   381   382   383