The process of understanding process scheduling and process switching during the time-tracking analysis process

Source: Internet
Author: User
Tags prev

the process of understanding process scheduling and process switching during the time-tracking analysis process20135111 Li Guangji Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

I. Experimental requirements :

1, the topic self-proposed, the content revolves on the process scheduling time and the process switch carries on;

  • 2, can combine the key code, experiment, stack state, CPU register state, etc.

  • 3, the blog content needs to carefully analyze the process scheduling timing, switch_to and corresponding stack state and so on.

  • 4, the summary part needs to clarify oneself to "the Linux system General execution process" the understanding.

  • two. Experimental content :

  • 1, understand the timing of the process scheduling in the Linux system, you can search the kernel code in the schedule () function, see where the call Schedule (), to determine whether the summary of our course content is accurate;

    • 2, using the GDB trace analysis of a schedule () function, verify your understanding of the Linux system process scheduling and process switching process.

    • 3, 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;

    • 4, according to this week's knowledge analysis and understanding of Linux process scheduling and process switching process.

    • Three. The experimental process
    • 2. (1) GDB Trace Analysis of a schedule () function
    • (2) Experimental Code analysis
    • #define switch_to (prev, Next, last)
    • \32 do {
      \34 * context-switching clobbers all registers, so we clobber
    • \35 * them explicitly, via unused output variables.
    • \36 * (EAX and EBP is not listed because EBP is saved/restored
    • \37 * Explicitly for Wchan access and EAX are the return value of
    • \38 * __SWITCH_TO ()
    • \40unsignedlongebx, ecx, edx, ESI, EDI;
    • \42asmvolatile("pushfl\n\t"/* Save the current process's flag save flags */
    • \43 "PUSHL%%ebp\n\t"/* pushes the stack base address of the current process to save EBP */
    • \44 "MOVL%%esp,%[prev_sp]\n\t"/* Save the current stack top, save to THREAD.SP [PREV_SP] is the use of labels, similar to the previous%1, where the use of string tag parameters
    • Save ESP */
    • \45 "MOVL%[next_sp],%%esp\n\t"/* Put the stack of the next process into the ESP register, from here on, all the stacks are in the next process, restore ESP */
    • \46 "Movl $1f,%[prev_ip]\n\t"/ * Save the EIP for the current process and restore the save Eip from here when the prev current process is resumed */
    • \47 "PUSHL%[next_ip]\n\t"/* to the next process starting point is the location of the IP stack, which is pressed to the next process stack, next process stack top is his starting point. The Restore EIP */\48__switch_canary\49 "jmp __switch_to\n"/* differs from the call calling function with the Register pass parameter regparm called */\
    • "1:\t"// from this start to execute the next process the first statement, the above statement is very vague, which process is not good to say, but from 45 lines after the next process has been stacked. \51 "POPL%%ebp\n\t"/* Restore EBP */
    • \52 "popfl\n"/* Restore Flags */
    • \54/* OUTPUT Parameters */
    • \55: [prev_sp] "=m" (PREV->THREAD.SP),//current process, because the interrupt is internal in the kernel state, the SP core stack stack top.
    • \56 [prev_ip] "=m" (PREV->THREAD.IP),//Thread.ip of the current process
    • \57 "=a" (last), \58\59/* Some registers that need to be destroyed. clobbered Output registers: */
    • \60 "=b" (EBX), "=c" (ecx), "=d" (edx), \61 "=s" (ESI), "=d" (EDI)
    • \63__switch_canary_oparam
    • \65/* Input Parameters: */
    • \66: [next_sp] "M" (NEXT->THREAD.SP),//input:next->thread.sp next process kernel stack stack top
    • \ [next_ip] "M" (Next->thread.ip),//next->thread.ip The starting point for the next process execution
    • \69/* regparm parameters for __switch_to (): */
    • \70 [prev] "a" (prev),
    • \71 [Next] "D" (next)
    • \73__switch_canary_iparam
    • \75:/* Reloaded Segment Registers */
    • \76 "Memory");
    • \77} while (0)
    • 3. Understanding Context Switching mechanisms
    • The timing of process scheduling interrupts processing (including clock interrupts, I/O interrupts, system calls, and exceptions), calling schedule () directly, or calling schedule () from the need_resched tag when returning to the user state; Kernel threads can call schedule () directly Process switching, can also be scheduled during interrupt processing, that is, the kernel thread as a class of special processes can be active scheduling, can also be passive scheduling, the user state process is unable to achieve active scheduling, only through the kernel state after a certain point in time to dispatch, that is, in the interrupt processing process scheduling. The schedule () function selects a new process to run and invokes Context_switch for context switching, a macro called SWITCH_TO for critical context switching
    • Next = Pick_next_task (RQ, prev);//process scheduling algorithms encapsulate this function internally Context_switch (RQ, Prev, next);//process Context Switch Switch_ To take advantage of Prev and next two parameters: Prev points to the current process, and next points to the scheduled process.
    • Four. Experimental experience
    • This experiment gives us a good understanding of the process scheduling mechanism of multiple operating systems. Process scheduling timing and process switching, operating system principle introduced a large number of process scheduling algorithms, these algorithms from the perspective of implementation is only from the running queue to select a new process, the process of selecting the use of different strategies. It is more important to understand the working mechanism of the operating system than the process scheduling timing and process switching mechanism. Process switching, 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 a process switch task switch context switch.

The process of understanding process scheduling and process switching during the time-tracking analysis 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.