Information Technology Reference
In-Depth Information
that allows the currently running thread to voluntarily give up the processor
and context switch the processor to some other thread that is ready to run.
Similarly, the threadjoin() and threadexit() calls can suspend execution
of the current thread and start running a different one.
Second, an interrupt or exception may invoke an interrupt handler, which
must save the state of the running thread, execute the handler's code, and switch
to some thread that is ready to run (or it may just restore the state and resume
running the thread that was just interrupted.)
For example, many thread libraries switch threads when a timer interrupt
occurs. In particular, thread libraries typically want to ensure that no thread can
monopolize the processor, so they arrange for a hardware timer to periodically
(e.g., every 10ms) cause a timer interrupt. The handler for the timer interrupt
saves the state of the running thread, chooses another thread to run, and runs
that thread by restoring its state to the processor.
Other hardware events (e.g., a keyboard key is pressed, a network packet
arrives, or a disk operation completes) also invoke interrupt handlers. In all
cases, these handlers save the state of the currently running thread so that it
can be restored later. They then execute the handler code, and when the handler
is done, they restore the state of some ready thread. If the interrupt caused a
high priority thread to become ready, then the scheduler will set things up so
that the interrupt returns to that thread instead of the interrupted one.
How does a kernel thread context switch work?
Regardless of whether a switch between kernel threads is triggered via an in-
terrupt or an explicit call to the threads library, what needs to be done is
conceptually simple:
1. Copy the running thread's registers from the processor to the thread's
TCB
2. Copy a ready thread's registers from the thread's TCB to the processor
The implementation details differ slightly depending on whether the thread
switch is caused by an interrupt or a explicit library call by the thread. Still,
the cases are more the same than dierent|we want to make sure that all of
the threads on the ready list have their state saved in the same way so that we
can restore them in the same way.
Hardware-triggered thread switch. We saw what happens when an inter-
rupt, exception, or trap interrupts a running user-level process in Chapter ??:
hardware and software work together to save the state of the interrupted process,
run the kernel's handler, and restore the state of the interrupted process.
 
Search WWH ::




Custom Search