Information Technology Reference
In-Depth Information
Kernel Thread 1
Kernel Thread 1
Kernel Thread 1
Process 1
Process 2
Exception
Stack
Globals
Kernel
TCB 1
TCB 1
TCB 1
PCB 1
PCB 2
Heap
Stack
Stack
Stack
Code
Process 1
Process 2
Thread
Thread
User-Level Processes
Stack
Stack
Globals
Heap
Globals
Heap
Code
Code
Figure4.14: A multi-threaded kernel with 3 kernel threads and 2 single-threaded user-level pro-
cesses.
Process switch v. thread switch. At a high level, thread switch is nearly
identical whether we are switching between kernel threads or switching between
a process's thread and a kernel thread. In all cases, we save the state of the
currently running thread and restore the state of the next thread to run.
The difference is that when switching from a user-level thread, we need
to switch from operating in user mode to to kernel mode. Leaving aside the
additional work needed to change the virtual memory mappings (which we'll
discuss in Chapter ??), the mode change can also slightly change the low-level
implementation details for saving and restoring thread state.
Hardware-triggered (interrupts and exceptions.). When an inter-
rupt or exception causes a hardware-triggered thread switch, the hardware
and software work together to save the state of the suspended thread. Ex-
actly what state gets saved by the hardware may differ slightly depending
on whether the interrupted thread is running in user mode or kernel mode,
but these changes are relatively small implementation details. For exam-
ple, we saw that for the x86 architecture, the hardware saves the stack
pointer when it switches from user to kernel mode but not when it stays
in kernel mode, and the iret instruction to resume a suspended thread
has to account for this difference.
Software-triggered (library calls v. system calls). When an in-
kernel thread accesses the threads library to create or delete a thread
or to suspend, resume, or switch threads it can use a simple procedure
call, but when a user-level thread accesses the threads library to do these
things, it needs to use a system call; because the PCBs are in the kernel's
memory, a user-level process's threads must invoke kernel code to save
their state.
Search WWH ::




Custom Search