The process of process scheduling and process switching in understanding process scheduling of Linux kernel analysis

Source: Internet
Author: User
Tags prev

First, the principle analysis

1. Scheduling Timing Background
Different types of processes have different scheduling requirements
The first kind of classification
I/o-bond: 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
The second kind of
Batch process: You do not have to interact with the user, usually run in the background, and do not need to respond quickly. Typical batch process: compiler, scientific calculation
Real-time processes (real-time process): There is real-time demand, should not be blocked by low-priority processes, response time is short, to be stable. Typical real-time processes: Video/audio, mechanical control, etc.
Interactive processes (interactive process): Requires frequent interaction with the user, so it takes a lot of time to wait for the user to enter the operation, the response time is fast and the average latency is lower than 50~150ms. Typical interactive programs: shell, text editing programs, graphics applications, etc.
process scheduling in 2.Linux
Linux supports both ordinary time-sharing and realtime-in, and the scheduling in Linux is a mixture of various scheduling strategies and scheduling algorithms.
A scheduling policy is a set of rules that determine when and how to choose a new process to run
The scheduling of Linux based on time-sharing and priority Linux is queued according to priority, the priority of the process is calculated based on a particular algorithm, and is 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 periodically adjusts the process based on the behavior of the process.
A process that has not been assigned to the CPU for a long time, usually ↑
A process that has been running on the CPU for a long time, usually ↓
3.scheudle function? schedule function Implementation scheduling
Purpose: To find a process in the run queue and assign the CPU to it
Call Method:
Call directly, such as sleep_on
Loosely called, according to the need_resched tag
4. 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 they can be dispatched during interrupt processing, which means that kernel threads can be actively dispatched as a special class of processes, or they can be dispatched passively.
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.
User-State Passive scheduling
Kernel threads only have a special process with no user-state, no system calls.
5. 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, and only the user state is switched to the kernel state execution.
The process context contains all the information required by the process execution: the user address space: Including program code, data, user stack, control information: Process descriptor, kernel stack, etc. hardware context (note that interrupts are also saved in the hardware context, but only in the same way)
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
6. Process Code switching Code Analysis
ESP First Cut, EIP re-switch using the push EIP +call played a similar call role, but the flexibility to modify the EIP.
7.Linux System General execution Process
The most common scenario: The running user-state process x switches to the process of running user-state process y
In the running user-state process x is interrupted, the hardware completes the following:
Save Cs:eip/esp/eflags (current) to kernel stack
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 you can continue from label 1
has become the context of the Y process, really Zhuang Zhou Dream
Restore_all//Recovery site
Iret-pop cs:eip/ss:esp/eflags from kernel stack
Continue to run user-state process y

Ii. contents of the experiment

In the shell, run the following command, get the code for this experiment, and compile and run

CD LINUXKERNELRM menu-rfgit clone https://github.com/mengning/menu.gitCD MENUMV test_exec.c Test.cmake rootfs   

The effect is as follows:

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 step-by Trace, you can explore the pick_next_task,switch_to and other functions of the execution process

Iii. Summary

Through this phase of learning, I have some preliminary knowledge of the Linux kernel. Linux uses more commands than Windows, which makes me feel a bit more difficult. And because have not learned the operating system before, so the understanding of some basic concepts is not enough, often listen to listen to faint, you have to combine the information on the Internet to read the course to barely understand one or two.

However, I think self-study is a characteristic of the university (although I started self-taught in high school), so I also learned another book during this period-"The art of Linux kernel design," the book is more than a picture, not entirely by the text and code to explain, I like to see the comic book is very suitable for people. And explained in a very basic, suitable for no basis from the beginning of learning. I think, since learning can not run, then I will learn from the walk. This book on the Linux kernel links to http://pan.baidu.com/s/1kTFXkTh, I wish you can learn to understand the Linux kernel design of the subtle.

Liu Shuai

Original works reproduced please indicate "Linux kernel analysis"

MOOC Course http://mooc.study.163.com/course/USTC-1000029000

The process of process scheduling and process switching in understanding process scheduling of Linux kernel analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.