Linux kernel eighth section 20135332 Seibu Yao

Source: Internet
Author: User
Tags prev switches

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 the use of a process scheduling policy, select a process as next

    • Context_switch (RQ, prev, n EXT);//Implement the process context switch

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

    • general process switching

      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 (the current process CPU state is pressed into the kernel stack of the user-state process X) Load CS:EIPSS:ESP (loading interrupt service routines and kernel stacks).
      3. enters the interrupt processing process, first Save_all, save live
      4. interrupt processing, or call schedule () before the interrupt is returned, where Switch_to has made a critical process context switch
      5. label 1 to start shipping User-State process y (where Y has been switched out through the above steps so you can continue from label 1)
      6. restore_all Restore the execution status of process x
      7. Iret-pop cs:eip/ss:esp/eflags from Kernel stack
      8. continues to run the user-state process y
      Several special cases
      • By interrupting the timing of the processing process, the user-state process and the kernel threads switch between each other and the kernel threads switch to each other, with the most general situation is very similar, except that there is no process user-state and kernel-state conversion during kernel thread running, and

      • Kernel thread actively calls schedule (), only the process context switch, no interrupt context switch, less than the most general situation;

      • Creates a child process's system call to the execution starting point in the subprocess and returns the user state, such as fork;

      • Loading a new executable program and returning to the user state, such as Execve;

Linux kernel eighth section 20135332 Seibu Yao

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.