Information Technology Reference
In-Depth Information
4.4.3
Thread context switch
When the kernel has multiple threads, we need a mechanism to switch which
threads areRunningand which areReady.
A thread context switch suspends execution of the currently running thread
Denition: thread context
switch
and resumes execution of some other thread. This is done by copying the cur-
rently running thread's registers from the processor to the thread's TCB and
then copying the other thread's registers from that thread's TCB to the proces-
sor.
The more things change, the more they stay the same. We've already
seen in Chapter 2 how an interrupt, an exception, or a system call trap can
cause a mode switch that suspends a running process, runs a kernel handler,
and returns to running the original process.
Recall that to implement a mode switch, the hardware saves some of the
registers of the currently running process, switches to kernel mode, and starts
running the handler. Then the handler software saves the remaining registers,
handles the event, and nally restores the suspended process's registers and
mode to resume running the suspended process exactly where it left off.
The mechanism for switching between threads in the kernel is almost identi-
cal. For example, if an interrupt or exception occurs: the hardware saves some
of the registers of the currently running process and starts running a handler;
the handler then saves the remaining registers, and begins running the main
body of the handler.
Then, when the handler is done running, instead of resuming the interrupted
process, it can invoke the scheduler to choose a different thread to run rather
than restoring the thread that was interrupted. This is easy to do: we have
already saved the state of the interrupted thread and we have a ready list of
other threads whose state has already been saved. We can copy the interrupted
thread's state to its TCB and copy the state of some ready thread from its TCB
to the registers; then our thread switch is done.
The devil is in the details. There are some small differences between the
two cases, but they amount to implementation details rather than changes in
the big picture. We describe these details below by discussing
What triggers a context switch?
How does context switch work?
The rest of this section discusses these issues.
Note that we defer discussing one issue
 
Search WWH ::




Custom Search