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
- 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, 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 interrupt context switch, and the most general situation is slightly abbreviated;
- The system call that creates the child process starts in the subprocess 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;
Basic concepts of Linux operating system architecture and system execution process operating system
Operating system: A basic set of programs contained in any computer system
- Kernel (process management, process scheduling, interprocess communication mechanism, memory management, interrupt exception handling, file system, I/O system, network part)
- Other programs (function 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 environment for user programs (applications)
The simplest and most complex operation of a typical Linux operating system is the LSCPU execution of instruction memory during the execution of instructions
Experiment: Analyze schedule function execution process using GDB trace
Linux and Security Week eighth summary