7. How does my computer do several things at once?
It doesn't, actually. Computers can only do one task (or
process) at a time. But a computer can change tasks
very rapidly, and fool slow human beings into thinking it's doing several
things at once. This is called
timesharing.
One of the kernel's jobs is to manage timesharing. It has a part
called the
scheduler
which keeps information inside itself about all the other (non-kernel)
processes in your zoo. Every 1/60th of a second, a timer goes off in the
kernel, generating a clock interrupt. The scheduler stops whatever process
is currently running, suspends it in place, and hands control to another
process.
1/60th of a second may not sound like a lot of time. But on today's
microprocessors it's enough to run tens of thousands of machine
instructions, which can do a great deal of work. So even if you have many
processes, each one can accomplish quite a bit in each of its
timeslices.
In practice, a program may not get its entire timeslice. If an
interrupt comes in from an I/O device, the kernel effectively stops the
current task, runs the interrupt handler, and then returns to the current
task. A storm of high-priority interrupts can squeeze out normal
processing; this misbehavior is called thrashing and
is fortunately very hard to induce under modern Unixes.
In fact, the speed of programs is only very seldom limited by the
amount of machine time they can get (there are a few exceptions to this
rule, such as sound or 3-D graphics generation). Much more often, delays
are caused when the program has to wait on data from a disk drive or
network connection.
An operating system that can routinely support many simultaneous
processes is called "multitasking". The Unix family of operating
systems was designed from the ground up for multitasking and is very good
at it — much more effective than Windows or the old Mac OS, which
had multitasking bolted into them as an afterthought and do it rather poorly.
Efficient, reliable multitasking is a large part of what makes Linux
superior for networking, communications, and Web service.