Linux Fourth reading notes

Source: Internet
Author: User

Fourth chapter: Process scheduling one, multi-tasking1. Non-preemptive multi-tasking

The process is executed until it is actively stopped (this step is called concession)

2. Preemptive multi-tasking

The Linux/unix uses a preemptive approach; the action of the forced suspend process is called preemption. The time a process can run before it is preempted is pre-set (that is, the time slice of the process)

Ii. Concepts related to Strategy1. Type of consumption of the process
    1. I/O consumption process
      • Most of the process is used to commit I/O requests or wait for I/O requests
      • Most user graphical interfaces (GUIs) are I/O intensive
    2. Processor-intensive
      • Time is mostly used to execute code.
      • such as Matlab
      • Often need to lengthen the running time and reduce the scheduling frequency
2. Process priority
    1. Priority-based scheduling: high-priority processes run first, and processes with the same priority are scheduled on a rotational basis;
    2. Priority is divided into two categories
      • 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
    3. Time slices
      • A time slice indicates the time at which a process can continue to run before it is preempted, and the scheduling policy 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.
third, Linux scheduling algorithm1. 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;
System scheduling problem in 2.Unix
    1. If the nice value is mapped to a time slice, the nice value must correspond to the absolute time of the processor, which causes the process switch to not be optimal;
    2. If you use a relative nice value, the effect will be greatly dependent on the initial value of Nice;
    3. If you perform a nice value to a time slice mapping, the time slice is greatly constrained by the timer.
3. Fair Dispatch
    1. CFS is based on a simple idea: the effect of process scheduling should be as good as the system has a perfect task processor. 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's operating ratio obtained by the process (rather than the time slice determined entirely by Nice);
      • Each process runs with a "time slice" corresponding to the proportion of its weights in all the running processes

"The so-called" fish and bear paw can not be "that is so-the smaller the scheduling cycle will show the better the interactivity, but also closer to the" simultaneous completion of multi-tasking "this diligently pursued the goal; However, the system has to withstand higher switching costs and poorer system throughput--even with most of its energy on the back and forth Daoteng.

iv. implementation of Linux scheduling1. Time Accounting
    • All schedulers must account for the running time of the process;
    • CFS uses scheduler entity structure to track running bookkeeping
2. Virtual Real-time
    • The vrntime variable "that is, in the entity structure mentioned above," stores the virtual run time. The virtual time is in NS and is independent of the beat timer;
    • The Update_curr () function implements the bookkeeping function, calculates the execution time of the current process and stores it in Data_exec, and then passes the run time to _update_curr (), which calculates the elapsed time based on the total number of currently running processes. Finally, the weight value above is determined with the vrntime of the current running process.
3. Process Selection
    1. The core of the CFS algorithm: selecting tasks with minimal vrntime
    2. How to: Use Red-black tree rbtree (two-fork tree that stores data as nodes)
    3. 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

"From this we can see that the binary tree is all stored in the executable process."

4. Process scheduling 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 Pick_next_state (), examine each scheduling class in turn, and select the highest priority process from the highest priority scheduling class.
5. Sleep and Wake up
    1. 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 that wait for some time to occur, and the kernel uses wake_queue_head_t to represent the waiting queue
    2. The wake operation is performed by the function wake_up ()
      • It calls the function try_to _wake_up () sets the process to the task_running state, and calls Enqueue_task () to place the process into the red-black tree
      • Of course, there is also the state of the false wakeup process
v. preemption and Context switching1. Context switching is the responsibility of the Context_switch () function defined in KERNEL/SCHED.C, which is called whenever a new process is selected to be 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
2.Linux System supports kernel preemption
    1. As long as there is no lock, the kernel can process preemption;
    2. In order to support preemption, each process of the thread_info are added to the Preempt_count counter (the initial value is 0, each time the lock is used to add 1, release the lock when the value minus 1), when the value of 0, the kernel can preempt
    3. 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

Linux Fourth 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.