Computing
book Well I'm now on holiday for the rest of July 2001, but it isn't really
a holiday, as I must use it to catch up on my writing. I've decided that I've
spent too long writing about detail, and for all my future books I'm going to
try and make the subject as interesting as possible to read, and still try and
cover the main principles. For my new Computing book I've engineered ever single
word, and tried to make the whole book flow from start to finish. An example (although
it's a bit simplistic) is the coverage of binary numbers. In the past I've just
launched straight into binary theory, and for this book I've decided to tell a
little story first, before introducing the main concepts. He's the first, unedited,
version:
Imagine
that you are an army scout who's job it is to sit on a hill and watch for an attacking
army. For this you have been a set of two flags: a red flag and a green flag,
which are inserted into a single flagpole. From this you can represent two different
conditions (or states). The green flag could represent that there were no armies
approaching, and the red flag would represent that there was an army advancing
on the city. Thus:
Red
flag - Approaching army.
Green flag - No approaching army.
Now, this does not
give much information on the size of the army, at all, or if the approaching army
is an aggressive one or a friendly one. Thus if we used two flags we could represent
four different conditions:
Red flag,
red flag - approaching large aggressive army. Get troops ready for battle.
Red flag, green flag - approaching small aggressive army. Put troops on standby.
Green flag, red flag - approaching friendly army. Setup greeting parade.
Green flag, green flag - no approaching army.
It
can be seen that we have to decide which is the flags is more significant than
the other; as we need to differentiate between a Green, Red and a Red, Green condition.
This we could signify that the flag on the left-hand side is more significant
than the flag at the right-hand side, as this flag represents that there is an
aggressive army approaching. This flag would be seen as the most-significant flag.
Now let us represent the flags with either R (for red), and G (for green). Thus
the states now become:
RR - approaching
large aggressive army. Get troops ready for battle.
RG - approaching
small aggressive army. Put troops on standby.
GR - approaching
friendly army. Setup greeting parade.
GG - no approaching army.
The
number of flags that we have thus determines the number of conditions that we
can represent. If we only have one color of flag, then we can only represent two
states with one flag, four states with two flags, eight states with three flags
(RRR, RRG, RGR, RGG, . GGR and GGG), and so on. This type of representation with
two conditions for each representation is known as binary. In computer systems
the conditions for each representation is a '0' and a '1' (or sometimes as TRUE
or FALSE). Thus, in binary, we could represent the red flag with a '1', and the
green flag with a '0'. Our conditions (or states) are now: 00 (no approaching
army); 01 (approaching friendly army); 10 (approaching small aggressive army);
and 11 (approaching aggressive army).
So what's the main principles that I've covered here? Well it's
the basic concept of binary having two states for each digit, and the concept
of changeover between states in binary values. I think that most people would
have few problems with an army scout standing on a hill with two flags, than they
would with the concept of 0's and 1's. Also I've made it a bit more interesting
to read, as the reader will fill their head with pictures of a little scout sitting
on a hill, signalling to his city about approaching armies.
So
what?
A particular problem that we might
have when we are signaling the information about the army is when our flags are
changing. For example, say that we where currently in the condition of GG, and
a large aggressive army started to approach. The scout would then have to change
the flags from GG to RR. This will take two changes before he can reflect the
new condition. How would he do it? If he changed the most-significant flag first,
he would signal that there was an approaching friendly army, and the city would
get a greeting parade ready, only then to be told that there was a large aggressive
army approaching. This would obviously confuse everyone in the city. If he changed
the least significant flag, then the troops would be put on standby, only to be
told that they would immediately be put on full alert. The problem we have is
that we need some way to change the state of the flags, so that intermediate values
are not taken as final values. Thus one way to do this would be to hide the changeover
of the flags. This also occurs in a computer system where values of the binary
digits changes, and these should not be taken as valid values. This is overcome
with clock signals and handshaking lines, which are used to define when the values
on the computer's bus are actually valid, or not.
Here's another,
on the definition of threads:
The logical
extension to multitasking programs is to split a program into a number of parts
(threads) and run each of these on the multitasking system (multi-threading).
Multi-threading can be likened to splitting sequential tasks, into a number of
interrelated tasks. For example, let's say that you have to cook a meal with the
following recipe:
1. Put potatoes on to boil. 2. Put pie in the microwave,
on HIGH for 10 minutes. 3. Wait for potatoes to become soft. 4. Take potatoes
out of pan, and place on the plates. 5. Wait for pie to complete. 6. Take
pie out of microwave and place on the plates. 7. Put carrots in pan, and boil.
8. Wait for carrots to become soft. 9. Take carrots out of pan, and place
on the plates.
The
problem with this recipe is that someone could be waiting at step 4 to complete,
while they could be checking the pie to see if it has complete. An improved method
would use independent subtask, which were interrelated. In this case the pie and
the potatoes are not interrelated, but the potatoes and the carrots are (assuming
that we only have one pan). Figure 3.9 shows a possible schedule using subtasks
(or threads of the main task), where we now have six main threads. Each of the
threads can run independently, but some cannot run to until they have received
something from another thread. For example it is not possible for the 'Put potatoes
on plate' thread to start until it has received the potatoes from the 'Boil potatoes'
thread.
A threads-based approach allows us to create specialized task,
which have a small defini-tive goal. It is much easier to test a small program
which has a definitive task, rather than to test a large and complex piece of
software. It is also easier to upgrade a thread, without af-fecting the overall
operation of the complete program.