Experiment VIII: Understanding the process of process scheduling and process switching during time tracking analysis process
Name: Li Donghui
Study No.: 20133201
Note: Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Cloud Class Notes:
Scheduling timing of processes and switching of processes
The principle of operating system describes a large number of process scheduling algorithms, these algorithms from the perspective of implementation is only to choose a new process from the run queue, the process of selecting the use of different strategies.
It is more important to understand the working mechanism of the operating system than the process scheduling timing and process switching mechanism.
Timing of process scheduling
Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly, or call schedule () based on the need_resched tag when returning to the user state;
Kernel threads can directly call schedule () for process switching, or in the process of interrupt processing, which means that kernel threads as a special kind of process can be active scheduling, but also can be passively dispatched;
The user-state process cannot implement the active scheduling, but can only be dispatched by a point in time after the kernel state, that is, scheduling during interrupt processing.
Switching of processes
To control the execution of the process, the kernel must have the ability to suspend a process that is executing on the CPU and resume execution of a previously suspended process called process switching, task switching, context switching;
Suspending a process that is executing on the CPU is different from saving the scene at the time of the outage, before and after the interrupt is in the same process context, but only by the user-state to the kernel state execution;
The process context contains all the information required by the process execution
User address space: Includes program code, data, user stack, etc.
Control information: Process descriptor, kernel stack, etc.
Hardware context (note that interrupts are also saved by the hardware context only if the method is saved differently)
The schedule () function selects a new process to run and invokes Context_switch for context switching, a macro called SWITCH_TO for critical context switching
Next = Pick_next_task (RQ, prev);//process scheduling algorithms encapsulate this function inside
Context_switch (RQ, Prev, next);//process Context switch
Switch_to takes advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process
general execution of Linux systems
The most common scenario: The running user-state process x switches to the process of running user-state process y
Running user-state process X
An interrupt occurred--save cs:eip/esp/eflags (current) to kernel Stack,then load Cs:eip (entry of a specific ISR) and SS:ESP (point to Kernel Stack).
Save_all//Save site
Schedule () is called during interrupt processing or before an interrupt is returned, where SWITCH_TO does a critical process context switch
The user-state process y is started after the label 1 (where Y has been switched out through the above steps so it can continue from label 1)
Restore_all//Recovery site
Iret-pop cs:eip/ss:esp/eflags from kernel stack
Continue to run user-state process y
Several special cases
1. By interrupting the timing of the processing process, the user-state process and kernel threads switch between each other and the kernel threads switch to each other, and the most common situation is very similar, but the kernel thread is running in the process of interruption without process user state and kernel state conversion;
2. Kernel thread actively calls schedule (), only the process context switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;
3. Create a child process of the system call in the child process execution starting point and return user state, such as fork;
4. After loading a new executable program, return to the condition of the user state, such as EXECVE;
Course Slides
Experimental content
1. Understand the timing of process scheduling in Linux systems, you can search the kernel code for the schedule () function, see where the schedule () is called, and determine whether the summary of our course content is accurate.
2. Use GDB trace to analyze a schedule () function to verify your understanding of Linux system process scheduling and process switching process, and recommend to complete the experiment in the lab Building Linux virtual Machine environment.
3. Pay particular attention to and carefully analyze the assembly code in Switch_to, understanding the switching mechanism of the process context, and the relationship to the interrupt context switch.
Experimental steps
1. Open the Lab building virtual machine
2. Run the following command in the shell to get the code for this experiment and compile the run
Cdlinuxkernel
Rmmenu–rf
Gitclone Https://github.com/mengning/menu.git
Cdmenu
MVTEST_EXEC.C test.c
Makerootfs
3. Terminal input QEMU–KERNELLINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE–INITRD Rootfs.img–s–s
Then open another terminal input
Gdb
(gdb) file Linux-3.18.6/vmlinux
(GDB) Target remote:1234
(GDB) B schedule
Then enter C to continue execution, the system can be stopped at the function, the next can use the command n or S step-by Trace, you can explore the pick_next_task,switch_to and other functions of the execution process.
Experiment
About the schedule () function:
The function contains the following:
- Handling for preemption
- Spin Lock (
raw_spin_lock_irq(&rq->lock);
)
- Check the status of the prev and reset state
- Process scheduling Algorithm (
next = pick_next_task(rq, prev);
)
- Update the clock for the ready queue
- Process Context switch (
context_switch(rq, prev, next);
)
Context_switch
After selecting the next process to be dispatched, if the selected process is not the currently running process, a context switch is required to execute the selected processcontext_switch。
The Context_switch contains:
- Determine if a kernel thread is required, that is, context switching (
mm
)
- If Next is a normal process, the schedule () function replaces the address space of the prev with the address space of next
- If prev is a kernel thread or a process that is exiting, the Context_switch () function saves a pointer to the Prev memory descriptor in the prev_mm field of the run queue and then sets the PREV->ACTIVE_MM
- Toggle stacks and registers (
switch_to(prev, next, prev);
)
Switch to
This macro implements a true switchover between processes:
- The contents of the Esi,edi and EBP registers are saved first in the kernel stack of the current process prev.
- The Prev kernel stack pointer, EBP, is then deposited into the PREV->THREAD.ESP.
- Place the kernel stack pointer next->thread.esp that will run the process next into the ESP register
- Save the address of the POPL directive in Prev->thread.eip, which is the next time the prev is dispatched
- Transfer to a function via the JMP directive (not the call command)
__switch_to()
- Restores the contents of the stack when next was last transferred. From now on, the next process becomes the current process and really begins to execute.
Summarize:
Schedule () finds a process primarily from the list of running queues and assigns the CPU to the process. Linux scheduling timing: 1. Processes are returned to the user state from interrupts, exceptions, and system calls 2. The kernel thread actively calls schedule (). After the scheduling is completed, the process switches, save (prev) the site into the stack save, switch to the current process address space, modify the EIP, etc., to start the implementation of new processes.
The process context is the environment at which the process executes, defined in Linux: When a process executes, the value of all registers of the CPU, the state of the process, and the contents of the stack. The interrupt context is the environment in which execution is interrupted, that is, some parameters that are passed by the hardware, and the environment in which the kernel needs to save the interrupted process.
The process of understanding process scheduling and process switching during the time-tracking analysis process