The transition of the experiment eight process and the general execution process of the system
20135114 Wang Zhaoxian Original works reprint please indicate the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
First, process switching key code SWITCH_TO analysis 1.1 process scheduling and process scheduling timing analysis
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.
1. Timing of process scheduling
The schedule () function implements the Dispatch:
- Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly, or call schedule () from the need_resched tag when returning to the user state
- Kernel threads can directly call schedule () for process switching, or they can be dispatched during interrupt processing, which means that kernel threads can be actively dispatched as a special class of processes or can be passively dispatched
- The user-state process is not able to implement active scheduling, only can be dispatched by a certain point in time after the kernel state, that is, scheduling during interrupt processing
2. different types of processes have different scheduling requirements
The user-state process can only be dispatched passively, and the kernel thread is a special process that has no user state in the kernel state.
1.2 Process Context switch related code analysis
1. in order 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.
2. 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.
3. The process context contains all the information required for 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)
4. Schedule () function selects a new process to run and invokes Context_ switch for context switching, this macro calls Switch_ to for critical context switching
5. Schedule () function code procedure
The above code Context_switch (Rq,prev,next) completes the process context switch from Prev to next.
6. Process Switching context Code procedure analysis
pushes the stack base address of the current process
Switch_ to complete the switch of the Register: Save the current process register, then the stack switch,
then switch the EIP so that the current process can be recovered from the new process
next_ IP is generally $1f (ret_ from_ Fork for newly created process)
indicates that the top of the next process stack is the starting point.
next_ IP is generally $1f, for the newly created child process is ret_ from_forkjmp _ Switch_ to is a function call, passing parameters through the Register; function execution ends with the next instruction (that is, the start of the new process)
The next
process was the Prev process, and the next execution after next executes is actually a process that has just been switched
Analysis of general execution process of 2.1 Linux system
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 in the general execution of 2.2 Linux system
Several special cases:
- By interrupting the timing of the process, the user-state process and the kernel thread switch between each other and the kernel threads switch to each other, very similar to the most common situation, but the kernel thread is running in the process of interruption without process user state and kernel state conversion
- 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
- The system call that creates the child process starts in the child process and returns the user state, such as fork
- A situation in which a new executable program is loaded and returned to the user state, such as Execve
2.3 Cores with dancers
- The address space of the process is a total of 4G, where 0--3g is user-configurable, and more than 3G can be accessed only by the kernel state
- The kernel is equivalent to a taxi and can provide a kernel-to-user conversion for each "wave" process.
- No process needs to be "hosted" when the kernel enters the IDLE0 process for "idling". When a user process has a requirement, the kernel breaks, helping the user process complete the request before returning to the user process. It's like taxi the user after a lap and then puts the user down.
- More than 3G is the "Taxi", which is shared by all processes, and is easier to switch between the kernel states
- The kernel is a collection of various interrupt handlers and kernel threads.
Iii. Overview of the Linux system architecture and execution process 3.1 Linux operating system architecture overview
1. Basic concepts and purposes of the operating system
2. typical Linux operating system architecture
3.3 Implementation of Linux systems from the perspective of CPU and memory
1. Execute the Gets () function
2. Execute system call, fall into kernel
3. wait for input, the CPU dispatches other processes to execute, and wait for an I/O interrupt
4. input ls, send I/O interrupt to CPU, interrupt handler for field save, press stack, etc.
5. The interrupt handler discovers that the X process is waiting for this I/O (at which point X has become blocked), and the handler sets X to Wake_up
6. process Management may set process X to the next process so that the get system invokes the data and returns to the user-state stack
7. from the point of view of the CPU execution instructions:
8. from a memory point of view, all physical addresses are mapped to more than 3G of address space. Because this part is shared for all processes:
Four, the experiment
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
CD Linuxkernel
RM MENU-RF
git clone https://github.com/mengning/menu.git
CD Menu
MV TEST_EXEC.C test.c
Make Rootfs
The effect is as follows:
3. Close the Qemu window, in the Shell window, the CD Linuxkernel back to the Linuxkernel directory, start the kernel with the following command and stop for debugging before the CPU runs the code:
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S
Next, we can split a new shell window horizontally, then start GDB debugging with the following command
Gdb
(gdb) file Linux-3.18.6/vmlinux
(GDB) Target remote:1234
And in the system call
Close the Qemu window, in the Shell window, the CD Linuxkernel back to the Linuxkernel directory, start the kernel with the following command and stop for debugging before the CPU runs the code:
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S
Next, we can split a new shell window horizontally, then start GDB debugging with the following command
Gdb
(gdb) file Linux-3.18.6/vmlinux
(GDB) Target remote:1234
and set a breakpoint at the entrance of the kernel function schedule, and then enter C to continue execution, then the system can stop at the function, then we will be able to use the command n or S to step-by trace, you can explore the pick_next_task,switch_to and other functions of the execution process, There is a picture for proof:
Summarize
This week's video focuses on the key code for process switching Switch_ to analysis, the general execution of Linux systems, the architecture of Linux systems, and the execution process.
The schedule () function implements the process scheduling,
Context_ switch completes the process context switch,
Switch_ to complete the switch of the register.
In the timing of scheduling, kernel threads as a class of special processes can be active scheduling, can also be passive scheduling.
And 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.
The transition of the experiment eight process and the general execution process of the system