Section eighth, process switching and general execution of the system

Source: Internet
Author: User
Tags prev switches

The eighth week process switchover and the system's general execution process by 135217 Sun Xiaobo this week's main content:
    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
The main code of the process switch switch_to the scheduling time of the process and the process switching different scheduling requirements for different types of processes

First Category:

    • I/o-bound: Frequent I/O, and it takes a lot of time to wait for I/O completion

    • Cpu-bound: computationally dense, requiring a lot of CPU time to perform operations

Second Category:

    • Batch process: Do not need to interact with the user, often in the background, do not have to respond quickly (typical batch system: compiler, scientific calculation).

    • Real-time processes: There is real-time demand, should not be blocked by low-priority processes, response time is short, to be stable (typical real-time process: Video/audio, mechanical control, etc.).

    • Interactive process: Requires frequent interaction with the user, so it takes a lot of time to wait for user input, response time is fast, and average latency is low (typical interactive programs: Shell, text-editing programs, graphics applications).

Process scheduling in Linux

Scheduling policy: Is the rule that determines when and how to choose a new process to run. Scheduling in Linux is a combination of various strategies and scheduling algorithms.

Linux supports both normal and process. Real-time processes are also supported. Linux scheduling is based on time-sharing and priority.

Linux is queued according to the priority of the process

    • Calculates the priority of a process based on a particular algorithm, represented by a value that indicates how the process is properly allocated to the CPU.

    • The priority of a process in Linux is dynamic, and the scheduler adjusts the priority of the process based on the periodic process behavior. (a process that has not been assigned to the CPU for a long time has a higher priority, and a process that has been running on the CPU for a long time has a lower priority)

The kernel's scheduling algorithm-related code uses a policy pattern similar to the one in Ood, which from the implementation point of view is simply to select a new process from the run queue, using different strategies in the selection process. The need to understand this strategy does not help our understanding of the kernel. It is more important to understand the working mechanism of the operating system than the process scheduling timing and process switching mechanism.

Schedule function

Schedule function: Implement scheduling, in the queue proud of a process to allocate the CPU to it.

Call Method:

    • Call Schedule Directly ()
    • Loosely called, according to the need_resched tag
Timing 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 they can be dispatched during interrupt processing, which means that kernel threads can be actively dispatched as a special class of processes or passively dispatched (kernel threads are special processes that have only kernel state and no user state).

    • The user-state process can only be dispatched passively, and can only be dispatched by a certain point in time after the kernel state, that is, scheduling during interrupt processing.

Transition of process context switch related code analysis process

Process switching (Task switching, context switching): In order to control the execution of a 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.

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 from the user state to the kernel state, while the process switching is scheduled between different processes.

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 also need to be saved in a hardware context just to save a different method, break: Save field & resume thread; Process Scheduler: switc_to mechanism)

Schedule () Function implementation method

The schedule () function selects a new process to run and invokes context switches to switch contexts, and this macro calls switch _ to for critical context switching

  • Next = Pick _ next _ Task (RQ, prev);//encapsulates some of the process scheduling policies used, selecting a process as Next

  • Context_switch (RQ, Prev, next);//implement process context switch

  • Switch_to toggles the state and stack of registers, takes advantage of two parameters: prev points to the current process, next points to the scheduled process

    #define SWITCH_TO (prev, Next, last)do {UnsignedLong ebx, ecx, edx, ESI, EDI;AsmVolatile"Pushfl\n\t"#保存当前进程的flags"Pushl%%ebp\n\t"#当前进程的堆栈基址压栈The following two sentences actually implement the kernel stack switch, after which all the stack actions are implemented in the kernel stack of next"Movl%%esp,%[prev_sp]\n\t"#把当前的栈底保存到prev_sp"Movl%[next_sp],%%esp\n\t"#把下一个进程的栈顶放入esp寄存器当中"Movl $1f,%[prev_ip]\n\t"#保存当前进程的eip, restore from here when the current process is resumed"Pushl%[next_ip]\n\t"#把next进程的起点压入栈, the top of the next process is its starting point.If it is a process switch here is generally $1f, if the child process is newly created here is Ret_from_fork __switch_canaryJMP is used here, so the parameters are passed through the Register ($1f); if it is call, the direct RET"JMP __switch_to\n"#通过寄存器传递参数"1:\t"#prev进程的起始点Recovering the context execution environment of the Prev process"Popl%%ebp\n\t"#ebp出栈"Popfl\n"#flags出栈/* OUTPUT Parameters */: [PREV_SP]"=m" (PREV->THREAD.SP),#当前进程内核堆栈的栈底 [PREV_IP]"=m" (PREV->THREAD.IP),#当前进程的eip"=a" (last),/ * Clobbered output registers: */ "=b" (EBX), "=c" (ecx), "=d" (edx), "=s" (ESI), "=d" (EDI) __SWI Tch_canary_oparam/ * Input Parameters: */: [next_sp] "M" (NEXT->THREAD.SP), #下一个内核堆栈的栈底 [next_ip] "M" (NEXT->THREAD.IP), #下一个进程执行的起点//prepare for process switchover [prev] "a" (prev), [next] "D" (next) __switch_canary_ Iparam:/ * Reloaded Segment Registers */ "Memory");} While (0)                 

With your own understanding and illustrated as follows:

The process of general process switching in general execution process of Linux system

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--save cs:eip/esp/eflags (the current process CPU state is pressed into the kernel stack of the user-state process x) load CS:EIPSS:ESP (load interrupt service routine and kernel stack).
    3. Enter the interrupt processing process, first Save_all, save the scene
    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 the execution state of the recovery process X
    7. Iret-pop cs:eip/ss:esp/eflags from kernel stack
    8. Continue to run 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 interruption context of the switch, more than the 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;

Linux system architecture and Execution Overview Basic concepts of the operating system

Any computer system contains a basic set of programs, called the operating system.

    • Kernel (process management, process scheduling, interprocess communication mechanism, memory management, interrupt exception handling, file system, I/O
      System, network part)

    • Other programs (such as libraries, shell programs, system programs, and so on)

The purpose of the operating system

    • Interact with hardware, manage all hardware resources

    • Provides a good execution for the user program (application)
      Ring

Linux system execution from the CPU point of view

    1. Get a command from the shell, a string c,c=gets () is a system call from the user-state stack into the kernel

    1. The process by which the CPU waits for the user to enter commands, switches to other process executions, involving process scheduling.

    1. A command is struck on the keyboard, the keyboard has an I/O interrupt, and the interrupt handler is called. When an input parameter is obtained, it wakes up the process that has just entered the blocking state as a result of waiting to enter the ready state.

    1. When the interrupt processing process occurs, it is possible for process X to execute as the next process, and process management switches to process X. The system call then gets the data that needs to be read from the keyboard and then returns to the user state.

    2. Repeat this process to complete different instructions.

Linux system execution from the memory point of view

Mapping between physical memory and virtual:

Experiment--Trace Debug Schedule function
    • Enter the virtual machine environment, boot the kernel, and enter the debug state

    • Set a breakpoint at schedule and C resumes execution.

    • Step through until you encounter the __schedule function, enter it to view

    • Continue execution, Discover Context_switch function

    • After setting a breakpoint, enter its internal view and find the key code of the kernel switch
      ?

(The code can be run in conjunction with the above and analyzed to facilitate understanding.) )

Resources

"Original works reproduced please specify the source"

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

Section eighth, process switching and 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.