Process switching and general execution of the system "go"

Source: Internet
Author: User
Tags prev

Transferred from: http://www.cnblogs.com/20135124freedom/p/5391170.html

Chen Min wo Original works reproduced please indicate the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

I. Basic knowledge of process scheduling

several different classifications of the process: The first Category: Type one:i/o-bound: frequent I/O, often spending a lot of time waiting for I/O operations to complete; type two:cpu-bound: computationally intensive, It takes a lot of CPU time to do the second classification: type one: batch process ; type two: real-time process ; type three: interactive process.

scheduling policy: is a set of rules that determine when and how to choose a new process to run, the scheduling of Linux based on ticks and priorities: with the change of the version, the time-sharing technology is changing, Linux supports both the ordinary timeshare process, but also supports the real process, Scheduling in Linux is a mixture of various scheduling strategies and scheduling algorithms.

Linux processes are queued according to priority: Calculates the priority of a process based on a particular algorithm, represented by a value indicating how the process is allocated appropriately to the CPU. The priority of processes in Linux is dynamic:The scheduling priority adjusts the priority of the process periodically based on the behavior of the process: processes that are not allocated to the CPU for a long time, usually rise, and processes that have been running on the CPU for a long time, usually fall. 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, only by the user state to the kernel state execution Process ContextContains all the information that is required for the process to execute. User address space: Includes program code, data, user stack, and other control information: process descriptors, kernel stacks and other hardware contexts (note that interrupts are also saved in the hardware context except that the method is saved) General execution process analysis of Linux systems:At this point we can have a condition to understand the general operating state of the Linux system, where a user-state process x needs to switch to user process Y. The process of switching from a running user-state process x to a 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 Kernel stack) 3.save_all//save scene 4. Call during interrupt processing or before an interrupt is returned Schedule (), where the switch_to made a critical process context switch 5. After the label 1, the user-state process y (where Y has been switched out through the above steps so that it can continue from label 1) 6.restore-all//Recovery is now Field 7.iret-pop cs:eip/ss:eip/eflags from kernel stack 8. Continue to run user-state process y two. Process scheduling key Code AnalysisWe also have a number of system calls that can implement the priority of the process, such as the following code:
    Nice     getpriority    sched_getscheduler/sched_setscheduler    sched_getparam/sched_setparam    sched_ Yield    Sched_get_priority_min/sched_get_priority_max
Sched_rr_get_interval
Scheduling algorithm strategy is just a strategy algorithm, regardless of the strategy used, is selected from the team to execute the next process, the scheduling algorithm and other parts of the coupling. process scheduling should be aware of the following four points:Interrupt processing (including clock interrupts, i\o interrupts, system calls, and exceptions), call schedule () directly, or return to the user state based on the need_resched tag call schedule (); Kernel threads can directly invoke schedule () for process switching, Can also be scheduled in the interrupt processing process, that is, the kernel thread as a special kind of process can be active, can also be passive scheduling, the user state process could not realize the active scheduling, can only be trapped in the kernel state of a time to dispatch, that is, in the interrupt processing process, scheduling, the user state can only be passively dispatched, Kernel threads are special processes that have no user state in the kernel state, and kernel threads can be actively dispatched or passively dispatched. Schedule Function: Schedule function Implementation scheduling, the purpose: in the running team to find a process, the CPU allocated to it, call method: Direct use of schedule (); loosely invoked, according to need_resched tag next = Pick_next_task (RQ, prev) ; The process scheduling algorithm encapsulates this function's internal context_switch (RQ, Prev, next); The process context Switch switch_to toggles the state of the stack and register, taking advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process context dispatch-related code analysis: in switch ():
31#define switch_to (prev, Next, last) 32do {33/* * Context-switching clobbers All registers, so we clobber * them explicitly, via unused output Variab     Les. * (EAX and EBP are not listed because EBP are saved/restored PNS * explicitly for Wchan access and EAX are the return Value of $ * __SWITCH_TO ()) */unsigned long ebx, ECX, ed                X, ESI, EDI;           ASM volatile ("pushfl\n\t"/* Save flags *//Save FLAGS43 of the current process    "PUSHL%%ebp\n\t"/* Save EBP *//stack base address of the current process "MOVL%%esp,%[prev_sp]\n\t"/* Save ESP *//Save the current stack top to Prev->thread.sp45 "MOVL%[next_sp],%%esp\n\t"/* restore ESP *///Save the stack top of the next process to ESP         , these two sentences complete the kernel stack switch, "MOVL $1f,%[prev_ip]\n\t"/* Save EIP *///Save the current process of EIP, can recover from this 47  "PUSHL%[next_ip]\n\t"/*/restore EIP *//press the start position of the next process to the stack, which is the top of the next process.                   NEXT_IP is generally $1f, for the newly created child process is ret_from_fork//general with return direct NEXT_IP pop out of the __switch_canary "JMP __switch_to\n"/* regparm call *///JMP Pass the parameters via registers, that is, the a,d behind. Function __switch_to also has a return to next_ip pop out of "1:\t"//think from this start to execute next process (EIP angle), the first command is NEXT_IP this starting point, but the front has been completed Kernel stack switching, is already the next process kernel stack (calculate prev process, relatively vague) "POPL%%ebp\n\t"/* Restore EBP *///next process was once a prev process, pressure stack over EBP "popfl\n"/* Restore Flags */parame/* Output             ters * *: [prev_sp] "=m" (PREV->THREAD.SP),//current process, inside interrupt, in kernel State, SP is stack top 56 of kernel stack [PREV_IP]                                  "=m" (PREV->THREAD.IP),//EIP of the current process "=a" (last), 58 */* Clobbered OUTPUT registers: */"=b" (EBX), "=c" (ECX),"=d" (edx), "=s" (ESI), "=d" (EDI) 62 63                __switch_canary_oparam/* Input parameters: */ [next_sp] "M" (NEXT->THREAD.SP),//stack top of the kernel stack for the next process [Next_ip] "M" (next-> THREAD.IP),//start of execution of the next process. */Regparm parameters for __switch_to ()                                  : * * [prev] "a" (prev),//register transfer [next] "D" (next) 72 __switch_canary_iparam 74 7                  5:/* Reloaded Segment Registers */"memory"); (0)
three. Several special cases during Linux system execution:1. 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, 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; 2. Kernel thread actively calls Schdule (), Only the process context of the switch, no interruption of the context of the switch, and the most general situation is slightly abbreviated; 3. Create a child process's system call execution starting point in the child process and return to the user state, such as fork;4. After loading a new executable program and returning to the user state, such as EXECVE; Four. Small summary in the video:Linux Operating System Architecture Overview Here We summarize what we have learned in this lesson: 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 section)-Other programs (such as libraries, shell programs, system programs, etc.) the purpose of the operating system-interacting with the hardware, Manage all hardware resources-provide a good execution environment for user programs (applications) then we can look at a graph: The bottom layer has disk management, physical memory management, memory controller and console, this place it has a kernel intelface to the hardware, That is, the management of hardware resources, and then in the previous layer is the implementation of the kernel, the implementation of the kernel is CPU scheduling, memory management, on-demand scheduling, virtual memory and so on, there are other drivers, Disk Management, file systems and so on, these operating system cores, The kernel of our course analysis is just the most critical code in the computer operating system, in fact the entire operating system is very complex, many other parts are not involved. So, this place has a system call interface, here involves different parts, the basic software is share shared library lib, dynamic loader these and so on.

2.ls command-The simplest and most complex operation

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

1. From the point of view of the CPU execution instructions:

2. From the memory point of view

Five. Experimental process and analysis

Build an experimental environment:

CD Linuxkernel   rm menu-rfgit clone HTTPS://GITHUB.COM/MENGNING/MENU.GITCD menumv test_exec.c test.cmake rootfs

GDB debugging

Qemu-kernel linux-3.18.6/arch/x86/boot/bzimage-initrd rootfs.img-s-sgdbfile. /linux-3.18.6/vmlinuxtarget remote:1234 Set breakpoints: B scheduleb pick_next_taskb Context_switchb switch_to

3. Set breakpoints at schedule, run, and expand functions with List

4. Single Step operation until __schedule ()

5. Set breakpoints at Context_switch to perform

Six. Knowledge Summary

The process scheduler is an important part of the kernel, because the running process is first using the computer (at least most of us see it). However, meeting the various needs of process scheduling is not easy, it is difficult to find the "one-size-fits-all" calculation stick, not only suitable for a number of operational processes, but also scalability, and in the scheduling cycle and throughput balance, but also to meet the needs of various load. However, the new CFS scheduler for the Linux kernel is trying to meet all aspects of the requirements and provides the best solution for better scalability and novel side-down. The previous chapters cover the content of process management, and this chapter examines the basic principles, implementation, scheduling, and interfaces used by the current Linux kernel to process scheduling.

Process switching and general execution of the system "go"

Related Article

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.