COMP3891

Implementation of Processes and Threads

Processes

Process Modes

Processes can run in two modes:

Threads

User-level Threads

Pros:

Cons:

Kernel-level Threads

Pros:

Cons:

Context Switching

Overview

A context switch refers to:

Occurence

Can happen any time OS is invoked:

A thread switch can happen between any two instructions.

Implementation

Steps of a context switch:

  1. Point stack pointer to kernel stack (originally at user stack).
  2. Trapframe pushed on stack.
    • Contains user-level program counter, stack pointer and relevant registers.
  3. Call C code to process syscall, exception, or interrupt.
    • Results in C activation stack building up.
  4. Kernel decides to perform a context switch:
    • Chooses a target thread (or process).
    • Pushes kernel context onto the process kernel-stack.

    • Any other existing thread must:
      • Be in kernel mode.
      • Have a similar stack layout to the stack we are currently using.
    • Save th current stack pointer in the PCB (or TCB), and load the stack pointer of the target thread.
      • Context has now been switched.