Knowledge Point GroomingI. Key code analysis for process switching1.1 Time analysis of process scheduling and process scheduling
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.
Different types of processes have different scheduling requirements
First Category:
- I/o-bound frequent I/O, often spending a lot of time waiting for I/O operations to complete
- Cpu-bound computationally intensive; requires a lot of CPU time to perform operations
Second Category:
- Batch processing process
- Real-time processes
- Interactive process Shell
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.
scheduling Policy : A set of rules that determine when and how to choose a new process to run.
Linux supports both normal and process. Real-time processes are also supported. Linux scheduling is based on time-sharing and priority.
Linux is queued according to the priority of the process
Calculates the priority of a process based on a particular algorithm, represented by a value that indicates how the process is properly allocated to the CPU.
The priority of a process in Linux is dynamic, and the scheduler adjusts the priority of the process based on the periodic process behavior. (a process that has not been assigned to the CPU for a long time has a higher priority, and a process that has been running on the CPU for a long time has a lower priority)
Schedule function : Implement scheduling, in the queue proud of a process to allocate the CPU to it.
Call Method:
- Call Schedule Directly ()
- Loosely called, according to the need_resched tag
1.2 Process Context switch related code analysis
To control the execution of a 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, and 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, and only the user state is switched 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
The schedule () function selects a new process to run and invokes context switches to switch contexts, and this macro calls switch _ to for critical context switching
Next = Pick _ next _ Task (RQ, prev);//encapsulates some of the process scheduling policies used, selecting a process as Next
Context_switch (RQ, Prev, next);//implement process context switch
Switch_to toggles the state and stack of registers, takes advantage of two parameters: prev points to the current process, next points to the scheduled process
Second, the general implementation of the Linux system processanalysis 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
Iii. Overview of the Linux system architecture and implementation process3.1 Linux operating system architecture Overview
1. Basic concepts and purposes of the operating system
Any computer system contains a basic set of programs, called the operating system.
Kernel (process management, process scheduling, interprocess communication mechanism, memory management, interrupt exception handling, file system, I/O
System, network part)
Other programs (such as libraries, shell programs, system programs, and so on)
The purpose of the operating system
Interact with hardware, manage all hardware resources
Provides a good execution for the user program (application)
Ring
2. Typical Linux operating system architecture
- Hardware Management
- Kernel implementation
- System calls
- Basic software (Shell, Lib)
- User Program
3.2 Simplest and most complex operation--executing the LS command
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:
The experimental process and
1.
"MOOC EXP" Linux kernel Analysis experiment Eight report