Context switching, that is, switching from one executable process to another, is handled by the context_switch () function defined in kernel/sched. C. Schedule () calls this function whenever a new process is selected to prepare for running. It completes two basic tasks:
1. the call declaration is in switch_mm () in <ASM/mmu_context.h>. This function switches the virtual memory from the previous process ing to the new process. In fact, it switches to the address space of another process, see the example:
2. Call the switch_to () Statement in <ASM/system. h>. This function switches from the last processor status to the processor status of the new process. This includes saving, restoring stack information and register information, and other state information related to the architecture. Each process must be managed and saved as an object. See the following example:
Next let's take a look at the timing of process preemption.
1.User Preemption
When a user space is returned from a system call
Slave interrupt handlingProgramWhen the user space is returned
2.Kernel preemption
The interrupt handler is executing and before returning the kernel space
KernelCodeOnce again
If the task in the kernel displays the call schedule ()
If the task in the kernel is blocked (this also causes schedule () to be called ())