Operating systems questions and answers, OS University exam questions, what needs to be saved and restored on a context switch between two threads in the same process
Question:
What needs to be saved and restored on a context switch between two threads in the same process? What if the two threads are in different processes?
Answer:
Context switch saves the state of a process or thread. This is to enable the process to resume execution later.
The registers (integer and floating-point), program counter, condition registers (status register or flag register), and any other execution state for a thread must be saved between context switches. Need to reload the registers, stack pointer, and program counter from the TCB of the new thread.
When the threads are from different processes, need to not only save and restore what was given above, but also need to load the pointer for the top-level page table of the new address space. (Note that this top-level page table pointer from the old process does not need to be saved since it does not change and is already contained in the process control block (PCB)).