"Linux Kernel Analysis" section eighth process switching and system general execution process

Source: Internet
Author: User

First, process switching key code SWITCH_TO Analysis 1. Progress of process and timing analysis 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.

2. Process context switch related code analysis
    • In order 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, different from saving the scene at the time of the outage, and in the same process context before and after the outage. Just 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 thecontext switch for a contextual switchover, which callsswitch to for critical context switching

      • Next = picknexttask (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

Second, the general implementation process of Linux system 1.Linux system General execution Process analysis
    • 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. 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 Kernel Stack).
      3. Save_all//Save site
      4. Schedule () is called during interrupt processing or before an interrupt is returned, where SWITCH_TO does a critical process context switch
      5. The user-state process y is started after the label 1 (where Y has been switched out through the above steps so it can continue from 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
Several special cases in the process of 2.Linux system execution
    • 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 at the execution point in the subprocess and returns the user state, such as fork, and returns to the user state after loading a new executable program, such as Execve;

3. The core and the dancer
    • 3G only the kernel states can access
Iii. Overview of the Linux system architecture and execution process 1.Linux operating system Architecture Overview

2. Simplest and most complex operation--perform LS operation

3. The implementation of Linux systems from a CPU and memory perspective

Iv. Experimental Report one, experimental requirements
    • Understanding the timing of process scheduling in Linux systems, you can search the kernel code for the schedule () function and see where the schedule () is called.

    • 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. Special attention and careful analysis of the assembly code in the Switch_to, understanding the process context of the switching mechanism, as well as the relationship with the interrupt context switch;

    • The blog content needs to carefully analyze the scheduling timing of the process, switch_to and corresponding stack state.

Second, the experimental steps

1. Open Qemu and GDB

2. Set breakpoints

3. View the code with list

4. Stepping through Discovery __schedule ()

5. Enter function

6. Continue stepping until you find Pick_nexi_task ()

7. Set breakpoints at Pick_next_task to perform

8. Set breakpoints at Context_switch to perform

"Linux Kernel Analysis" section eighth 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.