User`s manual

Dynamic C Users Manual digi.com 53
5. MULTITASKING WITH DYNAMIC C
In a multitasking environment, more than one task (each representing a sequence of operations) can
appear to execute in parallel. In reality, a single processor can only execute one instruction at a time. If an
application has multiple tasks to perform, multitasking software can usually take advantage of natural
delays in each task to increase the overall performance of the system. Each task can do some of its work
while the other tasks are waiting for an event, or for something to do. In this way, the tasks execute almost
in parallel.
There are two types of multitasking available for developing applications in Dynamic C: preemptive and
cooperative. In a cooperative multitasking environment, each well-behaved task voluntarily gives up con-
trol when it is waiting, allowing other tasks to execute. Dynamic C has language extensions, costatements
and cofunctions, to support cooperative multitasking.
Preemptive multitasking is supported by the slice statement, which allows a computation to be divided into
small slices of a few milliseconds each, and by the µC/OS-II real-time kernel.
5.1 Cooperative Multitasking
In the absence of a preemptive multitasking kernel or operating system, a programmer given a real-time
programming problem that involves running separate tasks on different time scales will often come up
with a solution that can be described as a big loop driving state machines.
Figure 5-1 Big Loop
Within this endless loop, tasks are accomplished by small fragments of a program that cycle through a
series of states. The state is typically encoded as numerical values in C variables.
State machine
State machine
State machine
Top of loop