Information Technology Reference
In-Depth Information
The mechanism is almost identical when an interrupt or trap triggers a
thread switch among threads in the kernel. We tweak the three steps described
in Chapter ?? for handling a context switch from a user-level process to a kernel
handler as follows (changes are written in italics):
1. Save the state. Save the currently running thread's registers so that the
handler can run code without disrupting the interrupted thread.
Recall that this is done with a combination of hardware saving some state
when the interrupt or exception occurs and software saving the rest of the
state when the handler runs.
2. Run the kernel's handler. Run the kernel's handler code to handle the
interrupt or exception.
Since we are already in kernel mode, we do not need to change from user
to kernel mode in this step.
We also do not need to change the stack pointer to the base of the kernel's
exception stack. Instead, we can just push and pop saved state or handler
variables onto the current stack starting from the current stack pointer.
3. Restore the state. Restore the next ready thread's registers so that it
can resume running where it left off.
In short, comparing this approach to what happens on a mode switch from a
user-level process, the only significant changes are (1) we do not need to switch
modes (and therefore do not need to switch stacks) and (2) we can resume any
thread on the ready list rather than always resuming the thread or process that
was just suspended.
Library-call-triggered thread switch. When a thread calls a library func-
tion such as threadyield() that triggers a thread switch, the steps are similar
to what an interrupt or exception does. We need to
1. Save the state. Save the currently running thread's registers so that the
kernel can later resume this thread.
Here there is no hardware event; the running thread just calls the kernel
handler with a regular procedure call. So, the library procedure must save
all of the state on its own.
That said, the library software should save the state to the thread's TCB
using the same format as used in the interrupt- or exceptin-triggered case
so that we can restore threads the same way regardless of what caused the
thread to be suspended.
2. Run the called library procedure. Run the code to handle the library
call.
 
Search WWH ::




Custom Search