0

When the scheduler part of the code is running in linux kernel, how it is saving all the registers of previously running process. How the scheduler knows? While the scheduler is running is it not overwriting those register values of the previous process?

chandran
  • 69
  • 1
  • 7

1 Answers1

2

The scheduler is implemented as a timer interrupt. You should pick an architecture and read up on interrupts. You'll find that a few key registers are saved automatically by the CPU when the interrupt occurs. The remainder are saved by the ISR in its first few instructions.

Here is a post which details the interrupt process on the x86 architecture.

Note that the flow for the scheduler is Process A -> ISR -> switch_to. At this point, the scheduler is interrupted in the middle of switch_to. The next time the scheduler is run, it will resume at that point: switch_to -> return to usermode -> Process A. Essentially this means that every process which is not running is stopped in the middle of an invocation of switch_to.

Community
  • 1
  • 1
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Thanks for the reply.. In linux before switch_to() function ,scheduler performs lot of other funcions and finaly only it saves the registers of previous process like esp and loads the next process esp.. until this point how the previous process registers are preserved.. – chandran Apr 09 '14 at 18:50
  • `switch_to` is switching from `kernel -> user process`. The registers can be saved at any time because these are the registers for the scheduler. The switch from `user process -> kernel` happens in an IRQ ISR (the timer or any other device which causes an interrupt). – Dark Falcon Apr 09 '14 at 19:31
  • `switch_to()` is saving/restoring *kernel* registers. For user space, the registers are stored on a `thread_info` structure and stack whenever we enter kernel space (irq, dabt, pabt, syscall, etc). Both sets of registers are part of a **process**; it is not clear which one you (chandran) are talking about. The `thread_info` is often part of the kernel stack, which is one of the register that `switch_to()` switches. – artless noise Apr 10 '14 at 16:28