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

Source: Internet
Author: User

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


First, the principle analysis

1, the process of scheduling time and process 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

(1) 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;

(2) kernel thread can directly call schedule () for process switching, can also be scheduled in the interrupt processing process, that is, kernel threads as a class of special processes can be active scheduling, but also can be passive scheduling;

(3) The user state process can not realize the active scheduling, only through the kernel state after a certain point in time to dispatch, that is, in the interrupt processing process scheduling.


3, the process of switching

(1) In order to control the execution of the process, the kernel must have the ability to suspend the process executing on the CPU and resume execution of a previously suspended process, which is called process switching, task switching, context switching;

(2) Hangs the process that is executing on the CPU, is different from the save scene at the time of interruption, before and after the interruption is in the same process context, only uses the user state to turn to the kernel state execution;

(3) The process context contains all the information required for 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 knowledge Save method)

(4) The 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 Han Hong

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

#define SWITCH_TO (prev, Next, last) do {/*        * Context-switching clobbers All registers, so we clobber * them explicitly, via unused output variables. * (EAX and EBP are not listed because EBP are saved/restored * explicitly for Wchan access and EAX are the return Valu E of * __SWITCH_TO ()) */unsigned long ebx, ecx, edx, ESI, ed                                                    I ASM volatile ("pushfl\n\t"/* Save Flags */"PUSHL%%ebp\n\t"/* Save EBP */"M OVL%%esp,%[prev_sp]\n\t "/* Save ESP */" MOVL%[next_sp],%%esp\n\t "/* Restore ESP */" MOV L $1f,%[prev_ip]\n\t "/* Save EIP */" PUSHL%[next_ip]\n\t "/* Restore EIP */__SWITC H_canary "JMP __switch_to\n"/* regparm call */"1:\ t "" POPL%%ebp\n\t "/* Restore EBP */" popfl\n "/* restore Flags */* OUTPUT parameters */: [prev_sp] "=m"                                                                (PREV->THREAD.SP), [prev_ip] "=m" (Prev->thread.ip), "=a" (last), /* Clobbered OUTPUT registers: */"=b" (EBX), "=c" (ecx ), "=d" (edx), "=s" (ESI), "=d" (EDI) __                           Switch_canary_oparam/* Input parameters: */                                                           : [next_sp] "M" (NEXT->THREAD.SP), [next_ip] "M" (Next->thread.ip), /* Regparm parameters for __switch_to (): */[prev] "a" (prev)                       ,    [Next]                                                             "D" (next) __switch_canary_iparam                  :/* Reloaded Segment Registers */"memory"); } while (0)


4, the general implementation process of Linux system

The most common scenario: The running user-state x switches to the process of running user-state process y

(1) Running user-state process X

(2) 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 Ke Rnel stack).

(3) Save_all//Save site

(4) Schedule () is invoked during interrupt processing or before the interrupt is returned, where SWITCH_TO does a critical process context switch

(5) After the label 1 starts to run the user-state process y (where 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 user-state process y


A few special cases:

(1) Through the timing of the terminal processing process, the user state process and the kernel thread switching between each other and the kernel thread to switch to each other, and the most common situation is very similar, but the kernel thread during the operation of the interruption without the user-state and kernel-state conversion;

(2) The kernel thread actively calls schedule (), only the process context switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;

(3) The execution starting point and returning user state, such as fork, that the system call of the child process is created in the subprocess;

(4) When a new executable program is loaded and returned to the user state, such as EXECVE;





Ii. contents of the experiment

1, understand the timing of the process scheduling in the Linux system, you can search the kernel code for the schedule () function, see where the schedule () is called, to determine whether the summary of our course content is correct;

2, using the GDB trace analysis of a schedule () function, verify your understanding of the Linux system process scheduling and process switching process;

3, pay special attention to and carefully analyze the assembly code in Switch_to, understand the switching mechanism of the process context, and the relationship with the interrupt context switch.


Third, the experimental process

Open the virtual machine and enter the following command:

CD Linuxkernel

RM MENU-RF

git clone https://github.com/mengning/menu.git

CD Menu

MV TEST_EXEC.C test.c

Make Rootfs

Then in terminal input: Qemu-kernel. /linux-3.18.6/arch/x86/boot/bzimage-inird Rootfs.img-s-S

Open a different shell terminal and enter the following command:

Gdb

File Linux-3.18.6/vmlinux

Target remote:1234

B Schedule

C

Then trace debugging, you can explore the switch_to and other functions of the execution process






The main processing procedure for the schedule () function is:

1. Handling for preemption

2, RAW_SPIN_LOCK_IRQ (&rq->lock);

3. Check the status of the prev and reset state

4, Next = Pick_next_task (RQ, prev); Process scheduling algorithm

5. The clock of the update-ready queue

6, Context_switch (RQ, Prev, next); Process Context Switch


The schedule () function is used to select a new process to run and invoke Context_switch () for context switching, which calls Switch_to () for critical context switching, where the Pick_next_task () function encapsulates the process scheduling algorithm.

The switch_to implements a true switchover between processes:

(1) The contents of ESI, EDI and EBP registers are saved first in the kernel stack of the current process prev.

(2) then the Prev kernel stack pointer ebp is deposited into the PREV->THREAD.ESP

(3) Assign the kernel stack pointer next->thread.esp that will run the process next to the ESP register

(4) Save the address of the POPL instruction in the PREV->THREAD.EIP, this address is prev the next scheduled address

(5) JMP transfer to __switch_to () function

(6) To restore the stack contents of next when it was last transferred, the next process becomes the current process to begin execution.


Iv. Summary of the experiment

Through the study of this course, I have a certain understanding of the process scheduling timing and process scheduling and process switching process. Timing of process scheduling: Interrupt processing, call schedule directly, or return to the user state according to the need_resched tag call schedule; Kernel threads can call schedule directly for process switching, or they can be dispatched during interrupt processing. That is, kernel thread as a special kind of process can be active scheduling, but also do not move scheduling, the user state process cannot realize the active scheduling, can only be dispatched during the interrupt processing. Process switching: The schedule function selects a new process to run and invokes Context_switch for context switching, which calls switch_to for critical context switching.





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.