20135302 Wei Quiet--book 4th study notes

Source: Internet
Author: User
Tags bitmask

4th Chapter Process scheduling
    • Scheduling: Scheduling is a balanced process. On the one hand, it is to ensure that each operation of the process to maximize the use of CP, on the other hand, to ensure that the process of fair use of the CPU.

    • Scheduling function: Determines which process runs and how long the process runs.

    • Scheduling implementation principle: Related to the priority of the process

    • Method of scheduling implementation on Linux: O (1) Scheduling algorithm

    • Scheduling-related system calls

4.1 Multi-tasking
    Multi-tasking systems can be divided into two categories: non-preemptive multitasking (cooperative multitasking) and preemptive multi-tasking
(preemptive multitasking). Like the
With a variant of UNIX and many other modern operating systems, Linux provides preemption
Multi-tasking mode. In this mode, it is up to the scheduler to decide when to stop a process from running,
So that other processes
Be able to get execution opportunities. This mandatory suspend action is called preemption (preemption). The process can run before it is preempted
The time is pre-set, and there is a
A special name, called the process of the time slice (timeslice). Time slices actually
Is the processor time period assigned to each running process. Effective management of time slices enables the scheduler to
The Global perspective does
The scheduling decision, this also prevents individual processes from monopolizing system resources. Today, many modern operating systems operate on programs that are
The dynamic time slice method is used, and the introduction
A configurable calculation strategy. But we will see that Linux elder sister no two
The "fair" scheduling level itself does not take time slices to achieve fair dispatch.
4.2Linux of process scheduling


0 (1) Scheduler one by one it is the theta that is named because of its calculated behavior. It solves many of the shortcomings of previous versions of the Linux scheduler and introduces many powerful new features and performance features.

This is mainly thanks to the static time slice algorithm and the run queue for each processor, which helps us to get rid of the limitations of the previous scheduling program.
The 0 (1) scheduler, while still showing near-perfect performance and scalability in a multi-processor environment with a few 10 (not hundreds) of processors, has time to prove that the scheduling

Scheduling those that respond to time-sensitive programs has some inherent weaknesses. These programs we call the interactive process one by one it undoubtedly includes all programs that require user interaction.

4.3 strategy

Policy determines when the scheduler will run what process

    • 1/0 consumption-type and processor-consumable processes
      • The process can be divided into UO consumption type and processor consumption type. The former refers to the process most of the time used to submit 1/0 requests or
        is to wait for the ν0 request. Processor-based processes spend most of their time on executing code. Unless they are preempted, they are usually
        Keep running because they don't have too much 1/0 demand.
      • A scheduling strategy typically seeks to strike a balance between two conflicting objectives: rapid process response (short response times) and maximum system
        Utilization (high throughput).
    • Process Priority
      • Nice value, the color range is from 20 to +19, the default value is 0: The larger the nice value means the lower priority
      • Real-time priority, its value is configurable, and by default it varies from 0 to 99 (including 0 and 99). In contrast to the nice value, the higher the real-time priority value means the higher the process priority.
4.4Linux Scheduling algorithm
    • Scheduler Class
      • The Linux Scheduler is provided in a modular manner to allow different types of processes to be selectively selected
        The choice of scheduling.
        This modular structure is called the Scheduler Class (Scheduler classes), which allows for a variety of different dynamically added schedules
        Can coexist, scheduling belongs to their own category of the process. Each scheduler has a priority, and the underlying scheduler code is defined in the
        KEMEL/SCBED.C file, it traverses the scheduling class in order of precedence, with the highest priority of an executable process
        The class wins and chooses the program to be executed below.
        The Complete Fair Dispatch (CFS) is a scheduling class for normal processes, called Sched_normal in Linux.
        (called Sched_other in POSIX), the CFS is implemented as defined in the file Kemel/scbed_ fair.c.
Implementation of 4.5Linux Scheduling
  • Time Billing
    • CFS no longer has the concept of time slices, but it must also maintain the time accounting for each process run, because it needs to ensure that
      Each process runs only within the processor time that is fairly allocated to it. CFS uses the scheduler entity structure (defined in file sched.h> struct_sched _entity) to track the process of running the bookkeeping
    • CFS uses the Vruntime variable to record how long a program is running and how long it should run.
      The Update_curr () function defined in the Kemevsched_fair.c file implements the accounting function.
    • Update_ CurrO calculates the execution time of the current process and stores it in the variable delta_exec. Then it will
      Run time is passed to an up also Te_curr (), which then weights the run time based on the total number of currently running processes
      Count The weight value above is finally added to the vruntime of the current running process.
  • Process Selection
    • The process selection algorithm of CFS can be summarized simply as "the one on the leftmost leaf node in the run RBTR back tree."
      Process ". The function that implements this procedure is _pick_next_ entityq, which is defined in the file Kemel/sched_ sword.
    • How to add a process to Rbtree, the enqueue_entity () function achieves this purpose
  • Dispatcher entry
    • The primary entry point for process scheduling is the function schedule (), which is defined in file kemel/sched. C. It is the kernel other
      Partially invokes the entry of the process scheduler with a child. It calls Pick_next_task () (also defined in file KERNEL/SCHED.C). Pick_
      Next_task () takes precedence, high to low, checks each scheduling class sequentially, and schedules from the highest priority class
      , select the highest-priority process.
  • sleep and wake up
    • Dormant (blocked) processes are in a special non-executable state.
    • Hibernation is processed by waiting for a queue. The wait queue is a simple list of processes that wait for certain events to occur. Within
      The kernel uses the Wake_queue_ head _t to represent the waiting queue. Wait queue can be established through Decl Waitqueue () static
      Can also be created dynamically by Init_waitqueue _head ().
    • The process adds itself to a wait queue by performing the following steps :
      • 1) Call the macro def Brush e_wait () to create an item waiting for the queue.
        2) Call Add_wai!_ queue () to add itself to the queue. The queue wakes up when a process waits for a condition that is full.
        Of course we have to write the code somewhere else, and when the event happens, Wake_u to wait for the queue to take action.
        3) Call Prepare_to_ Wait () party station to change the status of the process to task_ terruptible or task_
        Uninterruptible. And if necessary, the function adds the process back to the waiting queue, which is the next
        Required in the loop traversal.
        4) If the status is set to Task_interruptible, the bass I] signal wakes up the process. This is called pseudo-awakening.
        (Wakeup is not caused by events), so check and process the times.
        5) When the process is awakened, it checks to see if the condition is true again. If it is, it exits the loop: if not
        Yes, it again calls the schedule () well to repeat this operation.
        6) When the condition is met, the process sets itself to task_running and calls the finish_wait () party to move itself
        Out of the waiting queue.
        If the condition is reached before the process starts to hibernate, the loop exits and the process does not enter the
        tendency to sleep. It is important to note that kernel code often needs to do some other tasks in the loop body, for example, it may
        You need to release the locks before calling schedule (), and then regain them later, or respond to other events.
        function Inotify_r, mortar D (), located in the file Fs/notify/inotify/inotify_ user.c, is responsible for reading from the notification file descriptor
        Access information.
    • The wake-up operation is performed through the function Wake_upo, which wakes up all the processes on the specified wait queue. It calls the letter
      Number Y_to_wake_upo, which is responsible for setting the process to the task_running state, calling Enqueue_task () to
      This process is placed in a red-black tree.
4.6 Preemption and Context switching

Context switching, that is, switching from one executable process to another executable process, defined in KERNEL/SCHED.C
The Context_switch () function is responsible for processing. Whenever a new process is chosen to be ready for operation, schedule ()
The function is called. It has completed two basic tasks:
• Invoke switch_mm () declared in , which is responsible for putting virtual memory from the previous

The process map switches to the new process.
• Invoke switch_to () declared in <asm/system.h>,

This function is responsible for switching from the processor state of the previous process to the processor state of the new process. This includes saving, restoring, checking, and register information, as well as

Any other architecture-related state information must be managed and saved for each process object. _add_w Blood ' _queue mission

Added to the wait queue. Then call Schedule ()

    • User preemption
      • When the kernel is about to return to user space, if the need_resched flag is set, it will cause Scheduleo to be called.
        User preemption occurs at this time. When the kernel returns to the user space, it knows that it is safe, since it can
        Continue to execute the current process, then of course it can choose a new process to execute. So, either the kernel is at the point of interruption
        After the system call, the NEED_RESCHED flag will be checked. If it is set, then the kernel will select
        Other (more appropriate) processes to run. The return path returned from the interrupt handler or system call is followed by the system
        Structure-related, in en-cho. s (this file contains not only the kernel entry part of the program, the kernel exit part of the relevant code is also in its
        The file is implemented by assembly language.
        In short, user preemption occurs when the following conditions occur
        . When returning user space from system transfer.
        • When returning user space from an interrupt handler.
    • Kernel preemption
      • Kernel preemption can occur at 2
        • The interrupt handler is executing and before the kernel space is returned.
        • Once again the kernel code is preemptive.
        • If the task in the kernel explicitly calls schedule () O
        • If the task in the kernel is blocked (this also invokes schedule ()).
4.7 Real-Time scheduling strategy
    • Linux offers two real-time scheduling strategies: Sci rents d_fifo and SCHED_RR.
    1. The SCHED_FIFO implements a simple, first-in, first-out scheduling calculation: It does not use time slices. In a running
      The process at the SCHED_FIFO level of the state will be dispatched before any sched_normal-level processes. One and a
      SCI D_fifo level process is in an executable state and will execute until it is blocked by itself or explicitly releases the processor
      , it is not based on time slices and can be carried out continuously. Only higher-priority SCHED_FIFO or SCHED_RR
      Tasks to preempt sched_fifo tasks. If there are two or more sched_fifo-level processes with a priority, it
      Will take turns, but will only exit if they are willing to give up the processor. As long as there is a sched_fifo level process
      At execution time, other lower-level processes can only wait for it to become non-operational before the opportunity executes.
    2. The SC block RR-level process runs out of pre-assigned to it
      Time will not be able to continue to carry out. In other words, SC block RR is a sched_fifo-with a
      Real-time rotation scheduling calculation hang. When the SCHED_RR task is done with its time, other real-time processes at the same priority are
      Rotation scheduling. A time slice is only used to reschedule a process of the same priority. For SCHED_FIFO processes, high-priority is always
      Immediately preempt the low priority, but the low priority process must never preempt the SCHED_RR task, even if its time slice is out of the picture.
4.8 Scheduling-related system calls
  •   system calls related to scheduling policies and priorities
    • sched _ Setschedulero and Sched__getscheduler ( ) is used to set and get scheduling policies and real-time priority
      levels for a process, respectively. Similar to other system calls, their implementations are chimneys many parameter checking, initialization, and cleanup. In fact, the most heavy
      work is to read or overwrite the values of the policy and rt_priority of the process tast_struct.
      Sched _ SetParam () and Sched__getparam () are used to set and get the real-time priority of a process, respectively. The two systems
      call families are encapsulated in the rt_priority of the Sched_param special struct. Sched__get_priority_max 0 and Sched_
      get_priority _min () are used to return the maximum and minimum precedence for a given scheduling policy, respectively. The maximum priority of the real-time scheduling policy
      is Max_ User_rt_prio minus 1, and the minimum priority equals 1.
      For a normal process, the nice () function can increase the static priority of a given process by a given amount. Only a super-
      user can use a negative value when calling it, thereby increasing the priority of the process. The nice () function calls the kernel's set_ user_
      Nice () function, which sets the Static_prio and Prio values for the task_struct of the process.
  • System calls related to processor bindings
    • The Linux Scheduler provides mandatory processor bindings (processor affinity? Mechanism. In other words, the louse tries to make it.
      A soft, or natural, affinity that attempts to make a process run on the same processor as much as possible, but it also allows the user
      Forces the specified "this process must run on these processors anyway". This compulsive affinity is preserved in the process Task_
      The cpus_allowed of the struct is in this bitmask flag. Each bit of the mask flag corresponds to a system-available processor. Implied
      In case, all bits are set and the process can be executed on all available processors in the system. Users can pass
      Sched _ Setaffinity () sets a bitmask of different combinations of one or more bits, while calling Scbed_ getaffinity () returns when
      Before the knock us_allowed bitmask.
  • Discard Processor Time
    • Linux is called through the Sched_yieldo system, providing a way for the process to explicitly cede processor time to other
      The mechanism of the line process. It is by moving the process from the active queue (because the process is executing, so the color is definitely located in this queue when
      is implemented in the expiration queue. The resulting effect not only grabs the process and puts it into the last of the priority queue
      and put it in an expired queue one by one this ensures that it will not be executed for a period of time. Because real-time processes
      Does not expire, so it is an exception. They are moved only to the last face of their priority queue, which is not placed in the expiration queue. In
      In earlier versions of Linux, the Semantics of Sched_yield () were different, and the process was placed only at the end of the priority queue and placed
      The time of abandonment is often not too long. Now, the application and even kernel code should be carefully considered before calling Sched_yield ()
      No really want to discard the processor time.
      Kernel code for convenience, you can directly call yield (), first to determine that a given process is actually in the executable state, and then
      Applications that call Sched__yield () o User space can use the Sched__yield () system call directly.

20135302 Wei Quiet--book 4th study 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.