简体   繁体   中英

In what order does a context switch to the kernel occur

Out of these three steps, is this the right order, or do I need to switch any?

1) Save current state data

2) Turn on kernel mode

3) Determine cause of interrupt

So, let me try to help you figuring out the correct order.

  • Only the kernel can switch a context as only the kernel has access to the necessary data and can for example change the page tables for the other process' address space.
  • To determine whether to do a context switch or not, the kernel needs to analyse some "inputs". A context switch might be done for example because the timer interrupt fired and the time slice of a process is over or because the process started doing some IO.
  • Only the kernel can save the state of a user process because a user process would change its state when it would try storing it. The kernel however knows that if its running, the user process is currently interrupted (eg because of an interrupt or because the user space process voluntarily entered the kernel eg for a system call)
  1. The current context of a process is first saved partly by the hardware(processor) and rest by the software(kernel).
  2. Then the control is transferred from the user process to the kernel by loading the new eip, esp and other saved context of kernel is loaded by hardware from Task State Segment(TSS).
  3. Then based on the interrupt or trap no. the request is dispatched to the appropriate handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM