[previous]
[index]
[next]
Example 2: Pure Periodic Scheduling of Two Tasks
This example demonstrates two periodic tasks, one that toggles the
speaker port and another that varies the frequency of toggling.
In this example we will expand the number of tasks and use a simple
shared variable. Refer to the commented
source code of the example for the details.
Principle of Operation
- Any number of tasks may be scheduled, up to memory limits (there
is no predefined maximum)
- Tasks may share variables, since they share the same address space
- In general, some care must be taken to ensure "mutual exclusion,"
so that one task's actions on shared data aren't interrupted by
another's
- For small data types (char, int) that are acted on "atomically,"
i.e., whose reads and writes can't be interrupted, no worries
- For large types (structures, arrays), you need to use semaphores
or other mechanisms, described in Example 6: Shared Memory
- For this example, one task runs at a fixed 100 microsecond period,
and toggles the speaker port at intervals determined by a delay
count. The larger the count, the lower the audible frequency.
- A second task runs at a fixed 1 second period, and changes the
delay count each time, so you will hear the frequency change each second.
- The delay count is a shared variable, an 'int', so no worries
about interrupted reads/writes
- Since the task timing need not vary, the tasks both run in pure
periodic mode, using the shortest period of the two tasks as the base
- The frequency will continue to drop indefinitely as the delay
count increases. The 'run' script in the example directory lets it go
for 10 seconds.
Setting up the Tasks
- With two or more periodic tasks, you'll need to determine the base
period, multiples of which will be allowable for each task's period.
- In our case, we have a 100 microsecond task and a 1 second task,
so the base period is 100 microseconds.
- In general, you'll need to find the greatest common divisor of all
your desired task periods.
- Practically speaking, the lower bound on the base period is about
10 microseconds.
- The tasks are scheduled according to priority. If a task is ready
to run (its period has transpired), it can only interrupt tasks of
lower priority. A higher priority task will run its cycle to
completion.
- Recall from Example 1 that the lowest priority is
RT_LOWEST_PRIORITY, a large positive number, and that priorities are
higher as the numbers get lower, down to 0 as the highest-priority
task.
- In our case, we'll run the slowest task at
RT_LOWEST_PRIORITY, and the fastest task at RT_LOWEST_PRIORITY-1.
Running the Demo
To run the demo, change to the 'ex02_twoper' subdirectory of the
top-level tutorial directory, and run the 'run' script by typing
./run
Alternatively, change to the top-level tutorial directory and run the
'runall' script there by typing
./runall
and selecting the "Two Periodic Tasks" button.
You'll hear a series of tones for about 10 seconds, starting at 5
KHz acoustically and dropping in frequency as the second task
increases the delay time used by the first.
See the Code
Next: Example 3, Variable Task Timing
Back: Example 1, A Single Periodic Task