Context switching!

what happens when an interrupt (context switch) occurs? - hardware stacks program counter - hardware loads new program counter from interrupt vector - assembly lanauge saves registers - assembly language sets up new stack - C interrupt service runs - scheduler decides which process is to run next - C procedure returns to assembly code - assembly language starts up new process

But what actually is a context switch??

a context switch can either mean: - a swithc between threads - saving, restoring state associated with a thread - a switch between processes - thread switch stuff and on top of that with the extra process state stuff (memory)

When do context switches happen?

  • when a syscall happens
  • on an exception
  • on an interrupt
    • includes timing interrupts (schedulers!)

A thread swtich can happen between any two instructions!

How do we preform a context switch?

the goal is to create a thread that we can context swithc away from, and to be context switched back to. The userleverl thread won't notice this switch. That means that when we suspend a thread, we need to take all its context (the stuff that explains what its instructions mean, like registers, sp, pc) and restore its memory.

os161?

in os161, we have a thread switch mechanism where a thread can choose to participate in, which causes its stack to be suspended and transferred to another thread.

Cooperative concurrency