Study notes:
The timing analysis of process scheduling and process scheduling
1. Different types of processes have different requirements for 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, requiring a lot of CPU time to perform operations
Second Category:
-Batch process: Do not need to interact with the user, usually run in the background;
-Real-time process: real-time demand, not blocked by low priority processes, short response time, stable;
-Interactive process: requires frequent interaction with the user; fast response time
2. Scheduling policy: A set of rules that determines when and how to choose a new process to run.
The 3.Linux process sorts by priority, calculates the process priority with a specific algorithm, and uses a value to indicate how the process is allocated appropriately to the CPU. The priority is dynamic, periodically adjusted according to the behavior of the process, which is not assigned to the CPU for a long time, and is already running on the CPU for a long time to decrease the priority level.
The 4.Schedule function is used to implement scheduling, find a process in the queue, and assign the CPU to him.
-Call Schedule directly
-loosely called, according to the timing of need-resched tagging process scheduling
5. 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.
The user-state process can only be dispatched passively, and the kernel process is a special process that has no user state in the kernel state, and can be actively dispatched or passively dispatched.
Second, 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. Suspend the process that is executing on the CPU, which is different from the save scene at the time of interruption, before and after the interruption is in the same process context, only by the user state to the kernel state execution;
3. The process context contains all the information required for process execution
4. User address space: Including program code, data, user stack, etc.
5. Control information: Process descriptor, kernel stack, etc.
6. Hardware context (note that interrupts are also saved in the hardware context except that the method is saved differently)
The 7.schedule () function selects a new process to run and invokes Context_switch for context switching, which calls 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
8.next-ip is generally $1f, for newly created child processes are ret-from-fork
Third, the general implementation of the Linux system process
The most common scenario: The running user-state process x switches 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 site
4. Schedule () is invoked during interrupt processing or before an interrupt is returned, where SWITCH_TO does a critical process context switch
5. After the label 1 begins 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
Experimental process:
1. Enter the virtual machine environment, boot the kernel, and enter the debug state
2. Set breakpoints at schedule, run, expand function with List
3. Single Step operation until _schedule ()
4. Set breakpoints at Pick_next_task to perform
5. Set breakpoints at Context_switch to perform
Summarize:
The timing of the process is still a general situation and specific circumstances, we have to be based on the situation of the different specific analysis problems.
The most basic class of scheduling algorithm is the priority-based scheduling-the process is graded based on the value of the process and its need for processor time.
The scheduler always chooses the process in which the time slice is not exhausted and the highest priority is run.
Wang Xue Cheng
Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Linux kernel and analysis of the eighth week process switching and system general execution process