Tasks and Sounds
With the possibility of needing to monitor several input sensors, we introduce the notion oftasks. Each task is an independent program (think task main()), and, more importantly,
the tasks can run concurrently. Concurrent processes, also known as threads, involve forking
the program execution into several parallel threads that each run independently.
8.1 Tasks
We have used the task syntax all along when writing our programs. A task is an independent
program thread designed to be executed simultaneously, or in parallel, with the main task.
There are many kinds of parallelism in computer science{indeed there are whole books on
the topic. Pseudo parallelism, the kind we have in RobotC, is the simplest and oldest
form of parallelism. In pseudo parallelism the CPU's computational power, the number of
calculations it can do per second, is sliced up according to some prioritization in a process
called time-division multiplexing.
In a time-division multiplexing scheme, suppose that a CPU can do 100,000 calculations
per second and suppose that there are 4 tasks of equal priority and equal computational
requirements. Then, each second, the CPU will dedicate 25,000 calculations to each task.
Unfortunately, this simple situation rarely occurs.
More often, the tasks, which may include intense loops and leisurely waits, require dier-
ent amounts of work at dierent times. When the CPU decides to dedicate some calculations
to a particular task it may just so happen that the task is in a wait state and does not require
any calculations. A smart allocation of CPU calculations would have the CPU immediately
move on to another task rather than wait for the current task to start requiring calculations
again. After all, it is possible that the CPU can attend to the other tasks and return the
waiting task before it actually requires calculations.
In RobotC, all tasks are given equal priority by default. If two tasks of equal priority
require attention, the CPU will work on the task it has been away from longest, but only for
a time-slice equal to the task's share. After it receives its allotted calculations, whether it has
nished its task or not, the CPU will move to the next task requiring attention. There are
advanced prioritization schemes possible with RobotC summarized in the RobotC On-line
Support on the left side-bar under the NXT Functions ! Task Control section.


No comments:
Post a Comment