Context switching (Re: [Techtalk] Priority inheritance)

Mary Gardiner linuxchix at puzzling.org
Sat Jan 19 11:50:13 EST 2002


On Fri, Jan 18, 2002 at 02:53:03PM -0500, Raven, corporate courtesan wrote:
> 	Okay -- I get the semaphore, but could you write a bit on what a
> context switch is?

It's nothing more than one process stopping running and another
starting. On a multiprocess system like Linux this happens thousands of
times per second. Every time it happens, one process's register[1]
values are moved to the appropriate registers, the pointers to the
memory location to read instructions from is moved to the appropriate
place, some other pointers to memory locations of the active processes
data are moved around. The vales for the old process are copied into
RAM, and that process goes back in the queue to be scheduled again.

It is controlled by a "scheduler". The scheduler must maintain a list of
active processes (processes who are waiting for slow things such as hard
disk accesses, or worse, input from the keyboard or the network, go into
a waiting state and will only be scheduled when their data has arrived)
and determine which one to run. It can be a simple as letting a single
process run until it finishes, or goes into wait, but I bet you can
imagine the problems with that! It is generally more complex, and must
take into account priorities, and often deadlines (you don't want the
decoding of the next bit of your MP3 to take more than a tenth of a
second, say).

Of course the scheduler itself must be context-switched in, and consumes
resources. It is the teller in the original analogy.

-Mary.

[1] The register is a set of very very fast value small pieces of memory
in the CPU. Before the CPU can execute a command on a piece of data,
that piece of data must be moved from the process's memory in RAM to the
registers.

-- 
Mary Gardiner
<mary at puzzling.org>



More information about the Techtalk mailing list