Abstract: Pan Junyang Original works reproduced please indicate the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Experimental Purpose:
Use GDB trace to analyze a schedule () function to understand the timing of process scheduling in a Linux system.
Experimental process:
Open the shell terminal and execute the following command:
CD Linuxkernel
RM-RF Menu
git clone https://github.com/mengning/menu.git
CD Menu
MV TEST_EXEC.C test.c
Make Rootfs
Debug mode can be opened by adding-s-s startup parameters
Qemu-kernel. /linux-3.18.6/arch/x86/boot/bzimage-initrd. /rootfs.img-s-S
Open GDB for remote debugging
Gdb
File: /linux-3.18.6/vmlinux
Target remote:1234
Set breakpoints
B Schedule
b Context_switch
b switch_to
b pick_next_task
Experimental Analysis:
The experiment shows that the schedule () function is used to select a new process to run, and calls Context_switch () for the context switch, which calls Switch_to () for critical context switching, where pick_next_task () The function encapsulates the process scheduling algorithm.
Experiment Summary:
The process scheduling time has three:
1, interrupt processing (including clock interrupts, I/O interrupts, system calls and exceptions), call schedule () directly, or return to the user state according to the need_resched tag call schedule ();
2, kernel threads can directly call schedule () for process switching, can also be scheduled during interrupt processing, that is, kernel threads as a class of special processes can be active scheduling, can also be passive scheduling;
3, the user state process can not realize the active scheduling, only through the kernel state after a certain point in time to dispatch, that is, in the interrupt processing process scheduling.
Process switching: To control the execution of a process, the kernel must have the ability to suspend a process that is running on the CPU and resume execution of a previously suspended process. This behavior is known as Process switch, task switch, or context switch.
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, and only the user state is switched to the kernel state execution.
The process context contains all the information that the process needs to perform, including:
1. User address space: Including program code, data, user stack, etc.
2. Control information: Process descriptor, kernel stack, etc.
3. Hardware context (different from the method of saving hardware contexts)
General execution of Linux systems
In the most general case:
Running user-state process x switching to the process of running user-state process y
1. Running user-state process X
2. Interrupt--save Cs:eip/esp/eflags (current) to kernel Stack,then load Cs:eip (entry of a specific ISR) and Ss:esp (point to Kern El Stack).
3, Save_all//Save the scene
4. Schedule () is called during interrupt processing or before the interrupt is returned, where the switch_to does a critical process context switch
5, after the label 1 starts to run the user state process Y (here Y has been switched out through the above steps so you can continue from the label 1)
6, Restore_all//recovery site
7. Iret-pop Cs:eip/ss:esp/eflags from kernel stack
8, continue to run the user-state process y
Several special cases:
1, through the interrupt processing process scheduling time, the user state process and the kernel thread switch between each other and the kernel thread switch to each other, and the most common situation is very similar, but the kernel thread runs in the process of interruption without process user state and kernel state conversion;
2, the kernel thread actively calls schedule (), only the process context of the switch, there is no interruption context of the switch, and the most general situation is slightly abbreviated;
3, the creation of the child process of the system call in the child process execution starting point and return user state, such as fork;
4, loading a new executable program to return to the situation of the user state, such as EXECVE;
The process of understanding process scheduling and process switching during the time-tracking analysis process