The switchover of the eighth week of the Linux kernel analysis process and the general execution of the system

Source: Internet
Author: User

Dermot W.J. Zhang Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

I. Analysis of key code SWITCH_TO for process switching 1. Timing analysis of process scheduling and process scheduling
    1. Process classification
      1. Category 1
        1. I/o-bound: Waiting for I/O
        2. Cpu-bound: CPU-intensive computing
      2. Category 2
        1. Interactive process (shell)
        2. Real-time processes
        3. Batch processing process
    2. Process scheduling Policy
      • A set of rules that determine when a process is selected in which way
      • Linux scheduling is based on time-sharing and priority policies:
        1. The process is queued according to the priority (calculated by the system based on the specific algorithm);
        2. The value of this priority indicates how the CPU is properly allocated;
        3. The scheduler dynamically adjusts the priority based on the running cycle of the process;
        4. such as nice and other system calls, you can manually adjust the priority level
      • The scheduling strategy is essentially an algorithm that, from the point of view of implementation, chooses a new process from the running queue, using different strategies in the process of selection.
      • Scheduling algorithm related code in the kernel uses a policy pattern similar to that in Ood
    3. 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 (i.e., the user-state process can only be dispatched passively);
      • 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. Related code for the process switching context

1. Concept:

    • 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 (suspending a process that is executing on the CPU, which is different from the Save field at the time of the outage, and before and after the interrupt is in the same process context. 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.
      • The hardware context (interrupts also save the hardware context is only a different method of saving, interrupts are solved by pressing the stack, and this is done through the schedule function)
    • Switch_to Complete Register Switching: First save the current process register, and then make a stack switch (44th, 45 rows) since all the stack is in the new process, and then switch the EIP (46, 56 lines), so that the current process can be restored from new process, there are other necessary switches
    • NEXT_IP is generally $1f (ret_from_fork for newly created processes)
    • JMP __switch_to is a function call that passes a parameter through a register; the function executes at the end of the return, starting with the next instruction (that is, the beginning of the new process)
Second, the general implementation process of Linux system 1. 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

    • Running user-state process X
    • Interrupt occurred--
      • Save Cs:eip/esp/eflags (current) to kernel stack;
      • Then load CS:EIP (start of system call, entry of a specific ISR) and SS:ESP (point to kernel stack)
    • Enter kernel code, Save_all//save site
    • (This step may also not occur) schedule () is called before interrupt processing or interrupt return, where SWITCH_TO does a critical process context switch (switches the user stack of the current process x to the other process stack needed)
    • Label 1 starts running the user-state process y selected in the previous step (where Y has been switched out through the above steps so you can continue from label 1)
    • Restore_all//Recovery site
    • Iret-pop cs:eip/ss:esp/eflags from kernel stack
    • Continue to run user-state process y
    • Supplemental: Interrupt context and process long context toggle

The former is the switchover inside the CPU, the latter is the switch of the stack in the kernel

Several special cases in the process of 2.Linux system execution
    1. By interrupting the timing of the 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 in the process of interruption when there is no process user Configuration and kernel state conversion, CS will not change;
    2. Kernel thread actively calls schedule (), only the process context of the switch, there is no interruption context of the switch, do not need to return from the interrupt, compared with the most general situation is more abbreviated;
    3. The system call that creates the child process starts at the execution point in the subprocess (as Ret_from_fork, as mentioned earlier) and returns the user state;

A situation in which a new executable program is loaded and returned to the user state, such as EXECVE, in which the interrupt-saved information is modified inside the new process

3. Address switching
    1. The address space of the process is a total of 4G, where 0--3g is user-configurable, and more than 3G can be accessed only by the kernel state
    2. Cores & Dancers
      1. The kernel is a collection of various interrupt processing processes and kernel threads;
      2. The kernel is equivalent to a taxi, which can provide the transition of the kernel state to the user state for each "wave" process;
      3. No process needs "bearer" when the kernel enters the IDLE0 process for "idling";
      4. More than 3G is the "Taxi", which is shared by all processes, and is easier to switch between the kernel states
Iii. Overview of the Linux system architecture and execution process 1, CPU and memory view of Linux system implementation
    1. Execute the gets () function;
    2. Execute system call, fall into kernel;
    3. Waiting for input, the CPU dispatches other processes to execute while wait for an I/O interrupt;
    4. Hit LS, send I/O interrupt to the CPU, interrupt processing program for on-site storage, pressure stack and so on;
    5. The interrupt handler discovers that the X process is waiting for this I/O (at which point X has become blocked) and the handler sets X to Wake_up;
    6. Process management may set process X to the next process so that the get system invokes the data and returns to the user-state stack
    7. 7. From a memory point of view, all physical addresses are mapped to more than 3G of address space: Because this part is shared for all processes
Four, the experiment

GDB Debugging:

Set a breakpoint at schedule and click C to run.

C then press N to step through until you encounter the __schedule function and enter it to view

Continue execution until you find the Context_switch function, set a breakpoint, and try to get into its internal view

The switchover of the eighth week of Linux kernel analysis and the general execution of the system

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.