The process of understanding process scheduling and process switching during the time-tracking analysis process

Source: Internet
Author: User
Tags prev git clone

Study No.: 20133232

Name: Wang Chenguang

Wang Chenguang + original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

The last time NetEase Cloud Class Class!!! Some fried chicken excitement has wood there!

First, Cloud class notes:

1, the process of scheduling time and process of switching

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.

2, the 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.

3, the process of switching

• 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;

• Suspend a process that is executing on the CPU, which is different from the Save field at the time of the outage, before and after the interrupt is in the same process context, but only from 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

4, the general implementation process of Linux system

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

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 switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;

• Create a child process's system call execution starting point in the subprocess and returning 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;

Ch_to takes advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process

Ii. contents of the experiment

In the shell, run the following command, get the code for this experiment, and compile and 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 following is a detailed analysis of switch_to code:

Simply mention the called relationship of this macro and function:

Schedule ()--Context_switch ()--switch_to, __switch_to ()

In this case, schedule is the keynote function, which involves some scheduling algorithms, which are not discussed here. Transitions between processes occur when schedule () needs to pause execution of a process and continue execution of the B process. There are two main parts of process switching: 1. Toggle the Global page table entry, 2, switch the kernel stack and the hardware context. This switching work is done by Context_switch (). Among them, switch_to and __switch_to () mainly completed the second part. In more detail, __switch_to () mainly completes the hardware context switch, switch_to mainly completes the kernel stack switch.

When reading switch_to, please note: This is a macro, not a function, its parameters prev, next, last is not a value copy, but its caller context_switch () local variables. Local variables are indexed by%EBP registers, that is, by N (%EBP), n is the compile-time decision, and N is the same local variable in the same piece of code in different processes. In Switch_to, a stack switchover occurred, that is, EBP changed, so pay extra attention to which process the local variable belongs to at any one time. The call to the __switch_to () function is not implemented by ordinary calls, but by direct jmp, where the function parameters are not passed through the stack, but are passed through registers.

Third, the experimental summary:

The experiment shows that the schedule () function is used to select a new process to run, and calls Context_switch () for the context switch, which calls Switch_to () for critical context switching, where pick_next_task () The function encapsulates the process scheduling algorithm. 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; the kernel thread can call schedule () directly Process switching, can also be scheduled during interrupt processing, that is, the kernel thread as a class of special processes can be active scheduling, can also be passive scheduling, the user state process is unable to achieve active scheduling, only through the kernel state after a certain point in time to dispatch, that is, in the interrupt processing process scheduling.

The process of understanding process scheduling and process switching during the time-tracking analysis process

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.