20135239 Silam Linux Kernel Analysis The fourth chapter of reading notes

Source: Internet
Author: User

Chapter 4 Process scheduling over 4.1 tasks
    • A multitasking operating system is an operating system that can simultaneously concurrently execute multiple processes concurrently.
    • Multi-tasking systems can be divided into two categories:

       - 非抢占式多任务:        - 进程会一直执行直到自己主动停止运行(这一步骤称为让步) - 抢占式多任务:      - Linux/Unix使用的是抢占式的方式;强制的挂起进程的动作就叫做抢占。进程在被抢占之前能够运行的时间是预先设置好的(也就是进程的时间片)
Process scheduling for 4.2 Linux
    1. O (1) Scheduler: The workload on large servers is ideal, but the interaction process is missing.
    2. Inversion stair deadline scheduling algorithm (RSDL)
    3. Fully Fair scheduling algorithm (CFS)
4.3 strategy
    • The policy determines what program the scheduler is running in.
4.3.1 Process Classification
    • I/O consumption type
      • Most of the time the process is used to commit I/O requests or wait for I/O requests, often in a running state but running for a short period of time, and eventually blocking when waiting for more requests.
    • Processor consumption type

      • Most of the time is spent on executing code, unless it is preempted, and will usually run continuously.
    • A scheduling strategy usually seeks to balance the two conflicting goals:

      • Fast process scheduling (short response times)
      • Maximum system utilization (high throughput)
    • Linux tends to prioritize I/O consuming processes

4.3.2 Process Priority
    • The most basic type of scheduling algorithm is priority-based scheduling:
      • High-priority processes run first; processes with the same priority are scheduled on a rotational basis
    • The scheduler always chooses the process in which the time slice is not exhausted and the highest priority is run.
    • Linux uses two different priority ranges:
      • Nice value (from -20--+19): The default value is 0; the larger the number means the lower the priority; You can view the list of system processes by Ps-el and find the priority of the NI tag column
      • Real-time priority (from 0--99): higher real-time priority levels mean higher process priorities
      • They don't interact with each other
4.3.3 Time Slice
    • A time slice represents the time that a process can continue to run before it is preempted.
    • The scheduling strategy must determine a default time slice;
    • The CFS scheduler for Linux does not divide the time slices directly into the process, but instead divides the processor usage into processes. That is, the timing of the preemption depends on how much processor usage the new executable consumes, and if the consumed usage is smaller than the current process, the new process immediately runs to preempt the current process.
4.3.4 Scheduling policy Activity 4.4 Linux Scheduling algorithm 4.4.1 Scheduler class
    1. The Linux scheduler is provided in a modular manner (that is, the Scheduler Class) to allow different types of processes to selectively select scheduling algorithms
    2. Scheduler class allows a variety of dynamically added scheduling algorithms coexist, scheduling belongs to their own category of processes;
    3. The scheduler code traverses the scheduling class in order of precedence, and the Scheduler class with the highest priority of an executable process wins, choosing the program to be executed below;
Process scheduling in 4.4.2 Unix system

The scheduling algorithm used by UNIX is to allocate absolute time slices, which will trigger a fixed switching frequency, which is not conducive to fairness. The CFS used by Linux completely abandons the time slice, assigning the process a processor to use the weighting, guaranteeing constant fairness and changing the switching frequency.

4.4.3 Fair Dispatch (CFS)
    • The starting point of CFS is based on a simple idea: the effect of process scheduling should be as good as the ideal task processor in the system.
    • The CFS practices are as follows:

      • Allow each process to run for a period of time, cycle round, select the least-run process as the next running process;
      • The nice value is the weight of the processor run ratio as the process gets: the absolute nice value no longer affects the scheduling decision, and their relative values affect the processor time allocation ratio-the geometric weighting.
      • Each process runs with a "time slice" corresponding to the proportion of its weights in all the running processes
    • Target delay: Approximate value of an infinitely small dispatch period

    • Minimum granularity: The time slice bottom line that each process obtains, the default is 1ms.
    • There is no time-slice concept but time-keeping is still required.
Implementation of 4.5 Linux scheduling

--The implementation of the CFS scheduling algorithm.

Four components:

- 时间记账- 进程选择- 调度器入口- 睡眠和唤醒
4.5.1 Time Accounting
    • All schedulers must account for the running time of the process;
    • CFS uses scheduler entity structure to track running bookkeeping
    • 1. Scheduler Entity Structure
      • CFS uses the scheduler entity structure to track process run accounting:
    • 2. Virtual Real-time
      • CFS uses the Vruntime variable to hold the virtual run time of the process, to indicate how long the process is running, and how long it should run.
      • This virtual run time is weighted, regardless of the timer beat.
      • The virtual run time is in NS units.
      • The associated function is updateCurr (), which calculates the execution time of the current process and puts it into the variable Deltaexec, and then passes the run time toupdatecurr ();
      • _UpdateCurr () weights the time based on the total number of currently running processes, and eventually adds the weight value to the Vruntime value of the current running process.
4.5.2 Process Selection
    • The core of the CFS algorithm: selecting tasks with minimal vrntime
    • How to: Use Red-black tree rbtree (two-fork tree that stores data as nodes)
    • Example:
      • Select the next task: traverse the binary tree from the root node and continue to the leaf node (i.e. the vrntime smallest process);
      • To join a process to a tree: the process becomes executable or the first time the process is created by a fork () call;
      • Remove a process from a tree: Occurs when a process is blocked or terminated

**linux, red black tree is called Rbtree, is a self-balancing binary search tree, is a tree node form of data stored, the data will correspond to a key value, the key value can be used to quickly retrieve the data on the node, and the retrieval speed and the entire tree node size into an exponential ratio relationship. **

    1. Pick the next task:

      The node key value is the virtual run time of the running process, the process selection algorithm is "the process of running the leftmost leaf node of the rbtree tree species", the function is picknextentity ()

    2. To join a process to the tree:
      • Occurs when the process is awakened or the first time a process is created through a fork call.
      • function Enqueueentity (): Update run time and some other statistics, and then call enqueueentity (). function enqueue_entity (): Perform heavy insertion work and actually insert data items into the red-black tree:
    3. Remove a process from the tree

      • Delete actions occur when a process is blocked or terminated.

      • The correlation functions are dequeueentity () and dequeueentity ():

4.5.3 Scheduler Entry
    1. The main entry point for process scheduling is the function schedule (), which is defined in KERNEL/SCHED.C; this is where the internal and other parts are used to dispatch the process scheduler.
    2. The most important task of this function is to call picknextState (), examine each scheduling class in turn, and select the highest priority process from the highest priority scheduling class
4.5.4 Sleep and wake up
    • Process hibernation must be to wait for some events
      • The process marks itself as dormant, removed from the executable red-black tree;
      • Put into the wait queue-a list of processes waiting for some time to occur, and the kerneluses wake queue head_t to represent the waiting queue

3. The wake-up operation is performed by the function wake_up ()

- 它会调用函数try_to _wake_up()将进程设置为TASK_RUNNING状态,调用enqueue_task()将进程放入红黑树中- 当然,也存在虚假唤醒进程的状态
4.6 Preemption and Context switching
    • Context switches are responsible for the Context_switch () function defined in KERNEL/SCHED.C, which is called whenever a new process is selected to run, schedule ():

      • Call SWITCH_MM (), which is responsible for switching the virtual memory from the previous process map to the new process;
      • Call Switch_to (), which is responsible for switching from the processor state of the previous process to the processor state of the new process
    • Linux system supports kernel preemption
      • As long as there is no lock, the kernel can process preemption;
      • In order to support preemption, each process's threadinfo is addedto the preempt count counter (the initial value is 0, each time the lock is used to add 1, release the lock when the value is reduced by 1), when the value is 0, the kernel can preempt
    • Kernel preemption occurs in:
      • The interrupt handler is executing and before the kernel space is returned;
      • The kernel code once again has the time to be preemptive;
      • The task in the kernel explicitly calls the schedule function
Resources

Linux kernel design and implementation

20135239 Silam Linux Kernel Analysis The fourth chapter of reading notes

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.