20135239 Silam Linux kernel Analysis process switching and system general execution process

Source: Internet
Author: User
Tags prev

Week 8 process switching and general execution of the system "20135239 original please specify the source" Linux kernel Analysis "MOOC course http://mooc.study.163.com/course/USTC-1000029000" one, Timing analysis of process scheduling and process scheduling
  • The principle of operating system describes a large number of process scheduling algorithms, these algorithms from the perspective of implementation is only to choose a new process from the run queue, 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.
  • Different types of processes have different scheduling requirements
  • First Category:

    • I/o-bound

      • Frequent I/O
      • It usually takes a lot of time to wait for I/O operations to complete
    • Cpu-bound

      • COMPUTE-intensive
      • Requires a lot of CPU time to perform operations
  • The second type of classification

    • Batch processing process
      • No need to interact with the user, usually running in the background
      • No need to respond quickly
      • Typical batch process: compiler, scientific calculation
    • Real-time processes
      • Have real-time requirements and should not be blocked by low-priority processes
      • Short response times and stability
      • Typical real-time processes: Video/audio, mechanical control, etc.
    • Interactive process
      • Requires frequent interaction with the user, so it takes a long time for the user to enter the action
      • Response time is faster and average latency is lower than 50~150ms
      • Typical interactive programs: shell, text editing programs, graphics applications, etc.
  • Linux supports both common ticks and real-time processes

  • Scheduling in Linux is a mixture of various scheduling strategies and scheduling algorithms
  • What's the dispatch policy?
    • is a set of rules that they decide when and how to choose a new process to run
  • Linux scheduling is based on ticks and priorities
    • As the version changes, the time-sharing technology is changing
  • Linux processes are queued according to priority
    • Calculates the priority of a process based on a particular algorithm, with a value representing
    • This value indicates how the process is properly allocated to the CPU
  • The priority of processes in Linux is dynamic
    • The scheduler periodically adjusts the priority of the process based on the behavior of the process
      • A process that is not allocated to the CPU for a long time, usually up
      • Processes that have been running on the CPU for a long time usually go down
  • Scheduling algorithm related code in the kernel uses a policy pattern similar to that in Ood
  • The scheduling algorithm is coupled with the other parts
  • 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 call schedule () for process switching, or they can be dispatched during interrupt processing, which means that kernel threads can be scheduled or passively dispatched as a special class of processes.

    • The user-state process is not able to implement active scheduling, only can be dispatched by a point in time after the kernel state, that is, in the interrupt processing process scheduling.

  • User-State process can only be dispatched passively
  • Kernel threads are special processes that only have a kernel state without a user state
  • Kernel threads can be actively scheduled or passively dispatched
Ii. correlation Analysis of Process context code
  • Switching of processes

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

    • Suspend a process that is executing on the CPU, which is different from the Save field at the time of the outage, before and after the interrupt is in the same process context, but only from the user state to the kernel state execution;

    • 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 descriptors, kernel stacks, etc.

    • Hardware context (note that interrupts are also saved by the hardware context just by saving the method differently)

    • The schedule () function selects a new process to run and invokes thecontext switch for a contextual switchover, which callsswitch to for critical context switching

    next = picknexttask (RQ, prev);//process scheduling algorithms encapsulate this function inside

    Context_switch (RQ, Prev, next);//process Context switch

    switchto take advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process ' 1.31#define switchto (prev, next, last) \

  • 32do {\
  • 33/* \
  • * Context-switching clobbers All registers, so we clobber \
  • * them explicitly, via unused output variables. \
  • * (EAX and EBP are not listed because EBP is saved/restored \
  • PNS * Explicitly for Wchan access and EAX are the return value of \
  • *switchto ()) \
  • 39 */\
  • unsigned long ebx, ecx, edx, ESI, EDI; \
  • 41 \
  • ASM volatile ("pushfl\n\t"/* Save flags */\
  • "PUSHL%%ebp\n\t"/* Save EBP */\
  • "Movl%%esp,%[prev_sp]\n\t"/* save ESP */\
  • "Movl%[next_sp],%%esp\n\t"/* restore ESP */\
  • "Movl $1f,%[prev_ip]\n\t"/* Save EIP */\
  • "PUSHL%[next_ip]\n\t"/* restore EIP */\
  • Switchcanary \
  • "JMP _Switchto\n"/* regparm call */\
  • "1:\t" \
  • Wuyi "POPL%%ebp\n\t"/* restore EBP */\
  • "Popfl\n"/* Restore flags */\
  • 53 \
  • */* OUTPUT parameters */\
  • : [prev_sp] "=m" (PREV->THREAD.SP), \
  • [Prev_ip] "=m" (PREV->THREAD.IP), \
  • "=a" (last), \
  • 58 \
  • */* Clobbered output registers:/\
  • "=b" (EBX), "=c" (ecx), "=d" (edx), \
  • "=s" (ESI), "=d" (EDI) \
  • 62 \
  • Canary switchoparam \
  • 64 \
  • */* Input parameters: */\
  • : [next_sp] "M" (next->thread.sp), \
  • [Next_ip] "M" (NEXT->THREAD.IP), \
  • 68 \
  • */* Regparm parameters for _Switchto (): */\
  • [Prev] "a" (prev), \
  • [Next] "D" (next) \
  • 72 \
  • Canary switchiparam \
  • 74 \
  • :/* Reloaded Segment Registers */\
  • "Memory"); \
  • (0)

  • NextIP is generally ¥1f, for newly created child processes are RETfrom_fork

Third, the general execution process analysis of Linux system
    • General execution of Linux systems

    • The most common scenario: 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 occurred--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 site
      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//Recovery site
      7. Iret-pop cs:eip/ss:esp/eflags from kernel stack
      8. Continue to run user-state process y
    • Interrupt context switching and process context switching
Iv. Several special cases during the implementation of Linux system

Several special cases

• By interrupting the timing of the process, the user-state process and the kernel thread 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 switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;

• Create a child process's system call execution starting point in the subprocess and return the user state, such as fork; (NextIP = retfrom_fork)

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

Five, the core and the dancer

The kernel is a combination of various interrupt processing processes and kernel threads.

VI. Linux operating System Architecture overview
    • Basic concepts of the operating system

    • The structure of a typical Linux operating system

    • The simplest and most complex operation

    • Stand at the angle of the CPU execution instructions

    • From the memory point of view

Seven, the simplest and most complex operation--execute the LS command
    • Enter the LS command under the console

    • The shell program parses the input parameters and determines that this is the LS command

    • Call the system call Fork to generate a copy of the shell itself
        • What is a system call? ----->> Memory Protection, kernel-state user-state related issues
        • How is the system call implemented? --->> soft break, anomaly concept, trap door, System door
        • What is fork? Description of the---->>> process, creation of the process
        • Why to call fork--->cow technology
    • Call the exec system call to load the LS executable file into memory
    • Returning from System call

      • How do I get the right return?
    • Both Shell and LS are able to perform-->> process scheduling, running queue waiting queue maintenance

Eight, from the CPU and memory point of view of the implementation of Linux system
    1. Execute the gets () function;
    2. Execute system call, fall into kernel;
    3. Waiting for input, the CPU dispatches other processes to execute while wait for an I/O interrupt;
    4. Hit LS, send I/O interrupt to the CPU, interrupt processing program for on-site storage, pressure stack and so on;
    5. The interrupt handler discovers that the X process is waiting for this I/O (at which point X has become blocked) and the handler sets X to Wake_up;
    6. Process management may set process X to the next process so that the get system invokes the data and returns to the user-state stack

    1. From a memory point of view, all physical addresses are mapped to more than 3G of address space: Because this part is shared for all processes

20135239 Silam Linux kernel Analysis process switching and system general execution process

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.