The eighth week of Linux kernel Design Learning summary understanding process scheduling and process switching process scheduling time tracking analysis process

Source: Internet
Author: User
Tags prev git clone

Chen Chaojan Original works reproduced please specify the source

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

First, the video content

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

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

3. Save_all//Save site, here is the process that has entered the kernel interrupt

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

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 () from the need_resched tag when returning to the user state, or, for example, sleep, may call schedule directly
    • 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.

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, or process 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

31#define switch_to(prev, next, last) \

32do {\

33/* \

* Context-switching clobbers All registers, so we clobber \

* them explicitly, via unused output variables. \

* (EAX and EBP are not listed because EBP is saved/restored \

PNS * Explicitly for Wchan access and EAX are the return value of \

* __SWITCH_TO ()) \

39 */\

unsigned long ebx, ecx, edx, ESI, EDI; \

41 \

ASM volatile ("pushfl\n\t"/* Save flags */\

"PUSHL%%ebp\n\t"/* Save EBP */\

"Movl%%esp,%[prev_sp]\n\t"/* save ESP */\

"Movl%[next_sp],%%esp\n\t"/* restore ESP */\

"Movl $1f,%[prev_ip]\n\t"/* Save EIP */\

"PUSHL%[next_ip]\n\t"/* restore EIP */\

__switch_canary \

"JMP __switch_to\n"/* regparm call */\

"1:\t"/* where the next process begins to execute! */                        \

Wuyi "POPL%%ebp\n\t"/* restore EBP */\

"Popfl\n"/* Restore flags */\

53 \

*/* OUTPUT parameters */\

: [prev_sp] "=m" (PREV->THREAD.SP), \

[Prev_ip] "=m" (PREV->THREAD.IP), \

"=a" (last), \

58 \

*/* Clobbered output registers:/\

"=b" (EBX), "=c" (ecx), "=d" (edx), \

"=s" (ESI), "=d" (EDI) \

62 \

__switch_canary_oparam \

64 \

*/* Input parameters: */\

: [next_sp] "M" (next->thread.sp), \

[Next_ip] "M" (NEXT->THREAD.IP), \

68 \

*/* Regparm parameters for __switch_to (): */\

[Prev] "a" (prev), \

[Next] "D" (next) \

72 \

__switch_canary_iparam \

74 \

:/* Reloaded Segment Registers */\

"Memory"); \

(0)

Second, the experimental process

Experimental content

1. Understand the timing of process scheduling in Linux systems, 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 accurate;
2. Use GDB trace to analyze a schedule () function to verify your understanding of Linux system process scheduling and process switching process, and recommend to complete the experiment in the lab Building Linux virtual Machine environment.
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;

Experimental steps

1. Open the Lab building virtual machine https://www.shiyanlou.com/courses/running/890

2. Run the following command in the shell to get the code for this experiment and compile the 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 effect is as follows:



3. 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 in the system call

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 to step-by trace, you can explore the pick_next_task,switch_to and other functions of the execution process, There is a picture for proof:

Iii. Summary

The process scheduling time has three:

1, interrupt processing (including clock interrupts, I/O interrupts, system calls and exceptions), call schedule () directly, or return to the user state according to the need_resched tag call schedule ();

2, kernel threads can directly call schedule () for process switching, can also be scheduled during interrupt processing, that is, kernel threads as a class of special processes can be active scheduling, can also 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.

Process switching: To control the execution of a process, the kernel must have the ability to suspend a process that is running on the CPU and resume execution of a previously suspended process. This behavior is known as Process switch, task switch, or context switch.

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 that the process needs to perform, including:

1. User address space: Including program code, data, user stack, etc.

2. Control information: Process descriptor, kernel stack, etc.

3. Hardware context (different from the method of saving hardware contexts)

General execution of Linux systems

In the most general case:

Running user-state process x switching 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 the scene

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

5, after the label 1 starts 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:

1, through the interrupt processing process scheduling time, the user state process and the kernel thread switch between each other and the kernel thread switch to each other, and the most common situation is very similar, but the kernel thread runs in the process of interruption without process user state and kernel state conversion;

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

3, the creation of the child process of the system call in the child process execution starting point and return user state, such as fork;

4, loading a new executable program to return to the situation of the user state, such as EXECVE;

The eighth week of Linux kernel Design Learning summary understanding process scheduling and process switching process scheduling time tracking analysis process

Related Article

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.