Linux Kernel Analysis Eighth Week study summary-process switching and system general execution process

Source: Internet
Author: User

Linux Kernel Analysis Eighth Week study summary-process switching and system general execution process

Huang (Original works reproduced please specify the source)

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

I. SUMMARY OF KNOWLEDGE

    1. Basic concepts and related knowledge of process scheduling in Linux
    2. How the schedule function implements process scheduling
    3. Implementation process of Linux processes (general and special cases)
    4. Macro Description Linux System execution

Second, study notes

(i) Key code SWITCH_TO analysis of process switching

Time analysis of process progress and process scheduling

1. Different types of processes have different scheduling requirements.

2. The first Category:

    • I/o-bound frequent I/O, and it usually takes a lot of time to wait for I/O operations to complete.
    • Cpu-bound is computationally intensive and requires a lot of CPU time to perform operations.

Second Category:

    • Batch processing process
    • Real-time processes
    • Interactive process Shell

3. What is a scheduling policy?

is a set of rules, and they decide when and how to choose a new process to run.

4.Linux processes are queued according to priority

The priority of the 5.Linux process is dynamic

6. The scheduling algorithm related code in the kernel uses a policy pattern similar to that in Ood. The scheduling algorithm is coupled with other parts.

7. Timing of Process scheduling: schedule () function implementation 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.

Kernel threads are special processes that only have a kernel state without a user state.

Process Context switch related code analysis

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

Look at the schedule function specifically:

Click to enter:

Specifically how to switch, click to view:

Find the Switch_to assembly:

Assembly Code Analysis:

    • 42: Save flags for the current process
    • 43: Stack base address of the current process
    • 44: Save the current top of the stack.
    • 45: Put the next process stack top into the ESP register to complete the kernel stack switch
    • 46: Save the EIP for the current process, then restore
    • 47: The next process stack top is the starting point. NEXT_IP is generally $1f, for newly created child processes are ret_from_fork
    • 49: Passing Parameters through registers
    • 50: Thought it was the next process that started the 46~49 blur zone

(ii) General implementation process of Linux systems

Analysis of general execution 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 Y2

Several special cases in the process of Linux system execution

Once we have abstracted things, it is not necessarily accurate, there are 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;

(iii) Overview of the Linux system architecture and implementation process

Linux Operating System Architecture overview

1. Basic concepts of the operating system

2. Purpose of the operating system

3. Typical Operating system architecture

The simplest and most complex operation-performing LS operations

Implementation of Linux systems from the perspective of CPU and memory

1. Stand at the angle of the CPU execution instructions

2. From the memory point of view

Third, after-school homework

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

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;

See (a) Process switching key Code SWITCH_TO analysis process Context switch related Code Analysis section

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.

(1) Open Qemu and GDB:

(2) Set breakpoints:

(3) Press C to continue, you can find core.c inside called schedule ().

(4) View Code with list

(5) Step through Discovery __schedule ()

(6) Enter function:

(6) Continue stepping until you find Pick_nexi_task ():

(7) Continue stepping until the context_switch is found:

(8) cannot enter inside the function. So a new breakpoint is set

(9) After continuing, you can enter the Context_switch () function and discover the switch_to () and __switch_to ()

(10) Complete the tracking.

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;

See (a) Process switching key Code SWITCH_TO analysis process Context switch related code Analysis part of the assembly Code analysis

Linux Kernel Analysis Eighth Week study summary-process switching and system general execution 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.