2017-2018-1 20179205 "Linux kernel Principles and design" Nineth Week assignment

Source: Internet
Author: User
Tags exception handling prev

"Linux kernel Principles and design" The Nineth week work video learning and code analysis first, process scheduling timing and process switching

Different types of processes have different scheduling requirements, the first classification: I/O-bound the frequent process I/O, usually takes a lot of time to wait for I/O operations to complete, CPU-bound is computationally intensive, requires a lot of CPU time to operate, making other interactive processes unresponsive, Therefore, different algorithms are needed to make the system run more efficiently, and the CPU resources can be used to the fullest extent. The second classification includes the batch process, the real-time process (real-time processes), and the interactive processes (interactive process). Different classifications require different scheduling strategies, that is, deciding when and how to choose a new process to run. Linux scheduling is based on time-sharing and priority. Queued according to priority, and the priority is dynamic.

Timing of process scheduling
  • Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly to find a process in the run queue, assign the CPU to it, or call schedule () based on the need_resched tag when returning to the user state. The user-state process can only be dispatched passively;

  • Kernel thread is a special process that only the kernel state does not have the user state, it can call schedule () for process switching, or it can be dispatched during interrupt processing, that is, the kernel thread can be dispatched as a special kind of process, or it can be dispatched passively. The kernel thread may interrupt the execution of the clock interrupt, I/O interrupt, but the system call will not occur, because it can directly access the kernel functions;

  • 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.

    Process switching and related code analysis
  • 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, task switching, context switching;

  • 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, but only by the user-state to the kernel state execution;
  • The process context contains all the information required by the process execution

    ??
  • schedule()The function selects a new process to run, and invokes Context_switch for context switching, which is called by the macroswitch_toFor critical context switching

    next = pick_next_task(rq, prev);Packaging a process scheduling strategy

    context_switch(rq, prev, next);Process Context Switch

    switch_to(pre,next,prev)Toggles the state and stack of registers. Take advantage of Prev and next two parameters: Prev points to the current process, next points to the scheduled process

    Related Code Analysis:

    #define SWITCH_TO (prev, Next, last)//prev points to the current process, next points to the scheduled process do {unsigned long ebx, ecx, edx, ESI, EDI; ASM volatile ("pushfl\n\t"//Save the current proximity of the flags "PUSHL%%ebp\n\t"//Put the base address of the current process on the stack "MOVL%%esp,%[prev_sp]\n \ t "//Save the current process's stack top ESP to THREAD.SP" MOVL%[next_sp],%%esp\n\t "//Put [NEXT_SP] into ESP, which completes the switchover of the kernel stack" MOVL $1f,%[prev_i P]\n\t "//Put 1f into [prev_ip], save the EIP of the current process, restore" PUSHL%[next_ip]\n\t "from here when recovering the Prev process//Put the starting point of the next process, that is, the location of the IP to the stack, next_i    P is typically $1f __switch_canary "jmp __switch_to\n"//execute __switch_to () function, passing Parameters "prev][next],eax" via registers [1:\t and edx] "Popl%%ebp\n\t" "popfl\n"/* Output parameters */: [prev_sp] "=m" (PREV->THREAD.SP),//For better readability, use string [ PREV_SP] Tag parameters [prev_ip] "=m" (Prev->thread.ip), "=a" (last),/* Clobbered output registers: */"=b" (EBX), "=c" (ecx), "=d" (edx), "=s" (ESI), "=d" (EDI) __switch_canary_oparam/* Input Parameters: */: [next_sp ] "M" (Next->thread.sp), [next_ip] "M" (NEXT->THREAD.IP),/* Regparm parameters for __switch_to ():*/[prev] "a" (prev), [   Next] "D" (next) __switch_canary_iparam:/* Reloaded Segment Registers */"memory");   } while (0)
    Second, the general implementation process Analysis of Linux system

    The most common scenario: the process of running user-state process x switching to 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 (POI NT to kernel stack). CPU automatically completes save and load

    3. SAVE_ALL //Save site

    4. Schedule () is called before interrupt processing or interrupt return, where a switch_to critical process context switch is made//the kernel stack of the x process is switched to the kernel stack of the next process, and the EIP is switched

    5. after the label 1 begins to run the user-state process y (here Y has been switched out through the above steps so you can continue from the label 1)

    6.Restore_all//recovery site

    7.iret-pop cs:eip/ss:esp/eflags from kernel stack//take the Y process to the cs:eip/ss:esp/eflags pop that is stored in the kernel stack when an interrupt occurs

    8. continue to run the 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;

  • The user-state process cannot actively dispatch schedule (), but the kernel thread can actively call schedule (), only the process context switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;

  • The system call that creates the child process starts and returns the user state in the subprocess, then does not start from the label 1, executes from the user state, and next ip=ret from work, such as fork;

  • A situation in which a new executable program is loaded and returned to the user state, such as EXECVE;//Modify the interrupt context

    The core and the dancer

    0 to 3G users can access, more than 3G only the kernel state can access. In fact, all processes above 3G are fully shared, such as process x switch to process Y, but the address space is more than 3G, but the process descriptor and other process context switch, wait until the return to the user state will be different, in the kernel state regardless of which process code snippet and stack segment are identical, Therefore, it is easier to switch between the cores. There is a metaphor in the video that is the kernel and the dancer. The kernel is like a taxi, the process is a dancer, which process beckons can enter the kernel, walk a section can return to the user state. The kernel does not process when it enters the No. 0 process idle idling, there is a process interruption into the kernel state.

    Linux Operating System Architecture overview

    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, and so on)

    The purpose of the operating system

      • Interact with hardware, manage all hardware resources
      • Provides a good execution environment for user programs (applications)

    The simplest and most complex operation-execute the LS command

    The implementation of Linux system from the CPU point of view

    Looking at Linux systems from a memory perspective

    Reference: Image Link

    Teaching Materials 15, 16 chapters study

    1, the process address space consists of a process addressable virtual address, and the kernel allows the process to use this virtual memory address. Typically, each process has a unique, flat address space. Any valid address in the process address space can only be in a unique region, and these memory areas cannot be overwritten with each other.

    2. The address space of one process and the address space of another process, even with the same memory address, are actually irrelevant to each other, calling such a process a thread. Its parent process wants to share space with its child processes and can set flags when cloning () is called CLONE_VM ;

    3, the kernel zone of the process contains a variety of memory objects, such as: Executable code can contain a variety of memory mappings, called Code Snippets (text section), the executable's initialized global variables of the memory map, called data section, including uninitialized global variables, That is, the 0 pages of the BSS section (the information in the page is all 0 values, so it can be used to map the BSS segment, etc.) memory mapping; Memory mapping for the process user space stack;

    4. The kernel uses the memory descriptor structure to represent the address space of the process, which contains all the information about the process address space. The memory descriptor is mm_struct represented by the struct body. When allocating a memory descriptor, the fork () function uses copy_mm() the function to copy the memory descriptor of the parent process. Undoing the memory descriptor, the kernel calls the function defined in kernel/exit.c, the 的exit_mm() kernel thread does not have a process address space, and there is no associated memory descriptor, and the MM field in the process descriptor for all kernel threads is empty.

    5, the memory area is described by the vm_area_struct structure, defined in the file <linux/mm_types.h>. Memory areas are also often referred to as virtual address spaces in Linux. The VMA flag is a bit flag that is defined in <linux/mm.h>, which is contained mm_flags within a domain, which flags the behavior and information of the pages contained in the memory area.

    6. You can access the memory area through MMAP in the memory descriptor and one of the mm_rb domains. Each of these domains independently points to a memory-descriptor-related whole memory area object. In fact, they contain pointers to exactly the same am_area_struct structure, only different organizational methods. The mmap domain uses a separate chain table to connect all the memory area objects, and mm_rb the domain uses the red-black number to link all the memory area objects.

    7. The kernel provides functions to find out which memory area a given memory address belongs to find_vma() .

    8 do_mmap() . The function adds an address space to the address space of the process-whether it is expanding an existing area of memory or creating a new zone-the do_munmap() function removes the address space from a particular process space.

    9. When a program accesses a virtual address, the virtual address must first be converted to a physical address before the processor can resolve the address access request. Address conversion needs to be done by querying the page table, in summary, address translation needs to fragment the virtual address, so that each segment of the virtual address as an index to the page table, and the page table point to the next level of page table or point to the final physical page. Use the Level three page table in Linux to complete the conversion.

    10, the page cache (cache) is the Linux kernel implementation of the disk cache. Write caching is generally implemented as one of the following three strategies: the first policy is called Non-caching (nowrite); the second strategy, write automatically updates the memory cache, and also updates the disk files. The third strategy, also used by Linux, is called "write-back." Under this strategy, the program writes directly to the cache and is added to the Dirty page list. The pages in the Dirty page list are then periodically written back to disk by a process (write-back process), so that the data in the disk is eventually consistent with in-memory.

    11. The page cache is searched by two parameters address_space redemption plus an offset. Each address_space object has a unique cardinality, which is stored in the address_space struct. The cardinality is a binary tree, and you can quickly retrieve the desired page in the cardinality as long as the file offset is specified.

  • 2017-2018-1 20179205 "Linux kernel Principles and design" Nineth Week assignment

    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.